Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB to Hex converter

Tags:

r

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"
like image 357
alki Avatar asked Jul 22 '15 21:07

alki


People also ask

How do you convert RGB to hex?

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.

Is hex code RGB?

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.

What color is RGB 255 0 92?

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.


1 Answers

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"
like image 96
Hong Ooi Avatar answered Sep 17 '22 15:09

Hong Ooi