Skip to main content

How to convert a hex color string to RGBColor?



For example: "#FF8000".


How could I convert it to RGBColor or Hue?



Answer



Using IntegerDigits to convert directly to base 256:


hexToRGB = RGBColor @@ (IntegerDigits[
ToExpression@StringReplace[#, "#" -> "16^^"], 256, 3]/255.) &

hexToRGB["#FF8000"]
(* RGBColor[1., 0.501961, 0.] *)


Edit


Shorter version, since somebody mentioned golfing...


hexToRGB = RGBColor @@ (IntegerDigits[# ~StringDrop~ 1 ~FromDigits~ 16, 256, 3]/255.) &

Comments