text_size <- 16
linesize <- 1
width <- 12
height <- 6

# col_lightgray <- "#e7e7e7"
# col_blue <- "#F24159"
# col_b_smooth <- "#F7CE14"
# col_p_smooth <- "#58A64A"
# col_pointwise <- "#772395"
# col_b_constant <- "#BF236D"
# col_p_constant <- "#F6912E"
# col_optimum <- "#666666"

# https://www.schemecolor.com/retro-rainbow-pastels.php
col_lightgray <- "#e7e7e7"
col_blue <- "#F24159"
col_b_smooth <- "#5391AE"
col_p_smooth <- "#85B464"
col_pointwise <- "#E2D269"
col_b_constant <- "#7A4E8A"
col_p_constant <- "#BC677B"
col_optimum <- "#666666"
col_auto <- "#EA915E"

T_selection <- c(32, 128, 512)

# Lambda grid
lamgrid <- c(-Inf, 2^(-15:25))

# Gamma grid
gammagrid <- sort(1 - sqrt(seq(0, 0.99, .05)))

material_pals <- c(
  "red", "pink", "purple", "deep-purple", "indigo",
  "blue", "light-blue", "cyan", "teal", "green", "light-green", "lime",
  "yellow", "amber", "orange", "deep-orange", "brown", "grey", "blue-grey"
)
cols <- purrr::map(material_pals, ~ ggsci::pal_material(.x)(10)) %>%
  purrr::reduce(cbind)
colnames(cols) <- material_pals

cols %>%
  as_tibble() %>%
  mutate(idx = as.factor(1:10)) %>%
  pivot_longer(-idx, names_to = "var", values_to = "val") %>%
  mutate(var = factor(var, levels = material_pals[19:1])) %>%
  ggplot() +
  xlab(NULL) +
  ylab(NULL) +
  geom_tile(aes(x = idx, y = var, fill = val)) +
  scale_fill_identity() +
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) +
  theme_minimal() -> plot_cols

col_gas <- "blue"
col_eua <- "green"
col_oil <- "amber"
col_coal <- "brown"

col_scale2 <- function(x, rng_t) {
  ret <- x
  for (i in seq_along(x)) {
    if (x[i] < rng_t[1]) {
      ret[i] <- col_scale(rng_t[1])
    } else if (x[i] > rng_t[2]) {
      ret[i] <- col_scale(rng_t[2])
    } else {
      ret[i] <- col_scale(x[i])
    }
  }
  return(ret)
}

rng_t <- c(-5, 5)
h_sub <- c(1, 5, 30)

col_scale <- scales::gradient_n_pal(
  c(
    cols[5, "green"],
    cols[5, "light-green"],
    cols[5, "yellow"],
    # cols[5, "amber"],
    cols[5, "orange"],
    # cols[5, "deep-orange"],
    cols[5, "red"]
  ),
  values = seq(rng_t[1], rng_t[2], length.out = 5)
)
