The R Project SVN R

Rev

Rev 68948 | Rev 85908 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68948 Rev 75868
Line 13... Line 13...
13
}
13
}
14
\usage{
14
\usage{
15
make.rgb(red, green, blue, name = NULL, white = "D65",
15
make.rgb(red, green, blue, name = NULL, white = "D65",
16
         gamma = 2.2)
16
         gamma = 2.2)
17
 
17
 
18
colorConverter(toXYZ, fromXYZ, name, white = NULL)
18
colorConverter(toXYZ, fromXYZ, name, white = NULL, vectorized = FALSE)
19
}
19
}
20
\arguments{
20
\arguments{
21
  \item{red,green,blue}{Chromaticity (xy or xyY) of RGB primaries}
21
  \item{red,green,blue}{Chromaticity (xy or xyY) of RGB primaries}
22
  \item{name}{Name for the colour space}
22
  \item{name}{Name for the colour space}
23
  \item{white}{Character string specifying the reference white (see
23
  \item{white}{Character string specifying the reference white (see
Line 26... Line 26...
26
    string \code{"sRGB"} }
26
    string \code{"sRGB"} }
27
  \item{fromXYZ}{Function to convert from XYZ tristimulus coordinates
27
  \item{fromXYZ}{Function to convert from XYZ tristimulus coordinates
28
    to this space}
28
    to this space}
29
  \item{toXYZ}{Function to convert from this space to XYZ tristimulus
29
  \item{toXYZ}{Function to convert from this space to XYZ tristimulus
30
    coordinates.}
30
    coordinates.}
-
 
31
  \item{vectorized}{Whether \code{fromXYZ} and \code{toXYZ} are
-
 
32
    vectorized internally to handle input color matrices.}
31
}
33
}
32
\details{
34
\details{
33
  An RGB colour space is defined by the chromaticities of the  red, green and
35
  An RGB colour space is defined by the chromaticities of the  red, green and
34
  blue primaries.  These are given as vectors of length 2 or 3 in xyY
36
  blue primaries.  These are given as vectors of length 2 or 3 in xyY
35
  coordinates (the Y component is not used and may be omitted).
37
  coordinates (the Y component is not used and may be omitted).
Line 46... Line 48...
46
  The functions  should take two arguments. The first is a vector giving the
48
  The functions  should take two arguments. The first is a vector giving the
47
  coordinates for one colour. The second argument is the reference
49
  coordinates for one colour. The second argument is the reference
48
  white. If a specific reference white is included in the definition of
50
  white. If a specific reference white is included in the definition of
49
  the colour space (as for the RGB spaces) this second argument should
51
  the colour space (as for the RGB spaces) this second argument should
50
  be ignored and may be \code{...}.
52
  be ignored and may be \code{...}.
-
 
53
 
-
 
54
  As of R 3.6.0 the built in color converters along with
-
 
55
  \code{\link{convertColor}} were vectorized to process three column
-
 
56
  color matrices in one call, instead of row by row via \code{\link{apply}}.
-
 
57
  In order to maintain backwards compatibility, \code{colorConverter} wraps
-
 
58
  \code{fromXYZ} and \code{toXYZ} in a \code{apply} loop in case they do not
-
 
59
  also support matrix inputs.  If the \code{fromXYZ} and \code{toXYZ} functions
-
 
60
  you are using operate correctly on the whole color matrix at once instead of
-
 
61
  row by row, you can set \code{vectorized=TRUE} for a performance improvement.
51
}
62
}
52
\value{
63
\value{
53
  An object of class \code{colorConverter}
64
  An object of class \code{colorConverter}
54
}
65
}
55
\references{
66
\references{
Line 68... Line 79...
68
## converter for sRGB in #rrggbb format
79
## converter for sRGB in #rrggbb format
69
hexcolor <- colorConverter(toXYZ = function(hex, ...) {
80
hexcolor <- colorConverter(toXYZ = function(hex, ...) {
70
                            rgb <- t(col2rgb(hex))/255
81
                            rgb <- t(col2rgb(hex))/255
71
                            colorspaces$sRGB$toXYZ(rgb, ...) },
82
                            colorspaces$sRGB$toXYZ(rgb, ...) },
72
                           fromXYZ = function(xyz, ...) {
83
                           fromXYZ = function(xyz, ...) {
73
                              rgb <- colorspaces$sRGB$fromXYZ(xyz, ..)
84
                              rgb <- colorspaces$sRGB$fromXYZ(xyz, ...)
74
                              rgb <- round(rgb, 5)
85
                              rgb <- round(rgb, 5)
75
                              if (min(rgb) < 0 || max(rgb) > 1)
86
                              if (min(rgb) < 0 || max(rgb) > 1)
76
                                   as.character(NA)
87
                                   as.character(NA)
77
                              else rgb(rgb[1], rgb[2], rgb[3])},
88
                              else rgb(rgb[1], rgb[2], rgb[3])},
78
                           white = "D65", name = "#rrggbb")
89
                           white = "D65", name = "#rrggbb")
Line 83... Line 94...
83
 
94
 
84
## must make hex a matrix before using it
95
## must make hex a matrix before using it
85
(cc <- round(convertColor(as.matrix(hex), from = hexcolor, to = "sRGB",
96
(cc <- round(convertColor(as.matrix(hex), from = hexcolor, to = "sRGB",
86
                          scale.in = NULL, scale.out = 255)))
97
                          scale.in = NULL, scale.out = 255)))
87
stopifnot(cc == cols)
98
stopifnot(cc == cols)
-
 
99
 
-
 
100
## Internally vectorized version of hexcolor, notice the use
-
 
101
## of `vectorized = TRUE`:
-
 
102
 
-
 
103
hexcolorv <- colorConverter(toXYZ = function(hex, ...) {
-
 
104
                            rgb <- t(col2rgb(hex))/255
-
 
105
                            colorspaces$sRGB$toXYZ(rgb, ...) },
-
 
106
                           fromXYZ = function(xyz, ...) {
-
 
107
                              rgb <- colorspaces$sRGB$fromXYZ(xyz, ...)
-
 
108
                              rgb <- round(rgb, 5)
-
 
109
                              oob <- pmin(rgb[,1],rgb[,2],rgb[,3]) < 0 |
-
 
110
                                     pmax(rgb[,1],rgb[,2],rgb[,3]) > 0
-
 
111
                              res <- rep(NA_character_, nrow(rgb))
-
 
112
                              res[!oob] <- rgb(rgb[!oob,,drop=FALSE])},
-
 
113
                           white = "D65", name = "#rrggbb",
-
 
114
                           vectorized=TRUE)
-
 
115
(ccv <- round(convertColor(as.matrix(hex), from = hexcolor, to = "sRGB",
-
 
116
                           scale.in = NULL, scale.out = 255)))
-
 
117
stopifnot(ccv == cols)
-
 
118
 
88
}
119
}
89
\keyword{color}
120
\keyword{color}
90
 
121