For example: "#FF8000".
How could I convert it to RGBColor or Hue?
For example: "#FF8000".
How could I convert it to RGBColor or Hue?
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.) & Starting from version 10.1 you can use RGBColor directly:
RGBColor["#FF8000"] (* RGBColor[1., 0.5019607843137255, 0.] *) RGBColor["#45A"] (* RGBColor[0.26666666666666666`, 0.3333333333333333, 0.6666666666666666] *) ToColor[RGBColor["#FF8000"], Hue] (* Hue[0.08366013071895424, 1., 1.] *) Function that converts string to a list of 3 numbers: for R, G and B component:
toRGBSequence[i_] := Composition[FromDigits[#, 16] &, StringJoin] /@ Partition[Characters[StringDrop[i, 1]], 2] /. List -> Sequence; # sign.Characters function.Sequence to use with RGBColor function.Usage:
RGBColor[toRGBSequence["#FF5500"]] PS: This may, or may not be the most accurate and fast solution.
I hate regular expressions... :)
hexColorToRGB[s_] := RGBColor[FromDigits[#, 16]/255 & /@ Flatten[ StringCases[ToLowerCase@s, {RegularExpression[ "#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})" ] -> {"$1", "$2", "$3"}, RegularExpression["#([0-9a-f])([0-9a-f])([0-9a-f])"] -> {"$1$1", "$2$2", "$3$3"}} ]]] ColorSetter /@ hexColorToRGB /@ {"#000", "#FF0000", "#0F0", "#0000FF", "#FF0", "#00FFFF", "#F0F", "#C0C0C0", "#FFF"} 
Here is another option:
hexToRGB = RGBColor[ FromDigits[#, 16]/255 & /@ StringTake[#, {{2, 3}, {4, 5}, {6, 7}}] ] &; hexToRGB@"#FF8c00" // ColorSetter 
And another:
hexToRGB = RGBColor[ FromDigits[#, 16]/255 & /@ StringCases[#, Except["#"] ~~ _] ] &; #RGB formResponding to cormullion's comment:
hexToRGB[color_String | {colors__String}] := RGBColor[FromDigits[#, 16]/255 & /@ #] & @@@ StringCases[{color, colors}, {"#" ~~ r_ ~~ g_ ~~ b_ ~~ EndOfString :> {r ~~ r, g ~~ g, b ~~ b}, "#" ~~ r : (_ ~~ _) ~~ g : (_ ~~ _) ~~ b : (_ ~~ _) :> {r, g, b}}] ColorSetter /@ hexToRGB @ {"#000", "#FF0000", "#0F0", "#0000FF", "#FF0", "#00FFFF", "#F0F", "#C0C0C0", "#FFF"} 
Operating on the entire list of color strings should be faster than one at a time.
Also
rgb[x_] := RGBColor[FromDigits[#, 2] / 255 & /@ Partition[IntegerDigits[FromDigits[StringDrop[x, 1], 16], 2, 24], 8]] rgb@"#FF5500" RGBColor[{255, 85, 0}]
Edit
Golfing a one liner :)
rgb[x_] := RGBColor[FromDigits[#, 16]/255 & /@ StringJoin /@ Partition[Rest@Characters@x, 2]] rgb[x_] := RGBColor[FromDigits["" <> # , 16]/255 & /@ Rest@Characters@x ~Partition~ 2] or fully Golfed: rgb=RGBColor[FromDigits[""<>#,16]/255&/@Rest@Characters@#~Partition~2]& $\endgroup$ In version 10
Interpreter["StructuredColor"]["#FF8000"] RGBColor[1, Rational[128, 255], 0]
You guys are too fast for me ;)
My solution which is similar to @cormullion's:
hexToRGB[hex_String] :=Module[{RGB}, RGB = StringCases[hex, RegularExpression["^#(\\w{2})(\\w{2})(\\w{2})"] -> {"$1", "$2", "$3"}] // Flatten; RGBColor[FromDigits[#, 16]/100 - 1 & /@ RGB] ] Graphics[{hexToRGB["#A4A4A4"], Disk[]}] P.S.: Things are not always just #000000 and #FFFFFF. It's mostly varying of shades of #A4A4A4 :)
EDIT
PieChart of the sector angles proportional to {R, G, B}
hexToPiechart =PieChart3D @@ {ToExpression@#/255. & /@ (StringCases[#, RegularExpression["^#(\\w{2})(\\w{2})(\\w{2})"] -> {"16^^$1", "16^^$2", "16^^$3"}])} &; hexToPiechart["#A4A4A4"] 
ColorData["WebSafe", "Panel"] (*click to get RGBColor *) 
Just to add nothing to the previous answers
toRGB[str_String]:=ToExpression["16^^" <> #] & /@ str~StringDrop~1~StringCases~Repeated[_, {2}] /. List -> RGBColor