View source: R/compute_color_distance.R
color_distance | R Documentation |
Compute the Euclidean distance between two colors in a given color space.
color_distance(x, y, model = "lab")
cdistance(x, y, model = "lab")
x , y |
colors specified as hex strings or named R colors. |
model |
string defining the color model; valid models are |
The numerical distance between x and y (or a vector thereof).
deltaE
and CMClc
for perceptual, rather than numerical, distance between colors.
color_distance("pink", "hotpink")
color_distance("pink", "blue")
# The absolute value of the distance depends on the color space
# it is computed in but the relative changes still make sense
color_distance("pink", "hotpink",
c("cmyk", "hcl", "lch", "hsi", "hsl", "hsv", "lab", "rgb", "rgba"))
color_distance("pink", "blue",
c("cmyk", "hcl", "lch", "hsi", "hsl", "hsv", "lab", "rgb", "rgba"))
# Find the closest color in an array of possibilities
clrs <- rainbow(20)
show_col("pink", clrs)
# use various models
dhsv <- color_distance("pink", clrs, "hsv")
dlab <- color_distance("pink", clrs, "lab")
drgb <- color_distance("pink", clrs, "rgb")
# find closest colors in each space
match_hsv <- clrs[which.min(dhsv)]
match_lab <- clrs[which.min(dlab)]
match_rgb <- clrs[which.min(drgb)]
# display them
show_col(
"pink", clrs,
c("pink", match_hsv),
c("pink", match_lab),
c("pink", match_rgb)
)
# plot the values of the differences
plot(dhsv, pch=19, col=match_hsv)
points(dlab, pch=19, col=match_lab)
points(drgb, pch=19, col=match_rgb)
legend(1,50,legend=c("hsv", "lab", "rgb"), col=c(match_hsv, match_lab, match_rgb), pch=19)
# The rainbow is defined in HSV so the distance varies linearly (only
# H varies) and the match in terms of hue is best. The variations in L*a*b*
# and RGB are similar but different components weight differently. For
# L*a*b*, luminance and chroma seem to import more since the hue of the
# match is completely off.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.