|
| 1 | +--- |
| 2 | +title: "Lecture 6 - Color" |
| 3 | +output: html_notebook |
| 4 | +--- |
| 5 | + |
| 6 | +# Loading data and preparing workspace |
| 7 | +```{r} |
| 8 | +library("colorspace") |
| 9 | +library("viridis") |
| 10 | +library("RColorBrewer") |
| 11 | +library("ggthemes") |
| 12 | +``` |
| 13 | + |
| 14 | +# |
| 15 | +```{r} |
| 16 | +pal_viridis <- viridis::cividis(6) |
| 17 | +pal_set2 <- RColorBrewer::brewer.pal(6, "Set2") |
| 18 | +# Most used categorical palette |
| 19 | +pal_tableau <- ggthemes::tableau_color_pal()(6) |
| 20 | +pal_viridis |
| 21 | +``` |
| 22 | +```{r} |
| 23 | +colorspace::swatchplot( |
| 24 | + "Viridis" = pal_viridis, |
| 25 | + "Brewer" = pal_set2, |
| 26 | + "Tableau" = pal_tableau |
| 27 | + ) |
| 28 | +``` |
| 29 | +## Assessing quantitatively palettes |
| 30 | +```{r} |
| 31 | +colorspace::specplot(pal_tableau) |
| 32 | +``` |
| 33 | + |
| 34 | +## Assessing quality of palettes for color blind people |
| 35 | +```{r} |
| 36 | +pal_deu_tableau <- colorspace::deutan(pal_tableau) |
| 37 | +``` |
| 38 | +```{r} |
| 39 | +pal_deu_tableau |> |
| 40 | + colorspace::specplot() |
| 41 | +``` |
| 42 | +```{r} |
| 43 | +colorspace::swatchplot( |
| 44 | + "no cvd"=pal_tableau, |
| 45 | + "cvd"=pal_deu_tableau |
| 46 | +) |
| 47 | +``` |
| 48 | +###Protan blindness |
| 49 | +```{r} |
| 50 | +pal_viridis |> |
| 51 | + protan() |> |
| 52 | + specplot() |
| 53 | +``` |
| 54 | + |
| 55 | +### Tritan blindness |
| 56 | +```{r} |
| 57 | +pal_viridis |> |
| 58 | + tritan() |> |
| 59 | + specplot() |
| 60 | +``` |
| 61 | +## Grey-White |
| 62 | +```{r} |
| 63 | +pal_viridis |> |
| 64 | + desaturate(1) |> |
| 65 | + specplot() |
| 66 | +``` |
| 67 | + |
| 68 | +```{r} |
| 69 | +pal_tableau |> |
| 70 | + desaturate(1) |> |
| 71 | + specplot() |
| 72 | +``` |
| 73 | +```{r} |
| 74 | +rainbow(6) |> |
| 75 | + specplot() |
| 76 | +``` |
| 77 | + |
| 78 | +# Quantitative assessment of contrast |
| 79 | + |
| 80 | +Contrast ratio: luminance of the color / luminance of the background |
| 81 | + |
| 82 | +What are good colors? Around 3 is lower bound |
| 83 | + |
| 84 | +```{r} |
| 85 | +colorspace::contrast_ratio(pal_viridis, col2="grey30", plot=TRUE) |
| 86 | +``` |
| 87 | +# How can we fix tableau for blind people? |
| 88 | +Therefore cathegorical data for blind people safe. |
| 89 | +```{r} |
| 90 | +# Okabe and Ito - Japanese professors inventors |
| 91 | +palette.colors(6) |> |
| 92 | + desaturate(1) |> |
| 93 | + specplot() |
| 94 | +``` |
| 95 | + |
| 96 | +# Now we play |
| 97 | +```{r} |
| 98 | +pal_random <- c("#ff6f59", "#254441", "#43aa8b", "#b2b09b", "#ef3054") |
| 99 | +pal_random |> |
| 100 | + specplot() |
| 101 | +``` |
| 102 | +```{r} |
| 103 | +#Then you can define the following function: |
| 104 | +image_spec <- function(img_path, palette) { |
| 105 | + p_image <- cowplot::ggdraw() + |
| 106 | + cowplot::draw_image(img_path) |
| 107 | +
|
| 108 | + p_palette <- tibble(c = palette) |> |
| 109 | + ggplot(aes(x=palette, y=0, fill=palette)) + |
| 110 | + geom_tile(color="white", linewidth=4) + |
| 111 | + coord_fixed() + |
| 112 | + scale_fill_identity() + |
| 113 | + theme_void() |
| 114 | +
|
| 115 | + plot_grid(p_palette, p_image, ncol=1) |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +```{r} |
| 121 | +pal_dune <- c("#D96A6A","#8C4660","#592550","#F8AEA1","#260101") |
| 122 | +img_path <- "data/download.jpg" |
| 123 | +image_spec(img_path, pal_dune) |
| 124 | +``` |
| 125 | + |
| 126 | +```{r} |
| 127 | +pal_dune |> |
| 128 | + desaturate(1) |> |
| 129 | + specplot() |
| 130 | +``` |
| 131 | +```{r} |
| 132 | +pal_burano <- c("#B6DBF2","#049DBF","#055902","#F2B705","#F2CDAC") |
| 133 | +img_path <- "data/images.jpg" |
| 134 | +image_spec(img_path, pal_burano) |
| 135 | +``` |
| 136 | +# Another complicate palette |
| 137 | +```{r} |
| 138 | +base1 <- "#C91024" |
| 139 | +base2 <- "#1E8CE3" |
| 140 | +saturations <- c(0.0, 0.5, 1.0) |
| 141 | +lightenesses <- c(0.0, 0.3, 0.8) |
| 142 | +``` |
| 143 | +```{r} |
| 144 | +base1 |> |
| 145 | + desaturate(0.5) |> |
| 146 | + lighten(0.3) |> |
| 147 | + swatchplot() |
| 148 | +``` |
| 149 | +```{r} |
| 150 | +color_base_tibble <- function(base, saturation, lightness){ |
| 151 | + tibble(c=base, |
| 152 | + sat=saturation, |
| 153 | + light=lightness) |> |
| 154 | + transmute( |
| 155 | + c=lighten(desaturate(c,sat),light), |
| 156 | + c=fct_reorder(c, row_number()) |
| 157 | + ) |
| 158 | +} |
| 159 | +``` |
| 160 | +```{r} |
| 161 | +cols1 <- base1 |> |
| 162 | + color_base_tibble(saturations, lightenesses) |> |
| 163 | + rename(C1=c) |
| 164 | +base2 |> |
| 165 | + color_base_tibble(saturations, lightenesses) |> |
| 166 | + rename(C2=c) |
| 167 | +``` |
| 168 | + |
| 169 | +```{r} |
| 170 | +blend_colors <- function(C1,C2){ |
| 171 | + hex(RGB(coords(hex2RGB(C1)) * coords(hex2RGB(c2)) )) |
| 172 | +} |
| 173 | +``` |
| 174 | +```{r} |
| 175 | +
|
| 176 | +``` |
| 177 | + |
| 178 | + |
| 179 | + |
0 commit comments