Title: | Color Manipulation Tools |
---|---|
Description: | Manipulate and visualize colors in a intuitive, low-dependency and functional way. |
Authors: | Emil Hvitfeldt [aut, cre] |
Maintainer: | Emil Hvitfeldt <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.2.9000 |
Built: | 2024-11-18 20:35:55 UTC |
Source: | https://github.com/emilhvitfeldt/prismatic |
best_contrast()
finds the color in y
with the highest contrast to the
color x
.
best_contrast(x, y = c("#010101", "#FFFFFF"))
best_contrast(x, y = c("#010101", "#FFFFFF"))
x |
A vector of colors as described in |
y |
A vector of colors as described in |
A vector of the same length as x
with, for each element of x
, the
element of y
that has the highest contrast to x
.
best_contrast("red") best_contrast("grey20") best_contrast("white") best_contrast(rainbow(10), rainbow(3))
best_contrast("red") best_contrast("grey20") best_contrast("white") best_contrast(rainbow(10), rainbow(3))
check_color_blindness()
will showcase the effect of the three kinds of
color vision deficiency, Deuteranopia, Protanopia, and Tritanopia, at the
same time side by side in a plot.
check_color_blindness(col)
check_color_blindness(col)
col |
A |
Invisibly col
.
check_color_blindness(rainbow(10)) check_color_blindness(terrain.colors(10))
check_color_blindness(rainbow(10)) check_color_blindness(terrain.colors(10))
Set alpha in color
clr_alpha(col, alpha = 0.5)
clr_alpha(col, alpha = 0.5)
col |
A |
alpha |
Numeric between 0 and 1. 0 will result in full transparency and 1 in no transparency. |
A colors
object of the same length as col
.
plot(clr_alpha(rainbow(10), 0.5)) plot(clr_alpha(rainbow(10), 0.2)) plot(clr_alpha(rainbow(10), seq(0, 1, length.out = 10)))
plot(clr_alpha(rainbow(10), 0.5)) plot(clr_alpha(rainbow(10), 0.2)) plot(clr_alpha(rainbow(10), seq(0, 1, length.out = 10)))
Make a color darker
clr_darken(col, shift = 0.5, space = c("HCL", "HSL", "combined"))
clr_darken(col, shift = 0.5, space = c("HCL", "HSL", "combined"))
col |
A |
shift |
A number between 0 and 1. 0 will do zero darkening, and 1 will do complete darkening, turning the color to black. Defaults to 0.5. |
space |
A character string specifying the color space in which adjustment happens. Can be either "HCL", "HSL" or "combined". Defaults to "HCL". |
The colors will be transformed to HSL color space (hue, saturation,
lightness) where the lightness of the color will be modified. The lightness
of a color takes a value between 0 and 1, with 0 being black and 1 being
white. shift
takes a value between 0 and 1, where 0 means that the
lightness stays unchanged and 1 means completely black. As an example, if
the lightness of the color is 0.6 and shift
is 0.5, then the lightness
will be set to the halfway point between 0.6 and 0, which is 0.3.
If space = "HSL"
then the colors are transformed to HSL space where
the lightness value L is adjusted. If space = "HCL"
then the colors are
transformed to Cylindrical HCL space where the luminance value L is adjusted.
If space = "combined"
then the colors are transformed into HSL and
Cylindrical HCL space. Where the color adjusting is happening HSL is copied
to the values in the HCL transformation. Thus the "combined" transformation
adjusts the luminance in HCL space and chroma in HSL space. For more
information regarding use of color spaces, please refer to the colorspace
paper https://arxiv.org/abs/1903.06490.
A color
object of the same length as col
.
https://en.wikipedia.org/wiki/HSL_and_HSV
https://en.wikipedia.org/wiki/CIELUV
https://arxiv.org/abs/1903.06490
# Using linear shift plot(clr_darken(rep("red", 11), shift = seq(0, 1, 0.1))) plot(clr_darken(rep("red", 11), shift = seq(0, 1, 0.1), space = "HSL")) plot(clr_darken(rep("red", 11), shift = seq(0, 1, 0.1), space = "combined")) plot(clr_darken(terrain.colors(10))) # Using exponential shifts plot(clr_darken(rep("red", 11), shift = log(seq(1, exp(1), length.out = 11))))
# Using linear shift plot(clr_darken(rep("red", 11), shift = seq(0, 1, 0.1))) plot(clr_darken(rep("red", 11), shift = seq(0, 1, 0.1), space = "HSL")) plot(clr_darken(rep("red", 11), shift = seq(0, 1, 0.1), space = "combined")) plot(clr_darken(terrain.colors(10))) # Using exponential shifts plot(clr_darken(rep("red", 11), shift = log(seq(1, exp(1), length.out = 11))))
Make a color more desaturated
clr_desaturate(col, shift = 0.5)
clr_desaturate(col, shift = 0.5)
col |
A |
shift |
A numeric between 0 and 1. 0 will do zero desaturation, 1 will do complete desaturation. Defaults to 0.5. |
The colors will be transformed to HSL color space (hue, saturation,
lightness) where the saturation of the color will be modified. The
saturation of a color takes a value between 0 and 1, with 0 being black and
1 being white. shift
takes a value between 0 and 1, where 0 means that the
saturation stays unchanged and 1 means completely desaturated. As an example,
if the saturation of the color is 0.6 and shift
is 0.5, then the
saturation will be set to the halfway point between 0.6 and 0 which is 0.3.
A colors
object of the same length as col
.
https://en.wikipedia.org/wiki/HSL_and_HSV
plot(clr_desaturate(terrain.colors(10), shift = 0.5)) plot(clr_desaturate(terrain.colors(10), shift = 0.9)) plot(clr_desaturate(rep("firebrick", 11), shift = seq(0, 1, 0.1)))
plot(clr_desaturate(terrain.colors(10), shift = 0.5)) plot(clr_desaturate(terrain.colors(10), shift = 0.9)) plot(clr_desaturate(rep("firebrick", 11), shift = seq(0, 1, 0.1)))
Extract multiple color components at the same time.
clr_extract( col, components = c("red", "green", "blue", "hue_hsl", "saturation", "lightness", "hue_hcl", "chroma", "luminance") )
clr_extract( col, components = c("red", "green", "blue", "hue_hsl", "saturation", "lightness", "hue_hcl", "chroma", "luminance") )
col |
A |
components |
A character vector of components that should be extracted. See Details for allowed components. |
The allowed values for components
are:
red
green
blue
hue_hsl
saturation
lightness
hue_hcl
chroma
luminance
clr_extract()
is to be preferred over other extraction functions if you
need to extract multiple components at the same time, since it doesn't
repeat transformations.
A data.frame of components.
Other Extraction:
clr_extract_chroma()
,
clr_extract_hue()
,
extract_rgba()
clr_extract(rainbow(10)) clr_extract(rainbow(10), c("hue_hsl", "saturation"))
clr_extract(rainbow(10)) clr_extract(rainbow(10), c("hue_hsl", "saturation"))
Extract the hue, chroma, or luminance color components from a vector of colors.
clr_extract_chroma(col)
clr_extract_chroma(col)
col |
A |
The range of the value are:
Hue is ranging from 0 to 360.
Luminance is ranging from 0 to 100.
Chroma, while dependent on hue and luminance, will roughly be within 0 and 180.
Use clr_extract()
if you are planning to extraction multiple components.
Numeric vector of values.
Other Extraction:
clr_extract()
,
clr_extract_hue()
,
extract_rgba()
clr_extract_hue(rainbow(100), "HCL") clr_extract_chroma(rainbow(100)) clr_extract_luminance(rainbow(100))
clr_extract_hue(rainbow(100), "HCL") clr_extract_chroma(rainbow(100)) clr_extract_luminance(rainbow(100))
Extract the hue, saturation, or lightness color components from a vector of colors.
clr_extract_hue(col, space = c("HSL", "HCL")) clr_extract_saturation(col) clr_extract_lightness(col) clr_extract_luminance(col)
clr_extract_hue(col, space = c("HSL", "HCL")) clr_extract_saturation(col) clr_extract_lightness(col) clr_extract_luminance(col)
col |
A |
space |
A character string specifying the color space where hue is extracted from. Can be either "HCL" or "HSL" (default). |
The range of the value are:
From 0 to 360 for hue. This in a circular fashion such that 0 and 360 are near identical. 0 is red.
From 0 to 100 for saturation where 100 is full saturation and 0 is no saturation.
From 0 to 100 for lightness where 100 is full lightness and 0 is no lightness.
Use clr_extract()
if you are planning to extraction multiple components.
Numeric vector of values.
Other Extraction:
clr_extract()
,
clr_extract_chroma()
,
extract_rgba()
clr_extract_hue(rainbow(100), "HSL") clr_extract_saturation(rainbow(100)) clr_extract_lightness(rainbow(100))
clr_extract_hue(rainbow(100), "HSL") clr_extract_saturation(rainbow(100)) clr_extract_lightness(rainbow(100))
clr_grayscale()
has a selection of different methods to turn colors into
grayscale.
clr_grayscale( col, method = c("luma", "averaging", "min_decomp", "max_decomp", "red_channel", "green_channel", "blue_channel") ) clr_greyscale( col, method = c("luma", "averaging", "min_decomp", "max_decomp", "red_channel", "green_channel", "blue_channel") )
clr_grayscale( col, method = c("luma", "averaging", "min_decomp", "max_decomp", "red_channel", "green_channel", "blue_channel") ) clr_greyscale( col, method = c("luma", "averaging", "min_decomp", "max_decomp", "red_channel", "green_channel", "blue_channel") )
col |
A |
method |
A character string specifying the grayscaling method. Can be one of "luma", "averaging", "min_decomp", "max_decomp", "red_channel", "green_channel" and "blue_channel". Defaults to "luma". |
If method = "averaging"
then the red, green and blue will be
averaged together to create the grey value. This method does a poor job of
representing the way the human eye sees color. If method = "luma"
(the
default), a weighted average is used to calculate the grayscale values. The
BT. 709 method from the ITU Radiocommunication Sector have determined the
weights. If method
is "min_decomp"
or "max_decomp"
, then a decomposition
method is used where the minimum or maximum color value have been selected
for the color value. So the color rgb(60, 120, 40) would have the "min_decomp"
value of 40 and "max_decomp" value of 120. If method is "red_channel"
,
"green_channel"
or "blue_channel"
, then the corresponding color channel
will be selected for the values of grayscale.
A colors
object of the same length as col
.
https://tannerhelland.com/3643/grayscale-image-algorithm-vb6/
https://en.wikipedia.org/wiki/Luma
plot(clr_grayscale(rainbow(10))) plot(clr_grayscale(terrain.colors(10))) viridis_colors <- c( "#4B0055FF", "#422C70FF", "#185086FF", "#007094FF", "#008E98FF", "#00A890FF", "#00BE7DFF", "#6CD05EFF", "#BBDD38FF", "#FDE333FF" ) plot(clr_grayscale(viridis_colors, method = "luma")) plot(clr_grayscale(viridis_colors, method = "averaging")) plot(clr_grayscale(viridis_colors, method = "min_decomp")) plot(clr_grayscale(viridis_colors, method = "max_decomp")) plot(clr_grayscale(viridis_colors, method = "red_channel")) plot(clr_grayscale(viridis_colors, method = "green_channel")) plot(clr_grayscale(viridis_colors, method = "blue_channel"))
plot(clr_grayscale(rainbow(10))) plot(clr_grayscale(terrain.colors(10))) viridis_colors <- c( "#4B0055FF", "#422C70FF", "#185086FF", "#007094FF", "#008E98FF", "#00A890FF", "#00BE7DFF", "#6CD05EFF", "#BBDD38FF", "#FDE333FF" ) plot(clr_grayscale(viridis_colors, method = "luma")) plot(clr_grayscale(viridis_colors, method = "averaging")) plot(clr_grayscale(viridis_colors, method = "min_decomp")) plot(clr_grayscale(viridis_colors, method = "max_decomp")) plot(clr_grayscale(viridis_colors, method = "red_channel")) plot(clr_grayscale(viridis_colors, method = "green_channel")) plot(clr_grayscale(viridis_colors, method = "blue_channel"))
Make a color lighter
clr_lighten(col, shift = 0.5, space = c("HCL", "HSL", "combined"))
clr_lighten(col, shift = 0.5, space = c("HCL", "HSL", "combined"))
col |
A |
shift |
A number between 0 and 1. 0 will do zero lightening, and 1 will do complete lightening, turning the color to white. Defaults to 0.5. |
space |
A character string specifying the color space in which adjustment happens. Can be either "HCL", "HSL" or "combined". Defaults to "HCL". |
The colors will be transformed to HSL color space (hue, saturation,
lightness) where the lightness of the color will be modified. The lightness
of a color takes a value between 0 and 1, with 0 being black and 1 being
white. shift
takes a value between 0 and 1, where 0 means the lightness
stays unchanged and 1 means completely white. As an example, if the lightness
of the color is 0.6 and shift
is 0.5, the lightness will be set to the
halfway point between 0.6 and 1 which is 0.8.
If space = "HSL"
then the colors are transformed to HSL space where
the lightness value L is adjusted. If space = "HCL"
then the colors are
transformed to Cylindrical HCL space where the luminance value L is adjusted.
If space = "combined"
then the colors are transformed into HSL and
Cylindrical HCL space. Where the color adjusting is happening HLS is copied
to the values in the HCL transformation. Thus, the "combined" transformation
adjusts the luminance in HCL space and chroma in HSL space. For more
information regarding use of color spaces, please refer to the colorspace
paper https://arxiv.org/abs/1903.06490.
A colors
object of the same length as col
.
https://en.wikipedia.org/wiki/HSL_and_HSV
https://en.wikipedia.org/wiki/CIELUV
https://arxiv.org/abs/1903.06490
# Using linear shift plot(clr_lighten(rep("red", 11), shift = seq(0, 1, 0.1))) plot(clr_lighten(rep("red", 11), shift = seq(0, 1, 0.1), space = "HSL")) plot(clr_lighten(rep("red", 11), shift = seq(0, 1, 0.1), space = "combined")) plot(clr_lighten(terrain.colors(10))) # Using exponential shifts plot(clr_lighten(rep("red", 11), shift = log(seq(1, exp(1), length.out = 11))))
# Using linear shift plot(clr_lighten(rep("red", 11), shift = seq(0, 1, 0.1))) plot(clr_lighten(rep("red", 11), shift = seq(0, 1, 0.1), space = "HSL")) plot(clr_lighten(rep("red", 11), shift = seq(0, 1, 0.1), space = "combined")) plot(clr_lighten(terrain.colors(10))) # Using exponential shifts plot(clr_lighten(rep("red", 11), shift = log(seq(1, exp(1), length.out = 11))))
Mix color into color(s)
clr_mix(col, mix_in, ratio = 0.5)
clr_mix(col, mix_in, ratio = 0.5)
col |
A |
mix_in |
Same as |
ratio |
Numeric between 0 and 1. 0 will result in |
A colors
object of the same length as col
.
plot(clr_mix(rainbow(10), "blue")) plot(clr_mix(rainbow(10), "red")) plot(clr_mix(rainbow(10), "#5500EE")) plot(clr_mix(rainbow(10), "black", seq(1, 0, length.out = 10)))
plot(clr_mix(rainbow(10), "blue")) plot(clr_mix(rainbow(10), "red")) plot(clr_mix(rainbow(10), "#5500EE")) plot(clr_mix(rainbow(10), "black", seq(1, 0, length.out = 10)))
Negate colors in RGB space
clr_negate(col)
clr_negate(col)
col |
A |
The negation of color is happening in the red-green-blue colorspace RGB. This means if we take the specification for orange which is rgb(255, 165, 0), then we negate by taking the opposite number on the scale from 0 to 255, leaving us with rgb(0, 90, 255), which is a shade of blue.
A colors
object of the same length as col
.
clr_negate("orange") terr <- color(terrain.colors(10)) terr clr_negate(terr) plot(terr) plot(clr_negate(terr))
clr_negate("orange") terr <- color(terrain.colors(10)) terr clr_negate(terr) plot(terr) plot(clr_negate(terr))
Simulate color vision deficiency
clr_protan(col, severity = 1) clr_deutan(col, severity = 1) clr_tritan(col, severity = 1)
clr_protan(col, severity = 1) clr_deutan(col, severity = 1) clr_tritan(col, severity = 1)
col |
A |
severity |
A numeric indicating the severity of the color vision defect. Must be a number between 0 and 1, where 0 means no deficiency, and 1 means complete deficiency. Defaults to 1. |
The matrices used to perform transformations have been taken as the
1.0 value in table 1 in
http://www.inf.ufrgs.br/~oliveira/pubs_files/CVD_Simulation/CVD_Simulation.html.
Values for severity
values between 0 and 1 will be linearly interpolated.
A colors
object of the same length as col
.
http://www.inf.ufrgs.br/~oliveira/pubs_files/CVD_Simulation/CVD_Simulation.html
Gustavo M. Machado, Manuel M. Oliveira, and Leandro A. F. Fernandes "A Physiologically-based Model for Simulation of Color Vision Deficiency". IEEE Transactions on Visualization and Computer Graphics. Volume 15 (2009), Number 6, November/December 2009. pp. 1291-1298.
rainbow_colors <- color(rainbow(10)) plot(clr_protan(rainbow_colors)) plot(clr_deutan(rainbow_colors)) plot(clr_tritan(rainbow_colors)) viridis_colors <- c( "#4B0055FF", "#422C70FF", "#185086FF", "#007094FF", "#008E98FF", "#00A890FF", "#00BE7DFF", "#6CD05EFF", "#BBDD38FF", "#FDE333FF" ) plot(clr_protan(viridis_colors)) plot(clr_deutan(viridis_colors)) plot(clr_tritan(viridis_colors))
rainbow_colors <- color(rainbow(10)) plot(clr_protan(rainbow_colors)) plot(clr_deutan(rainbow_colors)) plot(clr_tritan(rainbow_colors)) viridis_colors <- c( "#4B0055FF", "#422C70FF", "#185086FF", "#007094FF", "#008E98FF", "#00A890FF", "#00BE7DFF", "#6CD05EFF", "#BBDD38FF", "#FDE333FF" ) plot(clr_protan(viridis_colors)) plot(clr_deutan(viridis_colors)) plot(clr_tritan(viridis_colors))
Rotate the colors around the hue wheel
clr_rotate(col, degrees = 0)
clr_rotate(col, degrees = 0)
col |
A |
degrees |
A number between 0 and 360, denoting the amount of degrees the colors should be rotated. Defaults to 0. |
The colors will be transformed to HCL color space (Hue-Chroma-Luminance) where the hue of the color will be rotated.
A colors
object of the same length as col
.
https://en.wikipedia.org/wiki/HCL_color_space
plot(clr_rotate(terrain.colors(10))) plot(clr_rotate(terrain.colors(10), degrees = 90)) plot(clr_rotate(terrain.colors(10), degrees = 180)) plot(clr_rotate(rep("magenta", 11), degrees = seq(0, 360, length.out = 11)))
plot(clr_rotate(terrain.colors(10))) plot(clr_rotate(terrain.colors(10), degrees = 90)) plot(clr_rotate(terrain.colors(10), degrees = 180)) plot(clr_rotate(rep("magenta", 11), degrees = seq(0, 360, length.out = 11)))
Make a color more saturated
clr_saturate(col, shift = 0.5)
clr_saturate(col, shift = 0.5)
col |
A |
shift |
A numeric between 0 and 1. 0 will do zero saturation, 1 will do complete saturation. Defaults to 0.5. |
The colors will be transformed to HSL color space (hue, saturation,
lightness) where the saturation of the color will be modified. The
saturation of a color takes a value between 0 and 1, with 0 being black and
1 being white. shift
takes a value between 0 and 1, where 0 means that the
saturation stays unchanged and 1 means completely saturated. As an example,
if the saturation of the color is 0.6 and shift
is 0.5, then the
saturation will be set to the halfway point between 0.6 and 1 which is 0.8.
A color
object of the same length as col
.
https://en.wikipedia.org/wiki/HSL_and_HSV
plot(clr_saturate(terrain.colors(10), shift = 0.5)) plot(clr_saturate(terrain.colors(10), shift = 1)) plot(clr_saturate(rep("firebrick", 11), shift = seq(0, 1, 0.1)))
plot(clr_saturate(terrain.colors(10), shift = 0.5)) plot(clr_saturate(terrain.colors(10), shift = 1)) plot(clr_saturate(rep("firebrick", 11), shift = seq(0, 1, 0.1)))
color
vectorTurn vector of colors to color
vector
color(col) colour(col)
color(col) colour(col)
col |
A |
Alpha values will be automatically added to hexcodes. If no alpha
value is present in col
, it will default to no alpha (FF).
A colors
object of the same length as col
. Returns hex 8 digits
form "#rrggbbaa". See Details.
terrain_10 <- color(terrain.colors(10)) terrain_10[1:4] plot(terrain_10) plot(terrain_10, labels = TRUE) grey_10 <- color(gray.colors(10, start = 0, end = 1)) grey_10 plot(grey_10, labels = TRUE)
terrain_10 <- color(terrain.colors(10)) terrain_10[1:4] plot(terrain_10) plot(terrain_10, labels = TRUE) grey_10 <- color(gray.colors(10, start = 0, end = 1)) grey_10 plot(grey_10, labels = TRUE)
contrast_ratio()
calculates the contrast ratio between the color x
and
the color(s) y
. Contrast ratios can range from 1 to 21 with 1 being no
contrast (i.e., same color) and 21 being highest contrast.
contrast_ratio(x, y)
contrast_ratio(x, y)
x |
A length 1 color object (see |
y |
A color object (see |
The formula used for calculating a contrast ratio between two colors is
where
L1 is the relative luminance of the lighter of the colors, and
L2 is the relative luminance of the darker of the colors.
Relative luminance is calculated according to https://www.w3.org/TR/WCAG21/#dfn-relative-luminance.
A numerical vector of the same length as y
of the calculated
contrast ratios.
https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html
contrast_ratio("red", "blue") contrast_ratio("grey20", grey.colors(10)) contrast_ratio("white", c("white", "black"))
contrast_ratio("red", "blue") contrast_ratio("grey20", grey.colors(10)) contrast_ratio("white", c("white", "black"))
Extract the red, green, or blue color components from a vector of colors.
clr_extract_red(col) clr_extract_green(col) clr_extract_blue(col) clr_extract_alpha(col)
clr_extract_red(col) clr_extract_green(col) clr_extract_blue(col) clr_extract_alpha(col)
col |
A |
The values of the output will range between 0 and 255.
Use clr_extract()
if you are planning to extract multiple components.
A numeric vector giving the extracted values.
Other Extraction:
clr_extract()
,
clr_extract_chroma()
,
clr_extract_hue()
clr_extract_red(rainbow(100)) clr_extract_green(rainbow(100)) clr_extract_blue(rainbow(100)) clr_extract_alpha(rainbow(100))
clr_extract_red(rainbow(100)) clr_extract_green(rainbow(100)) clr_extract_blue(rainbow(100)) clr_extract_alpha(rainbow(100))
colors
objectTest if object is a colors
object
is_color(x)
is_color(x)
x |
An object. |
TRUE
if the object inherits from the colors
class, else FALSE
.
This function lets you modify individual axes of a color in HCL color space.
modify_hcl(col, h, c, l)
modify_hcl(col, h, c, l)
col |
A |
h |
Expression to modify the hue of |
c |
Expression to modify the chroma of |
l |
Expression to modify the luminance of |
The expression used in h
, c
, and l
is evaluated in the hcl
space and
you have access to h
, c
, and l
as vectors along with vectors in the
calling environment.
h
ranges from 0 to 360, l
ranges from 0 to 100, and c
while dependent
on h
and l
will roughly be within 0 and 180, but often on a narrower
range. Colors after modification will be adjusted to fit within the color
space.
A colors
object.
https://en.wikipedia.org/wiki/HCL_color_space
plot(modify_hcl("red", h = 160)) plot(modify_hcl("red", h = h + 50)) plot(modify_hcl("red", h = h + 1:100)) plot(modify_hcl("red", c = c - 1:200)) plot(modify_hcl("red", l = l + 1:50)) plot(modify_hcl(rainbow(10), l = 25)) plot(modify_hcl(rainbow(10), h + h / 2, l = 70))
plot(modify_hcl("red", h = 160)) plot(modify_hcl("red", h = h + 50)) plot(modify_hcl("red", h = h + 1:100)) plot(modify_hcl("red", c = c - 1:200)) plot(modify_hcl("red", l = l + 1:50)) plot(modify_hcl(rainbow(10), l = 25)) plot(modify_hcl(rainbow(10), h + h / 2, l = 70))