Finding the closest match to an RGB value from an array

Hello! I am fairly new to arduino coding, and I am having some trouble.

I am using a color sensor that reads out an RGB value such as 0 255 0. I have an r, g, and b number. From this, I am trying to find a way to create an array of RGB combinations for a range of colors and find the closest match to that from the RGB value I get in.

Any suggestions are welcome! Thank you so much.

Welcome :)

Something like that?

To find the closest match to three values at the same time, you need to define "close".

One definition would be the shortest distance using Pythagoras' theorem. For two sets of values (r1,g1,b1) and (r2,g2,b2)

distance = sqrt( (r1-r2)(r1-r2) + (g1-g2)(g1-g2) + (b1-b2)*(b1-b2) );

You don't need to take the square root to find the shortest distance between two points, which saves time.

There are other possible definitions of "close".

Maybe subtract the colors to find the differences..?

diff = ABS(R1-R2) + ABS(G1-G2) + ABS(B1-B2);

Now you at least have something you can sort with.

Actually, this sounds like a fun experiment. And if you have something to display your color results, you could test out different algorithms.

-jim lee

You could define “close” to mean “nearly the same hue”. Find a HSB<->RGB algorithm on the net and use it on your RGB values to get the hue.