Suppose I have this vector
x <- c("165 239 210", "111 45 93")
Is there a neat package to convert RGB values to hex values in R? I found many javascript ways but not one for R.
x <- "#A5EFD2" "#6F2D5D"
First ValueTake the first number, 220, and divide by 16. 220 / 16 = 13.75, which means that the first digit of the 6-digit hex color code is 13, or D. Take the remainder of the first digit, 0.75, and multiply by 16. 0.75 (16) = 12, which means that the second digit of the 6-digit hex color code is 12, or C.
A HEX color is expressed as a six-digit combination of numbers and letters defined by its mix of red, green and blue (RGB). Basically, a HEX color code is shorthand for its RGB values with a little conversion gymnastics in between.
The RGB color 255, 0, 92 is a dark color, and the websafe version is hex FF0066. The color can be described as dark saturated red.
Just split the string up, and then use rgb
:
x <- c("165 239 210", "111 45 93")
sapply(strsplit(x, " "), function(x)
rgb(x[1], x[2], x[3], maxColorValue=255))
#[1] "#A5EFD2" "#6F2D5D"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With