# Convert PIXEL(…) integer value to CSS Hex

Hi Keyboard Maestros,

I'm currently trying to extract the color of a pixel on my screen and the final result should be a CSS Hex value.

Currently, I am this far:

PIXEL(Red,300,300)
PIXEL(Green,300,300)
PIXEL(Blue,300,300)

This leaves me with three integer values, which I would like to calculate to end up with one CSS Hex value. Is that possible or do I need to approach this problem differently?

Cheers!

I don't think your problem will difficult to solve, but I'm a little fuzzy on something here.

You are showing the Pixel function in KM in your sample code below?

https://wiki.keyboardmaestro.com/function/PIXEL

The doc says it returns a Real Number between 0 and 1. Which might make sense because different monitors/video cards have different maximum values for a white pixel. Some have 256, and some have 1024, I believe. So my guess is that to get the Real Number down to an integer you may have to multiple it and Round the result.

But that's only half the problem. You also have to convert it to a CSS Hex value. I've never seen a CSS Hex so I'm not sure what it looks like, but I'm guessing you can create it by using this feature of KM. Look at the very bottom of Peter's post here:

I suspect that's the second thing you will have to do.

If these hints don't help, please reply. I guess I was too lazy to look up CSS Hex and write the code for you.

This should work:

%Hex2%PIXEL(Red,300,300)*255%%Hex2%PIXEL(Green,300,300)*255%%Hex2%PIXEL(Blue,300,300)*255%
1 Like

Thank you so much, that's great!

I wasn't aware of the %Hex%-function but will certainly utilise it in the future. Great stuff!

1 Like

Peter, is there some sort of reverse-Hex way to read a hex (oct, bin, etc) value into an integer?

The HEX function does that.

How did I miss that all these years? Thanks.

By never typing "Hex" in to the search field in the Keyboard Maestro editor I'd guess:

1 Like