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$
  • $\begingroup$ If Mathmatica doesn't work with Hex colors, can I do same stuff with RGB colors? $\endgroup$ – Artem Anisimov 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. is away Jan 18 '17 at 19:12
4
$\begingroup$

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

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$
  • $\begingroup$ Another RGB -> hex converter can be found here. $\endgroup$ – J. M. is away 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$ – Artem Anisimov 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.