| 1 |
## Copyright 2005, Ross Ihaka. All Rights Reserved. |
| 2 |
## |
| 3 |
## Redistribution and use in source and binary forms, with or without |
| 4 |
## modification, are permitted provided that the following conditions |
| 5 |
## are met: |
| 6 |
## |
| 7 |
## 1. Redistributions of source code must retain the above copyright notice, |
| 8 |
## this list of conditions and the following disclaimer. |
| 9 |
## |
| 10 |
## 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
## notice, this list of conditions and the following disclaimer in the |
| 12 |
## documentation and/or other materials provided with the distribution. |
| 13 |
## |
| 14 |
## 3. The name of the Ross Ihaka may not be used to endorse or promote |
| 15 |
## products derived from this software without specific prior written |
| 16 |
## permission. |
| 17 |
## |
| 18 |
## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
| 19 |
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 |
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ROSS IHAKA BE LIABLE FOR |
| 22 |
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 27 |
## IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
## POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
## ---------------------------------------------------------------------------- |
| 31 |
|
| 32 |
## An S4 Color Space Class |
| 33 |
## |
| 34 |
## The following color spaces are available: |
| 35 |
## |
| 36 |
## RGB linearized sRGB space |
| 37 |
## sRGB Gamma-corrected sRGB space |
| 38 |
## XYZ CIE-XYZ space |
| 39 |
## LAB CIE-L*a*b* space |
| 40 |
## polarLAB CIE-L*a*b* space in polar coordinates |
| 41 |
## HSV Hue-Saturation-Value |
| 42 |
## LUV CIE-L*u*v* |
| 43 |
## polarLUV CIE-L*u*v* in polar coordinates (HCL) |
| 44 |
## HLS Hue-Lightness-Saturation |
| 45 |
## |
| 46 |
## The ``canonical'' space here is really CIE-XYZ, but in this |
| 47 |
## implementation all spaces are treated equally, because |
| 48 |
## they are all useful. |
| 49 |
## |
| 50 |
|
| 51 |
## ---------------------------------------------------------------------------- |
| 52 |
|
| 53 |
#' Class "color" |
| 54 |
#' |
| 55 |
#' Objects from the class \emph{color} represent colors in a number of color |
| 56 |
#' spaces. In particular, there are subclasses of color which correspond to |
| 57 |
#' RGB, HSV, HLS, CIEXYZ, CIELUV, CIELAB and polar versions of the last two |
| 58 |
#' spaces. |
| 59 |
#' |
| 60 |
#' |
| 61 |
#' @name color-class |
| 62 |
#' @aliases color-class RGB-class sRGB-class XYZ-class HSV-class HLS-class |
| 63 |
#' LAB-class LUV-class polarLAB-class polarLUV-class [,color-method |
| 64 |
#' coerce,color,RGB-method coerce,color,sRGB-method coerce,color,XYZ-method |
| 65 |
#' coerce,color,LAB-method coerce,color,polarLAB-method coerce,color,HSV-method |
| 66 |
#' coerce,color,HLS-method coerce,color,LUV-method coerce,color,polarLUV-method |
| 67 |
#' coords,color-method plot,color-method show,color-method |
| 68 |
#' @docType class |
| 69 |
#' @section Objects from the Class: Objects can be created by calls to the |
| 70 |
#' functions \code{RGB}, \code{sRGB}, \code{HSV}, \code{HLS}, \code{XYZ}, |
| 71 |
#' \code{LUV}, \code{LAB}, \code{polarLUV}, and \code{polarLAB}. These are all |
| 72 |
#' subclasses of the virtual class \emph{color}. |
| 73 |
#' @section Slots: \describe{ |
| 74 |
#' \item{\code{coords}:}{An object of class \code{"matrix"}.} |
| 75 |
#' } |
| 76 |
#' @section Methods: \describe{ |
| 77 |
#' \item{[}{\code{signature(x = "color")}: This method makes it possible to |
| 78 |
#' take subsets of a vector of colors.} |
| 79 |
#' \item{coerce}{\code{signature(from = "color", to = "RGB")}: convert |
| 80 |
#' a color vector to RGB.} |
| 81 |
#' \item{coerce}{\code{signature(from = "color", to = "sRGB")}: convert |
| 82 |
#' a color vector to sRGB.} |
| 83 |
#' \item{coerce}{\code{signature(from = "color", to = "XYZ")}: convert |
| 84 |
#' a color vector to XYZ.} |
| 85 |
#' \item{coerce}{\code{signature(from = "color", to = "LAB")}: convert |
| 86 |
#' a color vector to LAB. } |
| 87 |
#' \item{coerce}{\code{signature(from = "color", to = "polarLAB")}: convert |
| 88 |
#' a color vector to polarLAB. } |
| 89 |
#' \item{coerce}{\code{signature(from = "color", to = "HSV")}: convert |
| 90 |
#' a color vector to HSV. } |
| 91 |
#' \item{coerce}{\code{signature(from = "color", to = "HLS")}: convert |
| 92 |
#' a color vector to HLS. } |
| 93 |
#' \item{coerce}{\code{signature(from = "color", to = "LUV")}: convert |
| 94 |
#' a color vector to LUV. } |
| 95 |
#' \item{coerce}{\code{signature(from = "color", to = "polarLUV")}: convert |
| 96 |
#' a color vector to polarLUV. } |
| 97 |
#' \item{coords}{\code{signature(color = "color")}: extract the color |
| 98 |
#' coordinates from a color vector.} |
| 99 |
#' \item{plot}{\code{signature(x = "color")}: plot a color vector } |
| 100 |
#' \item{show}{\code{signature(object = "color")}: show a color vector. } |
| 101 |
#' } |
| 102 |
#' @author Ross Ihaka |
| 103 |
#' @seealso \code{\link{RGB}}, \code{\link{XYZ}}, \code{\link{HSV}}, |
| 104 |
#' \code{\link{HLS}}, \code{\link{LAB}}, \code{\link{polarLAB}}, |
| 105 |
#' \code{\link{LUV}}, \code{\link{polarLUV}}, \code{\link{mixcolor}}. |
| 106 |
#' @keywords classes |
| 107 |
#' @examples |
| 108 |
#' x <- sRGB(runif(1000), runif(1000), runif(1000)) |
| 109 |
#' plot(as(x, "LUV")) |
| 110 |
#' @useDynLib colorspace, .registration = TRUE |
| 111 |
#' @import methods |
| 112 |
|
| 113 |
#' @export |
| 114 |
setClass("color", representation = list(coords = "matrix")) |
| 115 |
|
| 116 |
#' @export |
| 117 |
setClass("RGB", contains = "color") |
| 118 |
|
| 119 |
#' @export |
| 120 |
setClass("sRGB", contains = "color") |
| 121 |
|
| 122 |
#' @export |
| 123 |
setClass("XYZ", contains = "color") |
| 124 |
|
| 125 |
#' @export |
| 126 |
setClass("LAB", contains = "color") |
| 127 |
|
| 128 |
#' @export |
| 129 |
setClass("polarLAB", contains = "color") |
| 130 |
|
| 131 |
#' @export |
| 132 |
setClass("HSV", contains = "color") |
| 133 |
|
| 134 |
#' @export |
| 135 |
setClass("HLS", contains = "color") |
| 136 |
|
| 137 |
#' @export |
| 138 |
setClass("LUV", contains = "color") |
| 139 |
|
| 140 |
#' @export |
| 141 |
setClass("polarLUV", contains = "color") |
| 142 |
|
| 143 |
|
| 144 |
## ---------------------------------------------------------------------------- |
| 145 |
|
| 146 |
#' Extract the Numerical Coordinates of a Color |
| 147 |
#' |
| 148 |
#' This function returns a matrix with three columns which give the coordinates |
| 149 |
#' of a color in its natural color space. |
| 150 |
#' |
| 151 |
#' |
| 152 |
#' @param color A color. |
| 153 |
#' @return A numeric matrix giving the coordinates of the color. |
| 154 |
#' @author Ross Ihaka |
| 155 |
#' @seealso \code{\link{RGB}}, \code{\link{XYZ}}, \code{\link{LAB}}, |
| 156 |
#' \code{\link{polarLAB}}, \code{\link{LUV}}, \code{\link{polarLUV}}, |
| 157 |
#' \code{\link{mixcolor}}. |
| 158 |
#' @keywords color |
| 159 |
#' @examples |
| 160 |
#' x <- sRGB(1, 0, 0) |
| 161 |
#' coords(as(x, "HSV")) |
| 162 |
#' @export coords |
| 163 |
#' @importFrom graphics pairs |
| 164 |
setGeneric("coords", function(color) standardGeneric("coords")) |
| 165 |
|
| 166 |
#' @export |
| 167 |
setMethod("coords", "color", function(color) {color@coords}) |
| 168 |
|
| 169 |
#' @export |
| 170 |
setMethod("show", "color", function(object) show(coords(object))) |
| 171 |
|
| 172 |
#' @export |
| 173 |
setMethod("[", "color", |
| 174 |
function(x, i, j, drop=FALSE) { |
| 175 |
do.call(class(x), list(coords(x)[i,,drop=FALSE])) |
| 176 |
}) |
| 177 |
|
| 178 |
#' @export |
| 179 |
setMethod("plot", signature("color"), |
| 180 |
function(x, y, pch=20, cex=3) |
| 181 |
pairs(coords(x), col=hex(x,fixup=TRUE), pch=pch, cex=cex)) |
| 182 |
|
| 183 |
|
| 184 |
## ---------------------------------------------------------------------------- |
| 185 |
|
| 186 |
## Auxiliary functions |
| 187 |
CheckMatrix <- |
| 188 |
function(x) |
| 189 |
if (!is.double(x) || length(x) < 1 |
| 190 |
|| length(dim(x)) != 2 || dim(x)[2] != 3) |
| 191 |
stop("invalid color matrix") |
| 192 |
|
| 193 |
CheckBounds <- |
| 194 |
function(x, lower, upper) |
| 195 |
if (any(x < lower | x > upper, na.rm=TRUE)) |
| 196 |
warning("out of gammut color") |
| 197 |
|
| 198 |
|
| 199 |
## ---------------------------------------------------------------------------- |
| 200 |
|
| 201 |
#' Create RGB Colors |
| 202 |
#' |
| 203 |
#' This function creates colors of class RGB; a subclass of the virtual |
| 204 |
#' \code{\link{color-class}} class. |
| 205 |
#' |
| 206 |
#' This function creates colors in the linearized sRGB color space (IEC |
| 207 |
#' standard 61966). |
| 208 |
#' |
| 209 |
#' @param R,G,B these arguments give the red, green and blue intensities of the |
| 210 |
#' colors (the values should lie between 0 and 1). The values can be provided |
| 211 |
#' in separate \code{R}, \code{G} and \code{B} vectors or in a three-column |
| 212 |
#' matrix passed as \code{R}. |
| 213 |
#' @param names A vector of names for the colors (by default the row names of |
| 214 |
#' \code{R} are used). |
| 215 |
#' @return An object of class \code{RGB} which inherits from class \code{color}. |
| 216 |
#' @author Ross Ihaka |
| 217 |
#' @seealso \code{\link{sRGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 218 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{LUV}}, |
| 219 |
#' \code{\link{polarLUV}}. |
| 220 |
#' @keywords color |
| 221 |
#' @examples |
| 222 |
#' # Create a random set of colors |
| 223 |
#' set.seed(1) |
| 224 |
#' RGB(R = runif(20), G = runif(20), B = runif(20)) |
| 225 |
#' @export RGB |
| 226 |
RGB <- |
| 227 |
function(R, G, B, names) |
| 228 |
{ |
| 229 |
if (missing(R)) return(new("RGB")) |
| 230 |
if (missing(names)) names = dimnames(R)[[1]] |
| 231 |
if (is.integer(R)) R[] <- as.numeric(R) |
| 232 |
coords = cbind(R, if (missing(G)) NULL else G, |
| 233 |
if (missing(B)) NULL else B) |
| 234 |
CheckMatrix(coords) |
| 235 |
dimnames(coords) = list(names, c("R", "G", "B")) |
| 236 |
new("RGB", coords = coords) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#' Create sRGB Colors |
| 242 |
#' |
| 243 |
#' This function creates colors of class sRGB; a subclass of the virtual |
| 244 |
#' \code{\link{color-class}} class. |
| 245 |
#' |
| 246 |
#' This function creates colors in the standard sRGB color space (IEC standard |
| 247 |
#' 61966). |
| 248 |
#' |
| 249 |
#' @param R,G,B these arguments give the red, green and blue intensities of the |
| 250 |
#' colors (the values should lie between 0 and 1). The values can be provided |
| 251 |
#' in separate \code{R}, \code{G} and \code{B} vectors or in a three-column |
| 252 |
#' matrix passed as \code{R}. |
| 253 |
#' @param names A vector of names for the colors (by default the row names of |
| 254 |
#' \code{R} are used). |
| 255 |
#' @return An object of class \code{sRGB} which inherits from class \code{color}. |
| 256 |
#' @author Ross Ihaka |
| 257 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 258 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{LUV}}, |
| 259 |
#' \code{\link{polarLUV}}. |
| 260 |
#' @keywords color |
| 261 |
#' @examples |
| 262 |
#' # Create a random set of colors |
| 263 |
#' set.seed(1) |
| 264 |
#' sRGB(R = runif(20), G = runif(20), B = runif(20)) |
| 265 |
#' @export sRGB |
| 266 |
sRGB <- |
| 267 |
function(R, G, B, names) |
| 268 |
{ |
| 269 |
if (missing(R)) return(new("sRGB")) |
| 270 |
if (missing(names)) names = dimnames(R)[[1]] |
| 271 |
if (is.integer(R)) R[] <- as.numeric(R) |
| 272 |
coords = cbind(R, if (missing(G)) NULL else G, |
| 273 |
if (missing(B)) NULL else B) |
| 274 |
CheckMatrix(coords) |
| 275 |
dimnames(coords) = list(names, c("R", "G", "B")) |
| 276 |
new("sRGB", coords = coords) |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
#' Create XYZ Colors |
| 282 |
#' |
| 283 |
#' This function creates colors of class XYZ; a subclass of the virtual |
| 284 |
#' \code{\link{color-class}} class. |
| 285 |
#' |
| 286 |
#' The X, Y and Z values are the levels of the CIE primaries. These are scaled |
| 287 |
#' so that the luminance of the display white-point is 100. The white-point is |
| 288 |
#' taken to be D65, which means that its coordinates are 95.047, 100.000, |
| 289 |
#' 108.883. |
| 290 |
#' |
| 291 |
#' @param X,Y,Z these arguments give the X, Y and Z coordinates of the colors. |
| 292 |
#' The values can be provided in separate \code{X}, \code{Y} and \code{Z} |
| 293 |
#' vectors or in a three-column matrix passed as \code{X}. |
| 294 |
#' @param names A vector of names for the colors (by default the row names of |
| 295 |
#' \code{X} are used). |
| 296 |
#' @return An object of class \code{XYZ} which inherits from class \code{color}. |
| 297 |
#' @author Ross Ihaka |
| 298 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{LAB}}, |
| 299 |
#' \code{\link{polarLAB}}, \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 300 |
#' @keywords color |
| 301 |
#' @examples |
| 302 |
#' ## Generate white in XYZ space |
| 303 |
#' XYZ(95.047, 100.000, 108.883) |
| 304 |
#' @export XYZ |
| 305 |
XYZ <- |
| 306 |
function(X, Y, Z, names) |
| 307 |
{ |
| 308 |
if (missing(X)) return(new("XYZ")) |
| 309 |
if (missing(names)) names = dimnames(X)[[1]] |
| 310 |
if (is.integer(X)) X[] <- as.numeric(X) |
| 311 |
coords = cbind(X, if (missing(Y)) NULL else Y, |
| 312 |
if (missing(Z)) NULL else Z) |
| 313 |
CheckMatrix(coords) |
| 314 |
## CheckBounds(coords, 0, Inf) |
| 315 |
dimnames(coords) = list(names, c("X", "Y", "Z")) |
| 316 |
new("XYZ", coords = coords) |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
#' Create LAB Colors |
| 322 |
#' |
| 323 |
#' This function creates colors of class ``LAB''; a subclass of the virtual |
| 324 |
#' \code{\link{color-class}} class. |
| 325 |
#' |
| 326 |
#' The \code{L}, \code{A} and \code{B} values give the coordinates of the |
| 327 |
#' colors in the CIE \eqn{L^*a^*b^*}{L*a*b*} space. This is a transformation |
| 328 |
#' of the 1931 CIE XYZ space which attempts to produce perceptually based axes. |
| 329 |
#' Luminance takes values between 0 and 100, and the other coordinates |
| 330 |
#' typically take values between -100 and 100, although these values can also |
| 331 |
#' be exceeded by highly saturated colors. The \eqn{a} and \eqn{b} coordinates |
| 332 |
#' measure positions on green/red and blue/yellow axes. |
| 333 |
#' |
| 334 |
#' @param L,A,B these arguments give the L, A and B coordinates of the colors. |
| 335 |
#' The values can be provided in separate \code{L}, \code{A} and \code{B} |
| 336 |
#' vectors or in a three-column matrix passed as \code{L}. |
| 337 |
#' @param names a vector of names for the colors (by default the row names of |
| 338 |
#' \code{L} are used). |
| 339 |
#' @return An object of class \code{LAB} which inherits from class \code{color}. |
| 340 |
#' @author Ross Ihaka |
| 341 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 342 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{LUV}}, |
| 343 |
#' \code{\link{polarLUV}}. |
| 344 |
#' @keywords color |
| 345 |
#' @examples |
| 346 |
#' ## Show the LAB space |
| 347 |
#' set.seed(1) |
| 348 |
#' x <- sRGB(runif(1000), runif(1000), runif(1000)) |
| 349 |
#' y <- as(x, "LAB") |
| 350 |
#' head(x) |
| 351 |
#' head(y) |
| 352 |
#' plot(y) |
| 353 |
#' @export LAB |
| 354 |
LAB <- |
| 355 |
function(L, A, B, names) |
| 356 |
{ |
| 357 |
if (missing(L)) return(new("LAB")) |
| 358 |
if (missing(names)) names = dimnames(L)[[1]] |
| 359 |
if (is.integer(L)) L[] <- as.numeric(L) |
| 360 |
coords = cbind(L, if (missing(A)) NULL else A, |
| 361 |
if (missing(B)) NULL else B) |
| 362 |
CheckMatrix(coords) |
| 363 |
## CheckBounds(coords[,1], 0, 100) |
| 364 |
## CheckBounds(coords[,2], -500, 500) |
| 365 |
## CheckBounds(coords[,3], -200, 200) |
| 366 |
dimnames(coords) = list(names, c("L", "A", "B")) |
| 367 |
new("LAB", coords = coords) |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
#' Create polarLAB Colors |
| 373 |
#' |
| 374 |
#' This function creates colors of class ``polarLAB''; a subclass of the |
| 375 |
#' virtual \code{\link{color-class}} class. |
| 376 |
#' |
| 377 |
#' The polarLAB space is a transformation of the CIE \eqn{L^*a^*b^*}{L*a*b*} |
| 378 |
#' space so that the \eqn{a} and \eqn{b} values are converted to polar |
| 379 |
#' coordinates. The radial component \eqn{C} measures chroma and the angular |
| 380 |
#' coordinate \eqn{H} is measures hue. |
| 381 |
#' |
| 382 |
#' @param L,C,H these arguments give the L, C and H coordinates of the colors. |
| 383 |
#' The values can be provided in separate \code{L}, \code{C} and \code{H} |
| 384 |
#' vectors or in a three-column matrix passed as \code{L}. |
| 385 |
#' @param names A vector of names for the colors (by default the row names of |
| 386 |
#' \code{L} are used). |
| 387 |
#' @return An object of class \code{polarLAB} which inherits from class \code{color}. |
| 388 |
#' @author Ross Ihaka |
| 389 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 390 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{LUV}}, |
| 391 |
#' \code{\link{polarLUV}}. |
| 392 |
#' @keywords color |
| 393 |
#' @examples |
| 394 |
#' ## Show the polarLAB space |
| 395 |
#' set.seed(1) |
| 396 |
#' x <- sRGB(runif(1000), runif(1000), runif(1000)) |
| 397 |
#' y <- as(x, "polarLAB") |
| 398 |
#' head(x) |
| 399 |
#' head(y) |
| 400 |
#' plot(y) |
| 401 |
#' @export polarLAB |
| 402 |
polarLAB <- |
| 403 |
function(L, C, H, names) |
| 404 |
{ |
| 405 |
if (missing(L)) return(new("polarLAB")) |
| 406 |
if (missing(names)) names = dimnames(L)[[1]] |
| 407 |
if (is.integer(L)) L[] <- as.numeric(L) |
| 408 |
coords = cbind(L, if (missing(C)) NULL else C, |
| 409 |
if (missing(H)) NULL else H) |
| 410 |
CheckMatrix(coords) |
| 411 |
## CheckBounds(coords[,1], 0, Inf) |
| 412 |
## CheckBounds(coords[,2], 0, Inf) |
| 413 |
## CheckBounds(coords[,3], 0, 360) |
| 414 |
dimnames(coords) = list(names, c("L", "C", "H")) |
| 415 |
new("polarLAB", coords = coords) |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
#' Create HSV Colors |
| 421 |
#' |
| 422 |
#' This function creates colors of class HSV; a subclass of the virtual |
| 423 |
#' \code{\link{color-class}} class. |
| 424 |
#' |
| 425 |
#' This function creates colors in an HSV color space. The hues should |
| 426 |
#' lie between between 0 and 360, and the saturations and values should |
| 427 |
#' lie between 0 and 1. |
| 428 |
#' |
| 429 |
#' HSV is a relative color space; it is a transformation of an RGB color |
| 430 |
#' space. Conversion of HSV colors to any other color space must first |
| 431 |
#' involve a conversion to a specific RGB color space, for example the |
| 432 |
#' standard \code{\link{sRGB}} color space (IEC standard 61966). |
| 433 |
#' |
| 434 |
#' @param H,S,V These arguments give the hue, saturation and value of the |
| 435 |
#' colors. The values can be provided in separate \code{H}, \code{S} and |
| 436 |
#' \code{V} vectors or in a three-column matrix passed as \code{H}. |
| 437 |
#' @param names A vector of names for the colors (by default the row names of |
| 438 |
#' \code{H} are used). |
| 439 |
#' @return An object of class \code{HSV} which inherits from class \code{color}. |
| 440 |
#' @author Ross Ihaka |
| 441 |
#' @seealso \code{\link{sRGB}}, \code{\link{RGB}}, \code{\link{XYZ}}, \code{\link{LAB}}, |
| 442 |
#' \code{\link{polarLAB}}, \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 443 |
#' @keywords color |
| 444 |
#' @examples |
| 445 |
#' # A rainbow of full-intensity hues |
| 446 |
#' HSV(seq(0, 360, length.out = 13)[-13], 1, 1) |
| 447 |
#' @export HSV |
| 448 |
HSV <- |
| 449 |
function(H, S, V, names) |
| 450 |
{ |
| 451 |
if (missing(H)) return(new("HSV")) |
| 452 |
if (missing(names)) names = dimnames(H)[[1]] |
| 453 |
if (is.integer(H)) H[] <- as.numeric(H) |
| 454 |
coords = cbind(H, if (missing(S)) NULL else S, |
| 455 |
if (missing(V)) NULL else V) |
| 456 |
CheckMatrix(coords) |
| 457 |
## CheckBounds(coords[,1], 0, 360) |
| 458 |
## CheckBounds(coords[,2], 0, 1) |
| 459 |
## CheckBounds(coords[,3], 0, 1) |
| 460 |
dimnames(coords) = list(names, c("H", "S", "V")) |
| 461 |
new("HSV", coords = coords) |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
#' Create HLS Colors |
| 467 |
#' |
| 468 |
#' This function creates colors of class HLS; a subclass of the virtual |
| 469 |
#' \code{\link{color-class}} class. |
| 470 |
#' |
| 471 |
#' This function creates colors in an HLS color space. The hues should |
| 472 |
#' lie between between 0 and 360, and the lightness and saturations |
| 473 |
#' should lie between 0 and 1. |
| 474 |
#' |
| 475 |
#' HLS is a relative color space; it is a transformation of an RGB color |
| 476 |
#' space. Conversion of HLS colors to any other color space must first |
| 477 |
#' involve a conversion to a specific RGB color space, for example the |
| 478 |
#' standard \code{\link{sRGB}} color space (IEC standard 61966). |
| 479 |
#' |
| 480 |
#' @param H,L,S These arguments give the hue, lightness, and saturation of the |
| 481 |
#' colors. The values can be provided in separate \code{H}, \code{L} and |
| 482 |
#' \code{S} vectors or in a three-column matrix passed as \code{H}. |
| 483 |
#' @param names A vector of names for the colors (by default the row names of |
| 484 |
#' \code{H} are used). |
| 485 |
#' @return An object of class \code{HLS} which inherits from class \code{color}. |
| 486 |
#' @author Ross Ihaka |
| 487 |
#' @seealso \code{\link{sRGB}}, \code{\link{RGB}}, \code{\link{XYZ}}, \code{\link{LAB}}, |
| 488 |
#' \code{\link{polarLAB}}, \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 489 |
#' @keywords color |
| 490 |
#' @examples |
| 491 |
#' # A rainbow of full-intensity hues |
| 492 |
#' HLS(seq(0, 360, length.out = 13)[-13], 0.5, 1) |
| 493 |
#' @export HLS |
| 494 |
HLS <- |
| 495 |
function(H, L, S, names) |
| 496 |
{ |
| 497 |
if (missing(H)) return(new("HLS")) |
| 498 |
if (missing(names)) names = dimnames(H)[[1]] |
| 499 |
if (is.integer(H)) H[] <- as.numeric(H) |
| 500 |
coords = cbind(H, if (missing(L)) NULL else L, |
| 501 |
if (missing(S)) NULL else S) |
| 502 |
CheckMatrix(coords) |
| 503 |
## CheckBounds(coords[,1], 0, 360) |
| 504 |
## CheckBounds(coords[,2], 0, 1) |
| 505 |
## CheckBounds(coords[,3], 0, 1) |
| 506 |
dimnames(coords) = list(names, c("H", "L", "S")) |
| 507 |
new("HLS", coords = coords) |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
#' Create LUV Colors |
| 513 |
#' |
| 514 |
#' This function creates colors of class ``LUV''; a subclass of the virtual |
| 515 |
#' \code{\link{color-class}} class. |
| 516 |
#' |
| 517 |
#' The \code{L}, \code{U} and \code{V} values give the coordinates of the |
| 518 |
#' colors in the CIE (1976) \eqn{L^*u^*v^*}{L*u*v*} space. This is a |
| 519 |
#' transformation of the 1931 CIE XYZ space which attempts to produce |
| 520 |
#' perceptually based axes. Luminance takes values between 0 and 100, and the other coordinates |
| 521 |
#' typically take values between -100 and 100, although these values can also |
| 522 |
#' be exceeded by highly saturated colors. The \eqn{u} and \eqn{v} |
| 523 |
#' coordinates measure positions on green/red and blue/yellow axes. |
| 524 |
#' |
| 525 |
#' @param L,U,V these arguments give the L, U and V coordinates of the colors. |
| 526 |
#' The values can be provided in separate \code{L}, \code{U} and \code{V} |
| 527 |
#' vectors or in a three-column matrix passed as \code{L}. |
| 528 |
#' @param names a vector of names for the colors (by default the row names of |
| 529 |
#' \code{L} are used). |
| 530 |
#' @return An object of class \code{LUV} which inherits from class \code{color}. |
| 531 |
#' @author Ross Ihaka |
| 532 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 533 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{polarLUV}}. |
| 534 |
#' @keywords color |
| 535 |
#' @examples |
| 536 |
#' ## Show the LUV space |
| 537 |
#' set.seed(1) |
| 538 |
#' x <- sRGB(runif(1000), runif(1000), runif(1000)) |
| 539 |
#' y <- as(x, "LUV") |
| 540 |
#' head(x) |
| 541 |
#' head(y) |
| 542 |
#' plot(y) |
| 543 |
#' @export LUV |
| 544 |
LUV <- |
| 545 |
function(L, U, V, names) |
| 546 |
{ |
| 547 |
if (missing(L)) return(new("LUV")) |
| 548 |
if (missing(names)) names = dimnames(L)[[1]] |
| 549 |
if (is.integer(L)) L[] <- as.numeric(L) |
| 550 |
coords = cbind(L, if (missing(U)) NULL else U, |
| 551 |
if (missing(V)) NULL else V) |
| 552 |
## CheckBounds(coords[,1], 0, 100) |
| 553 |
## CheckBounds(coords[,2], -Inf, Inf) |
| 554 |
## CheckBounds(coords[,3], -Inf, Inf) |
| 555 |
dimnames(coords) = list(names, c("L", "U", "V")) |
| 556 |
new("LUV", coords = coords) |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
#' Create polarLUV (HCL) Colors |
| 562 |
#' |
| 563 |
#' This function creates colors of class ``polarLUV''; a subclass of the |
| 564 |
#' virtual \code{\link{color-class}} class. |
| 565 |
#' |
| 566 |
#' The polarLUV space is a transformation of the CIE \eqn{L^*u^*v^*}{L*u*v*} |
| 567 |
#' space so that the \eqn{u} and \eqn{v} values are converted to polar |
| 568 |
#' coordinates. The radial component \eqn{C} measures chroma and the angular |
| 569 |
#' coordinate \eqn{H} is measures hue. It is also known as the HCL |
| 570 |
#' (hue-chroma-luminance) space. |
| 571 |
#' |
| 572 |
#' @aliases polarLUV HCL |
| 573 |
#' @param L,C,H these arguments give the L, C and H coordinates of the colors. |
| 574 |
#' The values can be provided in separate \code{L}, \code{C} and \code{H} |
| 575 |
#' vectors or in a three-column matrix passed as \code{L}. |
| 576 |
#' @param names A vector of names for the colors (by default the row names of |
| 577 |
#' \code{L} are used). |
| 578 |
#' @return An object of class \code{polarLUV} which inherits from class \code{color}. |
| 579 |
#' @author Ross Ihaka |
| 580 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 581 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{LUV}}, |
| 582 |
#' \code{\link{polarLUV}}. |
| 583 |
#' @keywords color |
| 584 |
#' @examples |
| 585 |
#' ## Show the polarLUV space |
| 586 |
#' set.seed(1) |
| 587 |
#' x <- sRGB(runif(1000), runif(1000), runif(1000)) |
| 588 |
#' y <- as(x, "polarLUV") |
| 589 |
#' head(x) |
| 590 |
#' head(y) |
| 591 |
#' plot(y) |
| 592 |
#' @export polarLUV |
| 593 |
polarLUV <- |
| 594 |
function(L, C, H, names) |
| 595 |
{ |
| 596 |
if (missing(L)) return(new("polarLUV")) |
| 597 |
if (missing(names)) names = dimnames(L)[[1]] |
| 598 |
if (is.integer(L)) L[] <- as.numeric(L) |
| 599 |
coords = cbind(L, if (missing(C)) NULL else C, |
| 600 |
if (missing(H)) NULL else H) |
| 601 |
dimnames(coords) = list(names, c("L", "C", "H")) |
| 602 |
new("polarLUV", coords = coords) |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
## ---------------------------------------------------------------------------- |
| 607 |
|
| 608 |
setAs("color", "RGB", function(from) |
| 609 |
RGB(.Call("as_RGB", from@coords, class(from), .whitepoint$white, |
| 610 |
PACKAGE = "colorspace"), |
| 611 |
names = dimnames(from@coords)[[1]])) |
| 612 |
|
| 613 |
setAs("color", "sRGB", function(from) |
| 614 |
sRGB(.Call("as_sRGB", from@coords, class(from), .whitepoint$white, |
| 615 |
PACKAGE = "colorspace"), |
| 616 |
names = dimnames(from@coords)[[1]])) |
| 617 |
|
| 618 |
setAs("color", "XYZ", function(from) |
| 619 |
XYZ(.Call("as_XYZ", from@coords, class(from), .whitepoint$white, |
| 620 |
PACKAGE = "colorspace"), |
| 621 |
names = dimnames(from@coords)[[1]])) |
| 622 |
|
| 623 |
setAs("color", "LAB", function(from) |
| 624 |
LAB(.Call("as_LAB", from@coords, class(from), .whitepoint$white, |
| 625 |
PACKAGE = "colorspace"), |
| 626 |
names = dimnames(from@coords)[[1]])) |
| 627 |
|
| 628 |
setAs("color", "polarLAB", function(from) |
| 629 |
polarLAB(.Call("as_polarLAB", from@coords, class(from), |
| 630 |
.whitepoint$white, PACKAGE = "colorspace"), |
| 631 |
names = dimnames(from@coords)[[1]])) |
| 632 |
|
| 633 |
setAs("color", "HSV", function(from) |
| 634 |
HSV(.Call("as_HSV", from@coords, class(from), .whitepoint$white, |
| 635 |
PACKAGE = "colorspace"), |
| 636 |
names = dimnames(from@coords)[[1]])) |
| 637 |
|
| 638 |
setAs("color", "HLS", function(from) |
| 639 |
HLS(.Call("as_HLS", from@coords, class(from), .whitepoint$white, |
| 640 |
PACKAGE = "colorspace"), |
| 641 |
names = dimnames(from@coords)[[1]])) |
| 642 |
|
| 643 |
setAs("color", "LUV", function(from) |
| 644 |
LUV(.Call("as_LUV", from@coords, class(from), .whitepoint$white, |
| 645 |
PACKAGE = "colorspace"), |
| 646 |
names = dimnames(from@coords)[[1]])) |
| 647 |
|
| 648 |
setAs("color", "polarLUV", function(from) |
| 649 |
polarLUV(.Call("as_polarLUV", from@coords, class(from), |
| 650 |
.whitepoint$white, PACKAGE = "colorspace"), |
| 651 |
names = dimnames(from@coords)[[1]])) |
| 652 |
|
| 653 |
|
| 654 |
## ---------------------------------------------------------------------------- |
| 655 |
|
| 656 |
#' Convert Colors to Hexadecimal Strings |
| 657 |
#' |
| 658 |
#' This functions converts \code{\link{color-class}} objects into hexadecimal strings. |
| 659 |
#' |
| 660 |
#' The color objects are first converted to sRGB color objects. They are then |
| 661 |
#' multiplied by 255 and rounded to obtain an integer value. These values are |
| 662 |
#' then converted to hexadecimal strings of the form \code{"#RRGGBB"} and |
| 663 |
#' suitable for use as color descriptions for R graphics. Out of gamut values |
| 664 |
#' are either corrected to valid RGB values by translating the the individual |
| 665 |
#' primary values so that they lie between 0 and 255. |
| 666 |
#' |
| 667 |
#' @param from The color object to be converted. |
| 668 |
#' @param gamma Deprecated. |
| 669 |
#' @param fixup Should the color be corrected to a valid RGB value before |
| 670 |
#' correction. The default is to convert out-of-gamut colors to the string |
| 671 |
#' \code{"NA"}. |
| 672 |
#' @return A vector of character strings. |
| 673 |
#' @author Ross Ihaka |
| 674 |
#' @seealso \code{\link{hex2RGB}}, \code{\link{RGB}}, \code{\link{sRGB}}, |
| 675 |
#' \code{\link{HSV}}, \code{\link{XYZ}}, \code{\link{LAB}}, |
| 676 |
#' \code{\link{polarLAB}}, \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 677 |
#' @keywords color |
| 678 |
#' @examples |
| 679 |
#' hsv <- HSV(seq(0, 360, length.out = 7)[-7], 1, 1) |
| 680 |
#' hsv |
| 681 |
#' hex(hsv) |
| 682 |
#' barplot(rep(1,6), col = hex(hsv)) |
| 683 |
#' @export hex |
| 684 |
hex <- |
| 685 |
function(from, gamma = NULL, fixup = FALSE) |
| 686 |
{ |
| 687 |
if (!is.null(gamma)) |
| 688 |
warning("'gamma' is deprecated and has no effect") |
| 689 |
coords <- as(from, "sRGB")@coords |
| 690 |
rval <- .Call("sRGB_to_RColor", coords, fixup, PACKAGE = "colorspace") |
| 691 |
if(!is.null(dnam <- attr(coords, "dimnames"))) names(rval) <- dnam[[1L]] |
| 692 |
return(rval) |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
#' Convert Hexadecimal Color Specifications to sRGB Objects |
| 698 |
#' |
| 699 |
#' This function takes a vector of strings of the form \code{"#RRGGBB"} |
| 700 |
#' (hexadecimal color descriptions) into \code{\link{sRGB}} objects. |
| 701 |
#' |
| 702 |
#' This function converts device-dependent color descriptions of the form |
| 703 |
#' \code{"#RRGGBB"} into sRGB color descriptions (linearized if \code{gamma} is |
| 704 |
#' \code{TRUE}). The alpha channel will be ignored if given |
| 705 |
#' (\code{"#RRGGBBAA"}). |
| 706 |
#' |
| 707 |
#' @param x a vector of hexadecimal color descriptions. |
| 708 |
#' @param gamma Whether to apply gamma-correction. |
| 709 |
#' @return An sRGB object describing the colors. |
| 710 |
#' @author Ross Ihaka |
| 711 |
#' @seealso \code{\link{hex}}, \code{\link{RGB}}, \code{\link{sRGB}}, |
| 712 |
#' \code{\link{HSV}}, \code{\link{XYZ}}, \code{\link{polarLAB}}, |
| 713 |
#' \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 714 |
#' @keywords color |
| 715 |
#' @examples |
| 716 |
#' hex2RGB(c("#FF0000","#00FF00", "#0000FF50")) |
| 717 |
#' @export hex2RGB |
| 718 |
hex2RGB = |
| 719 |
function(x, gamma = FALSE) { |
| 720 |
x <- substr(x,0,7) # Remove alpha |
| 721 |
rval <- if (gamma) |
| 722 |
RGB(.Call("hex_to_RGB", x, gamma, PACKAGE = "colorspace")) |
| 723 |
else |
| 724 |
sRGB(.Call("hex_to_RGB", x, gamma, PACKAGE = "colorspace")) |
| 725 |
if(!is.null(nam <- names(x))) attr(rval@coords, "dimnames")[[1L]] <- nam |
| 726 |
return(rval) |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
#' Read RGB Color Descriptions |
| 733 |
#' |
| 734 |
#' This function reads a set of RGB color descriptions (of the form written by |
| 735 |
#' \code{gcolorsel}) from a file and creates a color object containing the |
| 736 |
#' corresponding colors. |
| 737 |
#' |
| 738 |
#' The file is assumed to contain RGB color descriptions consisting of three |
| 739 |
#' integer values in the range from 0 to 255 followed by a color name. |
| 740 |
#' |
| 741 |
#' @param file The file containing the color descriptions. |
| 742 |
#' @param class The kind of color object to be returned. |
| 743 |
#' @return An color object of the specified class containing the color |
| 744 |
#' descriptions. |
| 745 |
#' @author Ross Ihaka |
| 746 |
#' @seealso \code{\link{writehex}}, \code{\link{readhex}}, |
| 747 |
#' \code{\link{hex2RGB}}, \code{\link{RGB}}, \code{\link{HSV}}, |
| 748 |
#' \code{\link{XYZ}}, \code{\link{LAB}}, \code{\link{polarLAB}}, |
| 749 |
#' \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 750 |
#' @keywords color |
| 751 |
#' @examples |
| 752 |
#' \dontrun{ |
| 753 |
#' rgb <- readRGB("pastel.rgb") |
| 754 |
#' hsv <- readRGB("pastel.rgb", "HSV") |
| 755 |
#' } |
| 756 |
#' @export readRGB |
| 757 |
readRGB <- |
| 758 |
function(file, class="RGB") |
| 759 |
{ |
| 760 |
x = scan(file, what=list(R = 0, G = 0, B = 0, name = "")) |
| 761 |
as(RGB(R = x$R/255, G = x$G/255, B = x$B/255), Class=class) |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
#' Read Hexadecimal Color Descriptions |
| 767 |
#' |
| 768 |
#' This function reads a set of hexadecimal color descriptions from a file and |
| 769 |
#' creates a color object containing the corresponding colors. |
| 770 |
#' |
| 771 |
#' The file is assumed to contain hexadecimal color descriptions of the form |
| 772 |
#' \code{#RRGGBB}. |
| 773 |
#' |
| 774 |
#' @param file The file containing the color descriptions. |
| 775 |
#' @param class The kind of color object to be returned. |
| 776 |
#' @return An color object of the specified class containing the color |
| 777 |
#' descriptions. |
| 778 |
#' @author Ross Ihaka |
| 779 |
#' @seealso \code{\link{writehex}}, \code{\link{readRGB}}, |
| 780 |
#' \code{\link{hex2RGB}}, \code{\link{RGB}}, \code{\link{HSV}}, |
| 781 |
#' \code{\link{XYZ}}, \code{\link{LAB}}, \code{\link{polarLAB}}, |
| 782 |
#' \code{\link{LUV}}, \code{\link{polarLUV}}, |
| 783 |
#' @keywords color |
| 784 |
#' @examples |
| 785 |
#' \dontrun{ |
| 786 |
#' rgb <- readhex("pastel.txt") |
| 787 |
#' hsv <- readhex("pastel.txt", "HSV") |
| 788 |
#' } |
| 789 |
#' @export readhex |
| 790 |
readhex <- |
| 791 |
function(file="", class="RGB") |
| 792 |
as(hex2RGB(scan(file, what = "")), Class=class) |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
#' Write Hexadecimal Color Descriptions |
| 797 |
#' |
| 798 |
#' Given a color object, this function writes a file containing the hexadecimal |
| 799 |
#' representation of the colors in the object. |
| 800 |
#' |
| 801 |
#' This function converts the given color object to RGB and then writes |
| 802 |
#' hexadecimal strings (of the form \code{#RRGGBB}) representing the colors to |
| 803 |
#' the specified file. |
| 804 |
#' |
| 805 |
#' @param x a color object. |
| 806 |
#' @param file the name of the file to be written. |
| 807 |
#' @return The name of the file is returned as the value of the function. |
| 808 |
#' @author Ross Ihaka |
| 809 |
#' @seealso \code{\link{readhex}}, \code{\link{readRGB}}, |
| 810 |
#' \code{\link{hex2RGB}}, \code{\link{RGB}}, \code{\link{HSV}}, |
| 811 |
#' \code{\link{XYZ}}, \code{\link{LAB}}, \code{\link{polarLAB}}, |
| 812 |
#' \code{\link{LUV}}, \code{\link{polarLUV}}. |
| 813 |
#' @keywords color |
| 814 |
#' @examples |
| 815 |
#' set.seed(1) |
| 816 |
#' x <- sRGB(runif(10), runif(10), runif(10)) |
| 817 |
#' ## IGNORE_RDIFF_BEGIN |
| 818 |
#' writehex(x, file.path(tempdir(), "random.txt")) |
| 819 |
#' ## IGNORE_RDIFF_END |
| 820 |
#' @export writehex |
| 821 |
writehex <- |
| 822 |
function(x, file="") |
| 823 |
{ |
| 824 |
cat(hex(x), sep="\n", file=file) |
| 825 |
file |
| 826 |
} |
| 827 |
|
| 828 |
.whitepoint <- new.env() |
| 829 |
#' Access or Modify the Whitepoint |
| 830 |
#' |
| 831 |
#' This function can be used to control the single global whitepoint |
| 832 |
#' that affects all color conversions within the package (that require |
| 833 |
#' a whitepoint, i.e., go through XYZ). |
| 834 |
#' |
| 835 |
#' @aliases whitepoint |
| 836 |
#' @param white,... Either missing (to query the whitepoint) or |
| 837 |
#' \code{NULL} or a specification of the XYZ coordinates of the whitepoint |
| 838 |
#' (to set the whitepoint, see examples). \code{NULL} corresponds to |
| 839 |
#' CIE D65 with XYZ coordinates 95.047, 100.000, 108.883. |
| 840 |
#' @return \code{whitepoint} returns an XYZ color object for the whitepoint |
| 841 |
#' (invisibly in case a new whitepoint was set). |
| 842 |
#' @seealso \code{\link{XYZ}} and \link{color-class}. |
| 843 |
#' @keywords color |
| 844 |
#' @examples |
| 845 |
#' # query current whitepoint (D65 by default) |
| 846 |
#' whitepoint() |
| 847 |
#' |
| 848 |
#' # Illuminant E |
| 849 |
#' whitepoint(XYZ(100, 100, 100)) |
| 850 |
#' |
| 851 |
#' # equivalently |
| 852 |
#' whitepoint(100, 100, 100) |
| 853 |
#' whitepoint(c(100, 100, 100)) |
| 854 |
#' whitepoint(cbind(100, 100, 100)) |
| 855 |
#' |
| 856 |
#' whitepoint() |
| 857 |
#' |
| 858 |
#' ## reset |
| 859 |
#' whitepoint(NULL) |
| 860 |
#' whitepoint() |
| 861 |
#' @export whitepoint |
| 862 |
#' @rdname whitepoint |
| 863 |
whitepoint <- function(white, ...) { |
| 864 |
## get whitepoint |
| 865 |
if (missing(white)) return(XYZ(.whitepoint$white)) |
| 866 |
|
| 867 |
## set whitepoint to default |
| 868 |
if (is.null(white)) { |
| 869 |
white <- cbind(95.047, 100.000, 108.883) |
| 870 |
## construct whitepoint XYZ coordinates from numeric input |
| 871 |
} else if (is.numeric(white)) { |
| 872 |
white <- do.call("cbind", list(white, ...)) |
| 873 |
if (all(dim(white) != 3L)) |
| 874 |
stop("'white' needs to be a 3-dimensional XYZ specification") |
| 875 |
if (ncol(white) != 3L) |
| 876 |
white <- t(white) |
| 877 |
} else { |
| 878 |
## coerce to XYZ if necessary |
| 879 |
if (!is(white, "XYZ")) white <- as(white, "XYZ") |
| 880 |
white <- coords(white) |
| 881 |
} |
| 882 |
## force to single row (color) |
| 883 |
white <- white[1L, , drop = FALSE] |
| 884 |
## set whitepoint and return XYZ color invisibly |
| 885 |
assign("white", white, envir = .whitepoint) |
| 886 |
invisible(XYZ(white)) |
| 887 |
} |
| 888 |
whitepoint(NULL) |
| 889 |
|
| 890 |
#' Compute the Convex Combination of Two Colors |
| 891 |
#' |
| 892 |
#' This function can be used to compute the result of color mixing, assuming |
| 893 |
#' additive mixing (e.g., as appropriate for RGB or XYZ). |
| 894 |
#' |
| 895 |
#' |
| 896 |
#' @param alpha The mixed color is obtained by combining an amount |
| 897 |
#' \code{1 - alpha} of \code{color1} with an amount \code{alpha} of |
| 898 |
#' \code{color2}. |
| 899 |
#' @param color1 The first color. |
| 900 |
#' @param color2 The second color. |
| 901 |
#' @param where The color space where the mixing is to take place. |
| 902 |
#' @return The mixed color. This is in the color space specified by |
| 903 |
#' \code{where}. |
| 904 |
#' @author Ross Ihaka |
| 905 |
#' @seealso \code{\link{RGB}}, \code{\link{HSV}}, \code{\link{XYZ}}, |
| 906 |
#' \code{\link{LAB}}, \code{\link{polarLAB}}, \code{\link{LUV}}, |
| 907 |
#' \code{\link{polarLUV}}. |
| 908 |
#' @keywords color |
| 909 |
#' @examples |
| 910 |
#' mixcolor(0.5, sRGB(1, 0, 0), sRGB(0, 1, 0)) |
| 911 |
#' @export mixcolor |
| 912 |
mixcolor <- |
| 913 |
function(alpha, color1, color2, where = class(color1)) |
| 914 |
{ |
| 915 |
alpha = as.numeric(alpha) |
| 916 |
if(where %in% c("polarLUV", "polarLAB", "HSV", "HLS")) { |
| 917 |
warning(sprintf("convex combination of colors in polar coordinates (%s) may not be appropriate", where)) |
| 918 |
} |
| 919 |
c1 = coords(as(color1, where)) |
| 920 |
c2 = coords(as(color2, where)) |
| 921 |
na = length(alpha) |
| 922 |
n1 = nrow(c1) |
| 923 |
n2 = nrow(c2) |
| 924 |
n = max(na, n1, n2) |
| 925 |
if (na < n) alpha = rep(alpha, length.out = n) |
| 926 |
if (n1 < n) c1 = c1[rep(1:n1, length.out = n),] |
| 927 |
if (n2 < n) c2 = c2[rep(1:n2, length.out = n),] |
| 928 |
get(where)((1 - alpha) * c1 + alpha * c2) |
| 929 |
} |