Title: | Basic S4 Classes and Methods for Mapping Between Numeric Values and Colors |
---|---|
Description: | A simple set of classes and methods for mapping between scalar intensity values and colors. There is also support for layering maps on top of one another using alpha composition. |
Authors: | Bradley R Buchsbaum [aut, cre] |
Maintainer: | Bradley R Buchsbaum <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.0 |
Built: | 2024-11-05 02:40:14 UTC |
Source: | https://github.com/bbuchsbaum/colorplane |
extract the alpha channel
alpha_channel(x, ...) ## S4 method for signature 'HexColorPlane' alpha_channel(x, normalize = TRUE) ## S4 method for signature 'ConstantColorPlane' alpha_channel(x, normalize = TRUE) ## S4 method for signature 'RGBColorPlane' alpha_channel(x, normalize = TRUE)
alpha_channel(x, ...) ## S4 method for signature 'HexColorPlane' alpha_channel(x, normalize = TRUE) ## S4 method for signature 'ConstantColorPlane' alpha_channel(x, normalize = TRUE) ## S4 method for signature 'RGBColorPlane' alpha_channel(x, normalize = TRUE)
x |
the object to extract alpha channel from |
... |
extra args |
normalize |
divide by 255 |
a numeric vector of alpha channel values
cp <- IntensityColorPlane(seq(1,5), cols=rainbow(25)) cl <- map_colors(cp, irange=c(0,50)) stopifnot(length(alpha_channel(cl)) == 5)
cp <- IntensityColorPlane(seq(1,5), cols=rainbow(25)) cl <- map_colors(cp, irange=c(0,50)) stopifnot(length(alpha_channel(cl)) == 5)
convert to hex colors
as_hexcol(x, ...) ## S4 method for signature 'RGBColorPlane' as_hexcol(x) ## S4 method for signature 'HexColorPlane' as_hexcol(x)
as_hexcol(x, ...) ## S4 method for signature 'RGBColorPlane' as_hexcol(x) ## S4 method for signature 'HexColorPlane' as_hexcol(x)
x |
the object to convert |
... |
extra args |
a character vector of ex colors
convert to rgb colors
as_rgb(x, ...) ## S4 method for signature 'RGBColorPlane' as_rgb(x) ## S4 method for signature 'HexColorPlane' as_rgb(x) ## S4 method for signature 'ConstantColorPlane' as_rgb(x)
as_rgb(x, ...) ## S4 method for signature 'RGBColorPlane' as_rgb(x) ## S4 method for signature 'HexColorPlane' as_rgb(x) ## S4 method for signature 'ConstantColorPlane' as_rgb(x)
x |
the object to convert |
... |
extra args |
a numeric matrix of rgb components
cp <- IntensityColorPlane(seq(1,100), cols=rainbow(25)) cl <- map_colors(cp, irange=c(0,50)) rgbcols <- as_rgb(cl)
cp <- IntensityColorPlane(seq(1,100), cols=rainbow(25)) cl <- map_colors(cp, irange=c(0,50)) rgbcols <- as_rgb(cl)
given two color planes, generate a new color plane by blending the colors using the supplied alpha multiplier.
blend_colors(bottom, top, alpha) ## S4 method for signature 'ColorPlane,ColorPlane,numeric' blend_colors(bottom, top, alpha = 1) ## S4 method for signature 'ColorPlane,ColorPlane,missing' blend_colors(bottom, top) ## S4 method for signature 'HexColorPlane,RGBColorPlane,numeric' blend_colors(bottom, top, alpha) ## S4 method for signature 'HexColorPlane,ConstantColorPlane,numeric' blend_colors(bottom, top, alpha = 1)
blend_colors(bottom, top, alpha) ## S4 method for signature 'ColorPlane,ColorPlane,numeric' blend_colors(bottom, top, alpha = 1) ## S4 method for signature 'ColorPlane,ColorPlane,missing' blend_colors(bottom, top) ## S4 method for signature 'HexColorPlane,RGBColorPlane,numeric' blend_colors(bottom, top, alpha) ## S4 method for signature 'HexColorPlane,ConstantColorPlane,numeric' blend_colors(bottom, top, alpha = 1)
bottom |
the bottom color plane |
top |
the top color plane |
alpha |
the alpha overlay value. |
The functions in this package blend colors based on the "over" operator where 'top' if foreground and 'bottom' is background.
a new ColorPlane
instance with 'top' and 'bottom' alpha-blended.
https://en.wikipedia.org/wiki/Alpha_compositing
top <- IntensityColorPlane(1:5, cols=rainbow(5)) bottom <- IntensityColorPlane(1:5, cols=rev(rainbow(5))) top <- map_colors(top) bottom <- map_colors(bottom) bc <- blend_colors(bottom, top, .5)
top <- IntensityColorPlane(1:5, cols=rainbow(5)) bottom <- IntensityColorPlane(1:5, cols=rev(rainbow(5))) top <- map_colors(top) bottom <- map_colors(bottom) bc <- blend_colors(bottom, top, .5)
convert color name to hex character string
col2hex(cname, alpha = 1)
col2hex(cname, alpha = 1)
cname |
one or more color names, e.g. "red" |
alpha |
the value of the alpha channel, ranging from 0 to 1 (default is 1) |
a vector of hex color values, one per color name
ColorScale
irange
the intensity range of the scale
threshold
the alpha thresholding range
clr
a vector of hex colors
ConstantColorPlane constructor taking a single hex 'character' vector defining a constant color plane.
ConstantColorPlane(clr)
ConstantColorPlane(clr)
clr |
a single hex color as a 'character' vector of length 1 defining the constant color. |
a new ConstantColorPlane
instance
clr
the constant color as hex value
cp <- ConstantColorPlane(clr="#FF0000")
cp <- ConstantColorPlane(clr="#FF0000")
DiscreteColorPlane constructor taking list with names mapping to color values in hex representation. This object is used when one has a one to one mapping between discrete set of strings/values to discrete set of colors.
DiscreteColorPlane(lookup)
DiscreteColorPlane(lookup)
lookup |
a "lookup table", which is a named list mapping discrete values to hex colors |
a new DiscreteColorPlane
instance
lookup
a lookup table mapping values to hex colors
lookup <- as.list(col2hex(c("red", "blue", "green"))) names(lookup) <- c("a", "b", "c") cp <- DiscreteColorPlane(lookup) values <- c("a", "b", "c", "a", "c")
lookup <- as.list(col2hex(c("red", "blue", "green"))) names(lookup) <- c("a", "b", "c") cp <- DiscreteColorPlane(lookup) values <- c("a", "b", "c", "a", "c")
get the color associated with one or more values
get_color(x, v, ...)
get_color(x, v, ...)
x |
the color lookup table |
v |
the intensity value(s) |
... |
extra args |
a color value
HexColorPlane constructor taking a 'character' vector of colors to define a color plane.
HexColorPlane(clr)
HexColorPlane(clr)
clr |
a vector of hex colors |
a new HexColorPlane
instance
An association of intensities and colors
IntensityColorPlane constructor
IntensityColorPlane(intensity, cols = rainbow(255), alpha = 1)
IntensityColorPlane(intensity, cols = rainbow(255), alpha = 1)
intensity |
a numeric vector of intensity values |
cols |
a vector of hex character codes |
alpha |
a vector of alpha values ranging from 0 to 1 |
a new IntensityColorPlane
instance
intensity
a vector of intensity values
alpha
a vector of alpha values
colmap
a color map containing a vector of hex character codes
instantiate a vector of colors from a ColorPlane specification.
map_colors(x, ...) ## S4 method for signature 'ConstantColorPlane' map_colors(x) ## S4 method for signature 'HexColorPlane' map_colors(x) ## S4 method for signature 'DiscreteColorPlane' map_colors(x, values, ...) ## S4 method for signature 'IntensityColorPlane' map_colors(x, alpha = 1, threshold = NULL, irange = NULL)
map_colors(x, ...) ## S4 method for signature 'ConstantColorPlane' map_colors(x) ## S4 method for signature 'HexColorPlane' map_colors(x) ## S4 method for signature 'DiscreteColorPlane' map_colors(x, values, ...) ## S4 method for signature 'IntensityColorPlane' map_colors(x, alpha = 1, threshold = NULL, irange = NULL)
x |
the object to map over |
... |
extra args |
values |
the values to map to colors via the discrete lookup table |
alpha |
alpha multiplier from 0 to 1. |
threshold |
two-sided threshold as a 2-element vector, e.g. 'threshold=c(-3,3)' indicating two-sided transparency thresholds. |
irange |
the intensity range defining min and max of scale. |
a HexColorPlane
instance containing the mapped colors
cp <- IntensityColorPlane(seq(1,100), cols=rainbow(25)) cl <- map_colors(cp, irange=c(0,50)) stopifnot(cl@clr[50] == rainbow(25)[25])
cp <- IntensityColorPlane(seq(1,100), cols=rainbow(25)) cl <- map_colors(cp, irange=c(0,50)) stopifnot(cl@clr[50] == rainbow(25)[25])
convert rgb colors to hex colors
rgb2hex(r, g, b, alpha)
rgb2hex(r, g, b, alpha)
r |
the red color component |
g |
the green color component |
b |
the blue color component |
alpha |
the alpha component |
a hex color represenation as 'character' vector
RGBColorPlane constructor taking a 3- or 4-column numeric matrix
of RGB(A) colors in the 0-255 range.
RGBColorPlane(clr)
RGBColorPlane(clr)
clr |
a matrix of colors where the first column is red, second column is green, third column is blue, and optional fourth column is alpha. |
a new RGBColorPlane
instance
rgba_cmat <- rbind(c(255,0,0,255), c(0, 255, 0, 255), c(0, 0, 255, 0)) cp <- RGBColorPlane(rgba_cmat) stopifnot(all(cp@clr[1,] == c(255,0,0,255)))
rgba_cmat <- rbind(c(255,0,0,255), c(0, 255, 0, 255), c(0, 0, 255, 0)) cp <- RGBColorPlane(rgba_cmat) stopifnot(all(cp@clr[1,] == c(255,0,0,255)))