1
$\begingroup$

I want to build a function which will work like this website: http://imagecolorpicker.com/

I'll upload the picture and get the hex or RGB color code of the color on the image, how is it possible?

For example I have this image I know, it's hex code is #FF5733, how can I make Mathmatica define color for me?

$\endgroup$
3
  • $\begingroup$ If Mathmatica doesn't work with Hex colors, can I do same stuff with RGB colors? $\endgroup$ Commented Jan 17, 2017 at 20:35
  • $\begingroup$ What that site does correspond to PixelValue[image, pos] in wolfram $\endgroup$
    – Coolwater
    Commented Jan 17, 2017 at 20:57
  • $\begingroup$ Note that if you load an image into Mathematica and then right-click on it, you'll see "Get Pixel Color" among the options. $\endgroup$ Commented Jan 18, 2017 at 19:12

1 Answer 1

4
$\begingroup$

Import image:

img = Import["https://i.sstatic.net/dApi1.png"]

Assuming all pixels are the same, grab the RGB data from the first one:

id = ImageData[img][[1, 1]]*255

Hexify:

StringJoin["#", 
 StringPadLeft[IntegerString[Round[id], 16], 2, "0"]]

"#ff5733"

Wrap it in a function (this time finding "commonest" pixel)...

gethex[img_?ImageQ] := 
 Module[{id = Round[Commonest[Join @@ ImageData[img]][[1]]*255]},
  StringJoin["#", StringPadLeft[IntegerString[Round[id], 16], 2, "0"]]
 ]
$\endgroup$
4
  • $\begingroup$ Another RGB -> hex converter can be found here. $\endgroup$ Commented Jan 18, 2017 at 10:30
  • $\begingroup$ @J.M. And a much more elegant and unrushed version as well... $\endgroup$
    – kale
    Commented Jan 18, 2017 at 16:56
  • $\begingroup$ Can I get RGB data automatically from the image by using Mathmatica? $\endgroup$ Commented Jan 18, 2017 at 20:39
  • $\begingroup$ @ArtemAnisimov, that's what the second step (id) does above. $\endgroup$
    – kale
    Commented Jan 19, 2017 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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