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?

  • $\begingroup$ If Mathmatica doesn't work with Hex colors, can I do same stuff with RGB colors? $\endgroup$ Jan 17 '17 at 20:35
  • $\begingroup$ What that site does correspond to PixelValue[image, pos] in wolfram $\endgroup$
    – Coolwater
    Jan 17 '17 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$
    – J. M.'s torpor
    Jan 18 '17 at 19:12

Import image:

img = Import["https://i.stack.imgur.com/dApi1.png"]

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

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


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


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"]]
  • $\begingroup$ Another RGB -> hex converter can be found here. $\endgroup$
    – J. M.'s torpor
    Jan 18 '17 at 10:30
  • $\begingroup$ @J.M. And a much more elegant and unrushed version as well... $\endgroup$
    – kale
    Jan 18 '17 at 16:56
  • $\begingroup$ Can I get RGB data automatically from the image by using Mathmatica? $\endgroup$ Jan 18 '17 at 20:39
  • $\begingroup$ @ArtemAnisimov, that's what the second step (id) does above. $\endgroup$
    – kale
    Jan 19 '17 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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