-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathREADME.Rmd
166 lines (133 loc) · 4.95 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# farver <a href='https://farver.data-imaginist.com'><img src='man/figures/logo.png' align="right" height="131.5" /></a>
<!-- badges: start -->
[![R-CMD-check](https://github.com/thomasp85/farver/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/thomasp85/farver/actions/workflows/R-CMD-check.yaml)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version-ago/farver)](https://cran.r-project.org/package=farver)
[![CRAN_Download_Badge](http://cranlogs.r-pkg.org/badges/farver)](https://cran.r-project.org/package=farver)
[![Codecov test coverage](https://codecov.io/gh/thomasp85/farver/branch/main/graph/badge.svg)](https://app.codecov.io/gh/thomasp85/farver?branch=main)
<!-- badges: end -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
The goal of farver is to provide very fast, vectorised functions for
conversion of colours between different colour spaces, colour comparisons
(distance between colours), encoding/decoding, and channel manipulation in
colour strings. To this end it provides an interface to a modified
version of the [ColorSpace](https://github.com/berendeanicolae/ColorSpace) C++
library developed by Berendea Nicolae.
## Installation
farver can be installed from CRAN using `install.packages('farver')`. The
development version can be installed from Github using `devtools`:
```{r, eval=FALSE}
# install.packages('devtools')
devtools::install_github('thomasp85/farver')
```
## Use
farver provides an alternative to the `grDevices::rgb()` and
`grDevices::col2rgb()` for encoding and decoding colours strings. The farver
functions are superficially equivalent but provides a uniform output format, and
the option to encode and decode directly from/to other colour spaces.
```{r}
library(farver)
codes <- rainbow(10)
codes
spectrum <- decode_colour(codes)
spectrum
encode_colour(spectrum)
```
It also provides an alternative to `grDevices::convertColor()` to switch between
colours spaces. If the origin is a colour string it is possible to decode
directly into the given colour space. Conversely, if the endpoint is a colour
string it is also possible to encode directly from a given colour space.
```{r}
spectrum_lab <- convert_colour(spectrum, 'rgb', 'lab')
spectrum_lab
decode_colour(codes, to = 'lab')
encode_colour(spectrum_lab, from = 'lab')
```
If colours are given as strings, manipulation of channels will normally require
decoding, conversion to the correct colour space, manipulation of the given
channel, converting back to rgb and the encoding to string. farver provides a
range of functions that allow you to change any channel in the supported spaces
directly in colour strings:
```{r}
# Add a value to the channel
add_to_channel(codes, channel = 'l', value = 1:10, space = 'lab')
# Set a channel to a specific value
set_channel(codes, 'alpha', c(0.3, 0.7))
# Limit a channel to a given value
cap_channel(codes, 'r', 200)
```
Lastly, farver also provides utilities for calculating the distance between
colours, based on a range of different measures
```{r}
spectrum2 <- t(col2rgb(heat.colors(10)))
compare_colour(spectrum, spectrum2, 'rgb', method = 'cie2000')[1:6, 1:6]
```
## Supported colour spaces
`farver` currently supports the following colour spaces:
- CMY
- CMYK
- HSL
- HSB
- HSV
- CIE L*AB
- Hunter LAB
- OK LAB
- LCH(ab)
- LCH(uv)
- LCH(OK)
- LUV
- RGB
- XYZ
- YXY
## Supported distance measures
`farver` supports the following colour distance metrics
- Euclidean
- CIE1976
- CIE94
- CIE2000
- CMC
## White References
`farver` allows you to set the white point for relative colour spaces, either
based on a standard illuminant (A-F series supported) or by specifying
chromaticity coordinates or tristimulus values directly
## Benchmark
`farver` is faster than its `grDevices` counterpart but less so than it was at
its first release, as the colour conversion in grDevices has been improved
since.
```{r, message=FALSE}
library(ggplot2)
test <- matrix(runif(300000, min = 0, max = 255), ncol = 3)
timing <- bench::mark(
farver = convert_colour(test, 'rgb', 'lab'),
grDevices = convertColor(test, 'sRGB', 'Lab', scale.in = 255),
check = FALSE,
min_iterations = 100
)
plot(timing, type = 'ridge')
```
Still, if the start- and/or endpoint are colour strings the ability
to decode and encode directly from/to any colour space will give a huge speed
up.
```{r, message=FALSE}
colour_strings <- colours()
timing <- bench::mark(
farver = decode_colour(colour_strings, to = 'lab'),
grDevices = convertColor(t(col2rgb(colour_strings)), 'sRGB', 'Lab', scale.in = 255),
check = FALSE,
min_iterations = 100
)
plot(timing, type = 'ridge')
```
## Code of Conduct
Please note that the 'farver' project is released with a
[Contributor Code of Conduct](https://farver.data-imaginist.com/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.