-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
302 lines (225 loc) · 7.69 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
output:
md_document:
fig_height: 0.3
fig_width: 3
variant: gfm
html_document:
fig_height: 0.3
fig_width: 3
---
```{r options, echo=FALSE}
knitr::opts_chunk$set(comment="#", tidy=FALSE)
```
# chroma
`chroma` is a [R](https://www.r-project.org) package for parsing and formating colors in various specifications, manipulating colors, and creating nice color scales and palettes. Much of the functionality is based on the excellent [chroma.js](https://github.com/gka/chroma.js/) javascript library by [Gregor Aisch](http://driven-by-data.net).
## Installation
`chroma` is not on CRAN yet. To get it from this page, install the package `devtools` and
```{r install, eval=F}
devtools::install_github("jiho/chroma")
library("chroma")
```
```{r load, echo=F}
library("ggplot2")
devtools::load_all(".", quiet=TRUE)
```
NB: some functions in the `chroma` package mask functions in basic R packages (e.g. `grDevices`) or often used ones (e.g. `ggplot2`). Most of the time, they are argument-compatible with the function they mask and, if their result is different, I would argue that the one in chroma is more correct. If you want to use chroma but would rather not mask the other functions, just install the package but do not load it with `library()`; instead, just call the functions with the `chroma::` prefix (e.g. `chroma::rgb()` instead of `rgb()`, to avoid masking `rgb()` in package `grDevices`).
## Color parsing
Parse colors in various specifications
```{r parse}
rgb(r=0.5, g=0.5, b=0.5)
ryb(r=0.5, y=0.5, b=0.5)
hsv(h=120, s=0.5, v=0.5)
hsl(h=120, s=0.5, l=0.5)
hsi(h=120, s=0.5, i=0.5)
hcl(h=120, c=0.5, l=0.5)
lab(l=0.5, a=-0.5, b=0.2)
hex("#393939")
hex("F39")
css("rgb(100,100,100)")
temperature(5600)
wavelength(500)
```
Parse directly from a matrix
```{r parse_matrix, comment="#"}
x <- matrix(c(0.2, 0.5, 0.5, 0.5, 0.6, 0.4), ncol=3)
print(x)
rgb(x)
```
## Color formating
Convert back in various formats
```{r convert}
as.rgb("coral1")
as.ryb("coral1")
as.hsv("coral1")
as.hsl("coral1")
as.hsi("coral1")
as.hcl("coral1")
as.lab("coral1")
as.hex("coral1")
as.css("coral1")
as.temperature("coral1")
as.wavelength("coral1")
```
## Color manipulation
Slightly modify a base color
```{r modify_color}
col <- "#7BBBFE"
show_col(c(col, brighten(col), darken(col)))
show_col(c(col, desaturate(col), saturate(col)))
```
Make a color semi-transparent
```{r alpha}
col <- "#7BBBFE"
show_col(c(col, alpha(col), alpha(col, 0.2)))
```
Mix or blend two colors
```{r mix}
show_col(c("#7BBBFE", "#FDFF68", mix("#7BBBFE", "#FDFF68")))
show_col(c("#7BBBFE", "#FDFF68", blend("#7BBBFE", "#FDFF68")))
show_col(c("#7BBBFE", "#FDFF68", blend("#7BBBFE", "#FDFF68", mode="screen")))
```
Compute the contrast between two colors
```{r contrast}
contrast("darkblue", "darkgreen")
contrast("yellow", "darkgreen")
```
Compute the Euclidean or perceptual distance between two colors (following the CIE Delta E 2000 formula)
```{r delta_e}
# pick three colors; lightgreen is closer to darkgreen that darkblue is
show_col(c("darkblue", "darkgreen", "lightgreen"))
color_distance("darkblue", "darkgreen")
color_distance("lightgreen", "darkgreen")
CMClc("darkblue", "darkgreen")
CMClc("lightgreen", "darkgreen")
deltaE("darkblue", "darkgreen")
deltaE("lightgreen", "darkgreen")
```
which allows to find the closest perceived color in an array of possibilities
```{r min_dist, fig.height=0.3*3}
target <- "limegreen"
choices <- rainbow(10)
closest_color <- choices[which.min(deltaE(target, choices))]
show_col(target, choices, closest_color)
```
Extract or set a color channel
```{r channel}
col <- "#7BBBFE"
channel(col, model="hcl", "h")
channel(col, model="hsv", "s")
channel(col, model="hsi", "i")
channel(col, model="rgb", "r")
col1 <- col2 <- col
channel(col1, model="hcl", "h") <- 120
channel(col2, model="hcl", "l") <- 1
show_col(c(col, col1, col2))
```
Compute or set the perceived luminance of a color
```{r luminance}
luminance(c("red", "yellow", "darkblue"))
col1 <- col2 <- col <- "#7BBBFE"
luminance(col)
luminance(col1) <- 0.6
luminance(col2) <- 0.2
show_col(c(col, col1, col2))
```
## Color scales and palettes
All scales and palettes are organised the same way:
- functions ending in `*_scale` return a *function* that takes a numeric vector `x` as argument and returns the corresponding colors in the scale.
- functions ending in `*_map` are shortcuts that build the scale, map the values, and return the colors.
- functions ending in `*_palette` return a *function* that takes an integer `n` as argument and returns `n` equally spaced colors along the scale.
- functions ending in `*_colors` are shortcut that create the palette and return the `n` colors.
```{r scales}
x <- 0:10/10
s <- interp_scale()
s(x)
# or
interp_map(x)
n <- 11
p <- interp_palette()
p(n)
# or
interp_colors(n)
```
Palettes can be built by interpolating between colors
```{r scale_interp}
show_col(interp_colors(10))
show_col(interp_colors(10, colors=c("#2D2B63", "#F7FF84")))
show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84")))
```
For the perception of "change" along the scale to be more linear, colors can be interpolated using bezier curves and their lightness can be corrected.
```{r scale_interp_correct}
show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84")))
show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84"), interp="bezier"))
show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84"), interp="bezier", correct.lightness=TRUE))
```
Preset palettes are available, from colorbrewer
```{r colorbrewer, fig.height=0.3*35, fig.width=2.1}
show_col(lapply(brewer_info$name, function(x) {brewer_colors(n=7, name=x)}))
```
Or viridis
```{r viridis, fig.height=0.3*4}
show_col(
viridis_colors(10),
magma_colors(10),
plasma_colors(10),
inferno_colors(10)
)
```
Or cubehelix
```{r cubehelix, fig.height=0.3*4}
show_col(
cubehelix_colors(10),
cubehelix_colors(10, h=300, rot=-0.75),
cubehelix_colors(10, h=120, rot=0.5),
cubehelix_colors(10, h=300, rot=0.5)
)
```
Or ETOPO1 and Wikipedia for topographical maps
```{r etopo, fig.height=0.3*2}
show_col(
etopo_colors(100),
wikitopo_colors(100)
)
```
Custom, perceptually appropriate, palettes can be built in HCL space, for either discrete
```{r hue}
show_col(hue_colors(10))
```
or continuous variables
```{r chroma_light}
show_col(chroma_colors(10, h=140))
show_col(light_colors(10, h=140))
```
## Helper functions
Additional functions are included to facilitate the use of the color scales in base and ggplot2 plots.
`sidemargin()` and `sidelegend()` allow to place legends on the side of base plots
```{r sidelegend, fig.height=4, fig.width=5}
attach(iris)
# make larger right margin
pars <- sidemargin()
# plot the data with a nice color scale
plot(Petal.Length, Petal.Width, col=hue_map(Species), pch=19)
# add a legend for the scale on the side
sidelegend(legend=levels(Species), col=hue_colors(nlevels(Species)), pch=19)
# set graphical parameters back to default
par(pars)
detach(iris)
```
`persp_facets()` allows to compute the colors for each facet of a `persp()` plot
```{r persp_facets, fig.height=4, fig.width=5}
persp(maunga, theta=50, phi=25, scale=FALSE, expand=2,
border=alpha("black", 0.4),
col=magma_map(persp_facets(maunga$z)))
```
Various `scale_color_*()` and `scale_fill_*()` functions allow to use the color scales defined in chroma directly with ggplots and `scale_xy_map()` draws nice `x` and `y` scales for maps.
```{r ggplot2, fig.height=4, fig.width=5}
ggplot(iris) +
geom_point(aes(Petal.Length, Sepal.Length, color=Species)) +
scale_color_cubehelix_d(h=0, rot=0.75, c=1, l=c(0.6, 0.6))
ggplot(thaixyz) + coord_quickmap() +
geom_raster(aes(x, y, fill=z)) +
scale_fill_wikitopo() + scale_xy_map()
```
---
Happy coloring!