RgbToCol: Find the Nearest Named R-Color to a Given RGB-Color

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/DescTools.r

Description

Converting a RGB-color to a named R-Color means looking for a color in the R-palette, which is nearest to the given RGB-color. This function uses the minimum of squared distance ("euclidean") or the minimum absolute distance ("manhattan") as proximity measure.
RgbToLong() converts a RGB-color to a long integer in numeric format. LongToRGB() does it the other way round.

Usage

1
2
3
4
RgbToCol(col, method = "rgb", metric = "euclidean")

RgbToLong(col)
LongToRgb(col)

Arguments

col

the color in rgb code, say a matrix with the red, green and blue code in the rows.

method

character string specifying the color space to be used. Can be "rgb" (default) or "hsv".

metric

character string specifying the metric to be used for calculating distances between the colors. Available options are "euclidean" (default) and "manhattan". Euclidean distances are root sum-of-squares of differences, and manhattan distances are the sum of absolute differences.

Details

It may not be clear from the start which method, rgb or hsv, yield the more natural results. Trying and comparing is a recommended strategy. Moreover the shortest numerical distance will not always be the best choice, when comparing the colours visually.

Value

the name of the nearest found R color.

Author(s)

Andri Signorell <andri@signorell.net>

See Also

ColToRgb and the other conversion functions

Examples

1
2
3
RgbToCol(matrix(c(162,42,42), nrow=3))

RgbToLong(matrix(c(162,42,42), nrow=3))

Example output

[1] "brown"
[1] 2763426

DescTools documentation built on June 17, 2021, 5:12 p.m.