At the core of the colorspace package are various utilities for computing with color spaces (Wikipedia 2019d), as the name conveys. Thus, the package helps to map various three-dimensional representations of color to each other (Ihaka 2003). A particularly important mapping is the one from the perceptually-based and device-independent color model HCL (Hue-Chroma-Luminance) to standard Red-Green-Blue (sRGB) which is the basis for color specifications in many systems based on the corresponding hex codes (Wikipedia 2019h), e.g., in HTML but also in R). For completeness further standard color models are included as well in the package. The connections are illustrated in the following graph:
Color models that are (or try to be) perceptually-based are displayed with circles and models that are not are displayed with rectangles. The corresponding classes and eponymous class constructors in colorspace are:
RGB()for the classic Red-Green-Blue color model that mixes three primary colors with different intensities to obtain a spectrum of colors. The advantage of this color model is (or was) that it corresponded to how computer and TV screens generated colors, hence it was widely adopted and still is the basis for color specifications in many systems. For example, the hex color codes are employed in HTML but also in R. However, the RGB model also has some important drawbacks: It does not take into account the output device properties, it is not perceptually uniform (a unit step within RGB does not produce a constant perceptual change in color), and it is unintuitive for humans to specify colors (say brown or pink) in this space. (See Wikipedia 2019f.)
sRGB()addresses the issue of device dependency by adopting a so-called gamma correction. Therefore, the gamma-corrected standard RGB (sRGB), as opposed to the linearized RGB above, is a good model for specifying colors in software and for hardware. But it is still unintuitive for humans to work directly with this color space. Therefore, sRGB is a good place to end up in a color space manipulation but it is not a good place to start. (See Wikipedia 2019g.)
HSV()is a simple transformation of the (s)RGB space that tries to capture the perceptual axes: hue (dominant wavelength, the type of color), saturation (colorfulness), and value (brightness, i.e., light vs. dark). Unfortunately, the three axes in the HSV model are confounded so that, e.g., brightness changes dramaticaly with hue. (See Wikipedia 2019e.)
HLS()(Hue-Lightness-Saturation) is another transformation of (s)RGB that tries to capture the perceptual axes. It does a somewhat better job but the dimensions are still strongly confounded. (See Wikipedia 2019e.)
XYZ()was established by the CIE (Commission Internationale de l’Eclairage) based on experiments with human subjects. It provides a unique triplet of XYZ values, coding the standard observer’s perception of the color. It is device-independent but it is not perceptually uniform and the XYZ coordinates have no intuitive meaning. (See Wikipedia 2019a.)
LAB()were therefore proposed by the CIE as perceptually uniform color spaces where the former is typically preferred for emissive technologies (such as screens and monitors) whereas the latter is usually preferred when working with dyes and pigments. However, the three axes of these two spaces still do not correspond to human perceptual axes. (See Wikipedia 2019c, 2019b.)
polarLAB()take polar coordinates in the UV plane and AB plane, respectively. Specifically, the polar coordinates of the LUV model are known as the HCL (Hue-Chroma-Luminance) model. These capture the human perceptual axes very well without confounding effects as in the HSV or HLS approaches. (More details follow below.)
All S4 classes for color spaces inherit from a virtual class
color which is internally always represented by matrices with three columns (corresponding to the different three dimensions).
Note that since the inception of the color space conversion tools within colorspace by Ihaka (2003) other R tools for this purpose became available. Notably, there is
grDevices::convertColor() in base R (R Core Team 2018) as well as
farver::convert_colour() (Pedersen, Nicolae, and François 2018) based on a C++ library. For many basic color conversion purposes the colorspace package and these alternatives are equally suitable. Some details differ, though, and the color conversion tools in colorspace are well-embedded into the remaining functionality of the package and make it self-contained.
For working with these color spaces various utilities are available:
as()method: Conversions of a
colorobject to the various color spaces, e.g.,
coords(): Extract the three-dimensional coordinates pertaining to the current color class.
hex(): Convert a color object to sRGB and code in a hex string that can be used within R plotting functions.
hex2RGB(): Convert a given hex color string to an sRGB color object which can also be coerced to other color spaces.
readhex()can read text files into color objects, either from RGB coordinates or hex color strings.
writehex(): Writes hex color strings to a text file.
whitepoint(): Query and change the white point employed in conversions from CIE XYZ to RGB. Defaults to D65.
As an example a vector of colors
x can be specified in the HCL (or polar LUV) model:
## L C H ## [1,] 70 50 0 ## [2,] 70 50 120 ## [3,] 70 50 240
The resulting three colors are pastel red (hue = 0), green (hue = 120), and blue (hue = 240) with moderate chroma and luminance. For display in other systems an sRGB representation might be needed:
## R G B ## [1,] 0.8931564 0.5853740 0.6465459 ## [2,] 0.5266113 0.7224335 0.4590469 ## [3,] 0.4907804 0.6911937 0.8673877
## H S V ## [1,] 348.0750 0.3446008 0.8931564 ## [2,] 104.6087 0.3645825 0.7224335 ## [3,] 208.0707 0.4341857 0.8673877
For display in many systems (including R itself) hex color codes based on the sRGB coordinates can be created:
##  "#E495A5" "#86B875" "#7DB0DD"
Ihaka, Ross. 2003. “Colour for Presentation Graphics.” In Proceedings of the 3rd International Workshop on Distributed Statistical Computing, Vienna, Austria, edited by Kurt Hornik, Friedrich Leisch, and Achim Zeileis. http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Proceedings/.
Pedersen, Thomas Lin, Berendea Nicolae, and Romain François. 2018. Farver: Vectorised Colour Conversion and Comparison. https://CRAN.R-project.org/package=farver.
R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wikipedia. 2019a. “CIE 1931 Color Space — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/CIE_1931_color_space, accessed 2019-01-05.
———. 2019b. “CIELAB Color Space — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/CIELAB_color_space, accessed 2019-01-05.
———. 2019c. “CIELUV — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/CIELUV, accessed 2019-01-05.
———. 2019d. “Color Space — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/Color_space, accessed 2019-03-11.
———. 2019e. “HSL and HSV — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/HSL_and_HSV, accessed 2019-01-05.
———. 2019f. “RGB Color Space — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/RGB_color_space, accessed 2019-01-05.
———. 2019g. “sRGB — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/sRGB, accessed 2019-01-05.
———. 2019h. “Web Colors — Wikipedia, the Free Encyclopedia.” URL https://en.wikipedia.org/wiki/Web_colors, accessed 2019-03-11.