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.
Installation
farver can be installed from CRAN using install.packages('farver').
The development version can be installed from Github using devtools:
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.
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.
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')
#> [1] "#FF0C03" "#FF9E0E" "#D5FF1C" "#48FF20" "#33FF74" "#3CFFFF" "#3D77FF"
#> [8] "#5A25FF" "#E839FF" "#FF41B4"
# Set a channel to a specific value
set_channel(codes, 'alpha', c(0.3, 0.7))
#> [1] "#FF00004C" "#FF9900B2" "#CCFF004C" "#33FF00B2" "#00FF664C" "#00FFFFB2"
#> [7] "#0066FF4C" "#3300FFB2" "#CC00FF4C" "#FF0099B2"
# Limit a channel to a given value
cap_channel(codes, 'r', 200)
#> [1] "#C80000" "#C89900" "#C8FF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF"
#> [8] "#3300FF" "#C800FF" "#C80099"
Lastly, farver also provides utilities for calculating the distance
between colours, based on a range of different measures
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.
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
)
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
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.
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.
farver
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.
Installation
farver can be installed from CRAN using
install.packages('farver'). The development version can be installed from Github usingdevtools:Use
farver provides an alternative to the
grDevices::rgb()andgrDevices::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.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.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:
Lastly, farver also provides utilities for calculating the distance between colours, based on a range of different measures
Supported colour spaces
farvercurrently supports the following colour spaces:Supported distance measures
farversupports the following colour distance metricsWhite References
farverallows 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 directlyBenchmark
farveris faster than itsgrDevicescounterpart but less so than it was at its first release, as the colour conversion in grDevices has been improved since.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.
Code of Conduct
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.