Hi,
I bought an Arduino Uno a few days ago and i'm very new to C/C++ programming. I'm trying to make a function that transforms HTML color code to RGB565 color code.
My function is called RGB888toRGB565.
For example: RGB888toRGB565(FFFFFF); should return 0xFFFF.
Or if you prefer, I'm trying to convert 24 bit color code (RGB888) to 16bit color code (RGB565).
So far i've done this:
int RGB888toRGB565 (String RGB888)
{
int RGB565;
String r,g,b;
char rc[3];
char gc[3];
char bc[3];
unsigned int R,G,B;
//Separamos cada canal de color en una variable tipo String para poder usar .substring()
r = RGB888.substring(0,2);
g = RGB888.substring(2,4);
b = RGB888.substring(4,6);
//Ahora lo convertimos a char para poder usarlo con strtoul()
r.toCharArray(rc,3);
R = strtoul(rc, NULL, 16);
g.toCharArray(gc,3);
G = strtoul(gc, NULL, 16);
b.toCharArray(bc,3);
B = strtoul(bc, NULL, 16);
//Dividimos para convertir el formato RGB888 a RGB565
R = (R / 5);
G = (G / 6);
B = (B / 5);
//???
return(RGB565);
}
I think the function so far it's not very hard to understand. I get the HTML/RGB888 color code, for example FF11EE and I split the color channels into variables. FF to r, 11 to g, and EE to b. Later I have to convert the String to a Char so I can use strtoul() wich will transform my ASCII hex code into real Hex values. Once we have the values in unsigned ints (R,G,B), we divide them by the bits per channel of the RGB565 format, so R/5, G/6, and B/5.
And right there i'm blocked. Now I would need to somehow put R,G and B together in a var called RGB565 and return it. It will be 16 bit long so unsigned int for RGB565 should work.
Thanks in advance