High Performance Colourspace Manipulation in R
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 C++
library developed by Berendea Nicolae.
farver can be installed from CRAN using install.packages('farver')
. The
development version can be installed from Github using devtools
:
# install.packages('devtools')
devtools::install_github('thomasp85/farver')
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.
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.
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:
# 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
spectrum2 <- t(col2rgb(heat.colors(10)))
compare_colour(spectrum, spectrum2, 'rgb', method = 'cie2000')[1:6, 1:6]
farver
currently supports the following colour spaces:
farver
supports the following colour distance metrics
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
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.
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.
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')
Please note that the ‘farver’ project is released with a
Contributor Code of Conduct.
By contributing to this project, you agree to abide by its terms.