Find the closest R color for a vector of colors
Usage
closestRcolor(
x,
colorSet = colors(),
Cgrey = getOption("jam.Cgrey", 5),
C_min = Cgrey,
showPalette = FALSE,
colorModel = c("hcl", "LUV"),
Hwt = 2.5,
Cwt = 1,
Lwt = 4,
warpHue = TRUE,
preset = "ryb",
method = "maximum",
returnType = c("color", "name", "match"),
verbose = FALSE,
...
)
closest_named_color(
x,
colorSet = colorjam::named_colors,
Cgrey = getOption("jam.Cgrey", 5),
C_min = Cgrey,
showPalette = FALSE,
colorModel = c("hcl", "LUV"),
Hwt = 2.5,
Cwt = 1,
Lwt = 4,
warpHue = TRUE,
preset = "ryb",
method = "maximum",
returnType = c("color", "name", "match"),
verbose = FALSE,
...
)Arguments
- x
character vector of colors, either in hex format or any valid color in R.
- colorSet
charactervector of colors, by default includes the R colorsgrDevices::colors().- Cgrey, C_min
numericdefault 5, usinggetOption("jamba.Cgrey", 5), the Chroma at which colors are considered "grey" ("gray"). The purpose is for saturated colors to match saturated colors, and non-saturated colors to match non-saturated colors.Cgreyis applied tocolorSet.C_minis applied tox.They should be the same value in most scenarios, however it may be useful to force
xto match with non-grey colors even with small but non-zero Chroma.
Rules:
All non-grey colors
xare compared with non-greycolorSet.Grey colors
xare compared with greycolorSet, therefore color Hue is not used.Note
Cgreyis an argument injamba::make_styles(), andjamba::applyCLrange()for similar use cases.
- showPalette
logicalindicating whether to display the input colors and resulting closest matching colors by usingjamba::showColors().- colorModel
charactercolor model to use:"hcl": default, uses HCL provided byjamba::col2hcl()which uses the equivalent ofcolorspace::polarLUV()and considers color hues in terms of 360 degree angles along a color wheel."LUV": uses CIELUV color space, provided bycolorspace::LUV()which encodes the angular color hue in 3-D Cartesian space, allowing comparisons using Euclidean distance.
- Hwt, Cwt, Lwt
numeric, defaults 2.5, 1, 4, respectively, relative weights for the H, C, and L channels, respectively, only whencolorModel="hcl".- warpHue
logicalindicating whether to perform the hue warp operation usingh2hw()which improves the ability to match colors between orange and green.- preset
characterstring to define the color wheel used when matching input colorsxto colors incolorSet. This preset is used withh2hw()andhw2h(). The defaultpreset="ryb"allows greatest distinction in colors without imposing additional restrictions such as bypreset="dichromat"which would only match color-safe colors. The purpose here is to identify and label colors based upon a reference set of colors.- method
characterstring passed tostats::dist(). The defaultmethod="maximum"works well forcolorModel="hcl", and assigns distance using the largest distance across the three color coordinates H, C, and L. It requires the best overall match across all three coordinates rather than any weighted combination of coordinate distances. Other methods in testing allowed matches of different color hues when luminance and chroma values were very similar. WithcolorModel="LUV"we recommend usingmethod="euclidean", which seems to work well with projected color coordinates L, U, and V. The U, and V coordinates are roughly the angular color hue projected into a flat plane, the L describing Luminance.- returnType
charactertype of data to return:"color"returns the color values incolorSet, which by default are color names fromgrDevices::colors()"name"returnsnames(colorSet)if they exist, otherwise values fromcolorSet"match"returns an integer vector as an index tocolorSet
- verbose
logicalwhether to print verbose output.
Details
This function is intended as a relatively efficient method to compare
a set of colors to the named R colors provided by grDevices::colors().
Color matching provides substantial improvements over similar functions from other R packages. Notably, colors are matched using either HCL or LUB color model by default, both of which provide vast improvement over RGB color matching, due to better spacing of colors, and increased resolution of color hue.
For colorModel="HCL" the coordinates are weighted to prioritize
matching color Hue above Chroma and Luminance. The distance method
by default uses method="maximum" which also emphasizes the lowest
distance in any of the three dimensions.
Notably, this function does not use color_distance(), in part
because in practice the color metrics used by 'cie2000', 'cie94', 'cmc'
did not prioritize the same color hue, and often the returned color
was similar but not the most visibly similar in terms of hue.
Also color_distance() does not currently permit weights for Hue,
Chroma, Luminance, as these values are already defined in
farver::compare_colour().
Todo
Consider testing then adding
color_distance()as an optional metric. Test whether it could improve results.
See also
Other colorjam core:
blend_colors(),
color_complement(),
colors_to_df(),
group2colors(),
rainbowJam(),
sort_colors(),
subset_colors()
Examples
closestRcolor(rainbowJam(12), showPalette=TRUE);
#> [1] "gold2" "tan4" "tan2" "salmon3"
#> [5] "lightcoral" "palevioletred2" "plum2" "lightslateblue"
#> [9] "mediumpurple1" "cornflowerblue" "skyblue1" "steelblue2"