Finding Pixel brightness

Hey guys,
I’m working on some code where i need to to find the color distance between the pixel i click on compared to all other pixels on the screen. By color distance, i mean that i need to find the brightness of each pixel (0-255). I have already gotten the first step done. When i click on a determined pixel, i get its brightness value. What i am struggling with is formulating the equation that gives me the color distance from the pixel i click on to all the other pixels on the screen.

if you just need the brightness distance it’s as easy as the absolute value of the difference of the two values:


if you need the color distance you can use the euclidean distance:

sqrt((r1-r2)² + (g1-g2)² + (b1-b2)²)  

the some components are perceptually more important than others so you’ll need to multiply the square differences by some weights but cannot remember which, something like:

sqrt((r1-r2)²*wr + (g1-g2)²*wg + (b1-b2)²*wb)  

as it’s a relation of one component with the other one of the weights is going to be 1.

you can also use the same equation to find the distance in other color spaces like hsv

note, if you’re just comparing distances to each other, you can skip taking the square root (in the Euclidean distance formula). Save some CPU cycles that way.