RGB to "color" (green , white etc)

I read the color of an object and it returns the RGB code.
With that , I want to draw a rectangle with background in that color but
the code for this use 'color' , like ASD.background("Red")

So , when I read 255,0,0 , I want to draw background in Red.

How to convert this RGB into "color" (where "color" is any color , green , white , blue etc.)

Sorry if this is a not soo smart question.

Henk.

it returns the RGB code

that's not enough for us to know what you get from a data representation perspective. 3 bytes or one uint32_t ?

ASD.background

what is ASD ? we don't know the components nor the library you use

Thanks J-M-L.

The sensor I use return 3 bytes for each basic R G B colors. f.e. 123 , 12 , 200
ASD.background or ASD.FillArea are a commands in the SerialDraw.h library

Does this helps ?

Henk.

What's your display?

Looking at the library, I find it a very very bad idea to use the String class to store colors...

String colors[] = {
  "black",  "white", "cyan", "darkgray", "gray", "green", "lightgray",  "magenta", "orange",
  "pink", "red", "blue", "yellow", "darkgreen"
};

if you are stuck with this library, then you'll have to make do - but if you can get another library that probably would be a good idea...

My display is a window produced by Java.
Do you have a suggestion to use which library I could use to draw a rectangle ( or circle) with a
background in the correct color ?
I'm not aware of any possibility to draw directly on my computer screen. Is it possible ?

Henk.

You can see how the library draws using serial, filling an area is as simple as:

#define COLOR_FIRST 1
#define COLOR_LAST 14

byte color_current = 1;

void loop()
{
  //fillArea(10, 10, 100, 100, color)
  Serial.print("a,");
  Serial.print(10);
  Serial.print(",");
  Serial.print(10);
  Serial.print(",");
  Serial.print(100);
  Serial.print(",");
  Serial.print(100);
  Serial.print(",");
  Serial.println(color_current);
  if (++color_current > COLOR_LAST) color_current = COLOR_FIRST;
  delay(1000);
}

My display is a window produced by Java.

Would have been interesting to have more details.

in any case, if you are stuck with this communication channel, then it seems you do need to send a string over the serial line for the color and can't send numbers - so you need to map the RGB values you read from the sensor to the "closest" color name.

The color names have conventional (R,G,B) definitions, could be something like this

black		(0,0,0)
white		(255,255,255)
cyan		(0,255,255)
darkgray	(169,169,169)
gray		(128,128,128)
green		(0,255,0)
lightgray	(211,211,211)
magenta 	(255,0,255)
orange 		(255,165,0)
pink		(255,192,203)
red		(255,0,0)
blue		(0,0,255)
yellow 		(255,255,0)
darkgreen	(0,100,0)

So now you know what RGB values are associated with the names and you need to calculate the distance between (R1, G1, B1) which is what you got from your sensor and (R2, G2, B2) which will take all the possible values in the table and find the minimum distance. The (R2, G2, B2) which gave you that minimum distance will be the name to use.

Measuring a distance between two RGB colors is not as trivial as it may sound. There are many possibilities, you can read about it in wikipedia for example.

I'd suggest you pick a formula in there. Either the weighted approach (red 30%, green 59%, and blue 11%)

d = sqrt(((R[sub]2[/sub]-R[sub]1[/sub])*0.3)^2 + ((G[sub]2[/sub]-G[sub]1[/sub])*0.59)^2 + ((B[sub]2[/sub]-B[sub]1[/sub])*0.11)^2)

but the better one is probably the more complex one

and since you're not really interested in the actual d value, you can skip the square root

create an 2D array with the RGB values in the same order as their color name array

String colors[] = {
  "black",  "white", "cyan", "darkgray", "gray", "green", "lightgray",  "magenta", "orange",
  "pink", "red", "blue", "yellow", "darkgreen"
};

write a distance function that takes two (RGB) triplets and return the distance as described above
write a for loop going through the array and find the index of the shortest distance
use this index to decide what string to send.

J-M-L

Here I found how to make a window with Java :

Ok.. I would use Processing to make something custom on the PC side that would be more tailored to my needs and minimize the serial communication.. your pc is way faster than the arduino yet this solution has the arduino doing all the heavy lifting... it does not make sense to me.

That being said, the algorithm exposed above should not be difficult to write on the arduino. Give it a try.

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