Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

For example: "#FF8000".

How could I convert it to RGBColor or Hue?

share|improve this question
2  
Incidentally, this site, among many others, also uses three character color codes (#888). Should answers accept both forms? –  cormullion Jan 26 '13 at 17:53
add comment

9 Answers

up vote 20 down vote accepted

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.) &
share|improve this answer
    
That is a particularly clever solution... –  Stefan Jan 26 '13 at 11:10
1  
Why the "3" in the golfed version? –  belisarius Jan 26 '13 at 11:39
1  
@belisarius, you need to pad to 3 digits in case the red component is zero –  Simon Woods Jan 26 '13 at 11:52
1  
Again we think alike. You're even using infix! FWIW when using infix I recommend the spacing that I just edited your post to include. I think it's much more readable that way, and I can tell you from experience that a lot of people find it hard to read anyway. –  Mr.Wizard Jan 26 '13 at 17:13
1  
@Mr.Wizard, thanks for the edit - I agree it's more readable like that. –  Simon Woods Jan 26 '13 at 18:06
show 2 more comments

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;
  • First, dropping the # sign.
  • Converting to a list, using the Characters function.
  • Partitioning in groups of 2.
  • Composition of function to first join the two letters and than convert it to a base-10 format.
  • Map over a list.
  • Converting everything to Sequence to use with RGBColor function.

Usage:

RGBColor[toRGBSequence["#FF5500"]]

PS: This may, or may not be the most accurate and fast solution.

share|improve this answer
1  
+1 it's more beautiful than mine is, that's for sure :) –  cormullion Jan 26 '13 at 10:23
add comment

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"}

test

share|improve this answer
add comment

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]]
share|improve this answer
    
You need to work on your swing: rgb[x_] := RGBColor[FromDigits["" <> # , 16]/255 & /@ Rest@Characters@x ~Partition~ 2] or fully Golfed: rgb=RGBColor[FromDigits[""<>#,16]/255&/@Rest@Characters@#~Partition~2]& –  Mr.Wizard Jan 26 '13 at 17:32
    
@Mr.Wizard Good one :). I wasn't golfing seriously, just trying to fit it in one line. Simon's answer is far better, anyway –  belisarius Jan 26 '13 at 17:37
    
Yeah, I'm pretty miffed he got that one before me. :o) –  Mr.Wizard Jan 26 '13 at 17:44
add comment

Borrowing from amr's answer here

ToExpression["16^^" <> #] & /@ 
   Partition[Characters@StringTrim[#, "#"], 2] & /@ {"FFFFFF", 
  "#FFFFFF", "000000", "#FF5500", "#005500"}

=>{{255, 255, 255}, {255, 255, 255}, {0, 0, 0}, {255, 85, 0}, {0, 85, 0}}

share|improve this answer
add comment

You guys are to fast for me ;)

My solution which is similar to's @cormullion:

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 a 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"]

enter image description here

share|improve this answer
add comment
 ColorData["WebSafe", "Panel"] (*click to get RGBColor *)

enter image description here

share|improve this answer
add comment

Here is another option:

hexToRGB =
  RGBColor[ FromDigits[#, 16]/255 & /@ StringTake[#, {{2, 3}, {4, 5}, {6, 7}}] ] &;

hexToRGB@"#FF8c00" // ColorSetter

Mathematica graphics

And another:

hexToRGB =
  RGBColor[ FromDigits[#, 16]/255 & /@ StringCases[#, Except["#"] ~~ _] ] &;

#RGB form

Responding 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"}

Mathematica graphics

Operating on the entire list of color strings should be faster than one at a time.

share|improve this answer
    
I really like the second one... –  Simon Woods Jan 26 '13 at 18:07
add comment

Just to add nothing to the previous answers

toRGB[str_String]:=ToExpression["16^^" <> #] & /@ 
  str~StringDrop~1~StringCases~Repeated[_, {2}] /. 
 List -> RGBColor
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.