These functions specify colour spaces for use in
convertColor.
Usage
make.rgb(red, green, blue, name = NULL, white = "D65",
gamma = 2.2)
colorConverter(toXYZ, fromXYZ, name, white = NULL)
Arguments
red,green,blue
Chromaticity (xy or xyY) of RGB primaries
name
Name for the colour space
white
Character string specifying the reference white (see
‘Details’.)
gamma
Display gamma (nonlinearity). A positive number or the
string "sRGB"
fromXYZ
Function to convert from XYZ tristimulus coordinates
to this space
toXYZ
Function to convert from this space to XYZ tristimulus
coordinates.
Details
An RGB colour space is defined by the chromaticities of the red, green and
blue primaries. These are given as vectors of length 2 or 3 in xyY
coordinates (the Y component is not used and may be omitted).
The chromaticities are defined relative to a reference white, which
must be one of the CIE standard illuminants: "A", "B", "C",
"D50", "D55", "D60", "E" (usually "D65").
The display gamma is most commonly 2.2, though 1.8 is used for Apple RGB.
The sRGB standard specifies a more complicated function that is close
to a gamma of 2.2; gamma = "sRGB" uses this function.
Colour spaces other than RGB can be specified directly by giving
conversions to and from XYZ tristimulus coordinates.
The functions should take two arguments. The first is a vector giving the
coordinates for one colour. The second argument is the reference
white. If a specific reference white is included in the definition of
the colour space (as for the RGB spaces) this second argument should
be ignored and may be ....
(pal <- make.rgb(red = c(0.6400, 0.3300),
green = c(0.2900, 0.6000),
blue = c(0.1500, 0.0600),
name = "PAL/SECAM RGB"))
## converter for sRGB in #rrggbb format
hexcolor <- colorConverter(toXYZ = function(hex, ...) {
rgb <- t(col2rgb(hex))/255
colorspaces$sRGB$toXYZ(rgb, ...) },
fromXYZ = function(xyz, ...) {
rgb <- colorspaces$sRGB$fromXYZ(xyz, ..)
rgb <- round(rgb, 5)
if (min(rgb) < 0 || max(rgb) > 1)
as.character(NA)
else rgb(rgb[1], rgb[2], rgb[3])},
white = "D65", name = "#rrggbb")
(cols <- t(col2rgb(palette())))
zapsmall(luv <- convertColor(cols, from = "sRGB", to = "Luv", scale.in = 255))
(hex <- convertColor(luv, from = "Luv", to = hexcolor, scale.out = NULL))
## must make hex a matrix before using it
(cc <- round(convertColor(as.matrix(hex), from = hexcolor, to = "sRGB",
scale.in = NULL, scale.out = 255)))
stopifnot(cc == cols)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(grDevices)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/grDevices/make.rgb.Rd_%03d_medium.png", width=480, height=480)
> ### Name: make.rgb
> ### Title: Create colour spaces
> ### Aliases: make.rgb colorConverter
> ### Keywords: color
>
> ### ** Examples
>
> (pal <- make.rgb(red = c(0.6400, 0.3300),
+ green = c(0.2900, 0.6000),
+ blue = c(0.1500, 0.0600),
+ name = "PAL/SECAM RGB"))
Color space converter: PAL/SECAM RGB
Reference white: D65
display gamma = 2.2
>
> ## converter for sRGB in #rrggbb format
> hexcolor <- colorConverter(toXYZ = function(hex, ...) {
+ rgb <- t(col2rgb(hex))/255
+ colorspaces$sRGB$toXYZ(rgb, ...) },
+ fromXYZ = function(xyz, ...) {
+ rgb <- colorspaces$sRGB$fromXYZ(xyz, ..)
+ rgb <- round(rgb, 5)
+ if (min(rgb) < 0 || max(rgb) > 1)
+ as.character(NA)
+ else rgb(rgb[1], rgb[2], rgb[3])},
+ white = "D65", name = "#rrggbb")
>
> (cols <- t(col2rgb(palette())))
red green blue
[1,] 0 0 0
[2,] 255 0 0
[3,] 0 205 0
[4,] 0 0 255
[5,] 0 255 255
[6,] 255 0 255
[7,] 255 255 0
[8,] 190 190 190
> zapsmall(luv <- convertColor(cols, from = "sRGB", to = "Luv", scale.in = 255))
L u v
[1,] 0.00000 0.00000 0.00000
[2,] 53.48418 175.36468 37.80017
[3,] 71.91841 -68.70661 87.86408
[4,] 32.24075 -9.66060 -130.19297
[5,] 91.01106 -71.17303 -15.34147
[6,] 60.50231 84.56678 -108.14654
[7,] 97.14950 7.89815 106.44098
[8,] 76.97594 0.00000 0.00000
> (hex <- convertColor(luv, from = "Luv", to = hexcolor, scale.out = NULL))
[1] "#000000" "#FF0000" "#00CD00" "#0000FF" "#00FFFF" "#FF00FF" "#FFFF00"
[8] "#BEBEBE"
>
> ## must make hex a matrix before using it
> (cc <- round(convertColor(as.matrix(hex), from = hexcolor, to = "sRGB",
+ scale.in = NULL, scale.out = 255)))
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 255 0 0
[3,] 0 205 0
[4,] 0 0 255
[5,] 0 255 255
[6,] 255 0 255
[7,] 255 255 0
[8,] 190 190 190
> stopifnot(cc == cols)
>
>
>
>
>
> dev.off()
null device
1
>