Code implementation for classifying colors according to the RGB range

Hi!
I am interested in making a device that knows how to classify colors and for that I use the TCS3200 module. I see tables like the ones I will attach below and I assume that the RGB values I will get from the TCS3200 will not be exactly as in the table but similar to them for the obvious reason that each object has a slightly different shade (purple a little darker, purple A little brighter, etc...).
My question is, is it possible to define a range of values for each particular color?
If there is a way how can it be implemented in code?

Guess there are quite a few people here who have come across this question...

You could calculate a 'distance' between the readings and each of the named colors to find the closest.

size_t FindNearestColor( int R, int G, int B)
{
  size_t nearestIndex = 0;
  uint32_t nearestDistance = 255 * 255 * 255;
  for (size_t i = 0; i < NumberOfNamedColors; i++)
  {
    uint32_t deltaR = NamedColor[i].red - R;
    uint32_t deltaG = NamedColor[i].green - G;
    uint32_t deltaB = NamedColor[i].blue - B;

    uint32_t distance = (deltaR * deltaR) 
       + (deltaG * deltaG) + (deltaB * deltaB);

    // Actual distance is sqrt(distance) but we are only 
    // comparing them so the extra sqrt() doesn't change
    // anything.

    if (distance < nearestDistance)
    {
      nearestDistance = distance;
      nearestIndex = i;
    }
  }
  return nearestIndex;
}

Thank you! Very nice idea! :sunglasses:

?Why was this value chosen in order to subtract from it the read value of the RGB values

I was in a hurry. I should have used "3 * (255ul*255ul)" which is the largest possible distance. The distance between black (0,0,0) and white (255,255,255).

!Now I understand! Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.