Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Hex to RGB converter

Tags:

r

Suppose I have this color in HEX values (including alpha):

x <- "#FF2400FF"

Is there a neat package to convert HEX values to RGB values in R? Or a simple bit of code to do that?

[#1] c("36 0 255")

Edit: This is the opposite way of RGB to Hex converter question

like image 777
juanmah Avatar asked May 11 '17 09:05

juanmah


People also ask

How do I convert hex to RGB?

Hex to RGB conversionGet the 2 left digits of the hex color code and convert to decimal value to get the red color level. Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level.

What color is 0xffff0000?

For instance, the color int representation of opaque red is 0xffff0000 .


1 Answers

Based on the comments already given, you can use this code:

x <- "#FF2400FF"
paste(as.vector(col2rgb(x)), collapse = " ")
#> [1] "255 36 0"

However, looking at your requested result, it seems that you have the alpha-value as first hex-number in your x - so you need to create a substring:

x <- "#FF2400FF"
paste(as.vector(col2rgb(paste0("#", substr(x, 4, 10)))), collapse = " ")
#> [1] "36 0 255"
like image 161
Daniel Avatar answered Sep 23 '22 20:09

Daniel