I'm trying to convert the raw RGB values from a Colour Sensor into HSL but it doesn't seem to be working:
R = rgbn[INPUT_RED];
G = rgbn[INPUT_GREEN];
B = rgbn[INPUT_BLUE];
var_R = ( R/255 );
var_G = ( G/255 );
var_B = ( B/255 );
float H = 0;
float S = 0;
float V = 0;
float var_Max = var_R;
if (var_G > var_Max) var_Max = var_G;
if (var_B > var_Max) var_Max = var_B;
float var_Min = var_R;
if (var_G > var_Min) var_Min = var_G;
if (var_B > var_Min) var_Min = var_B;
del_Max = var_Max - var_Min;
// Calculating H
if (del_Max == 0) {H=0;}
else if (var_Max = var_R) {H=(60*((var_G-var_B)/del_Max))%6;}
else if (var_Max = var_G) {H=(60*((var_B-var_R)/del_Max))+2;}
else if (var_Max = var_B) {H=(60*((var_R-var_G)/del_Max))+4;}
// Calculating S
if (var_Max == 0) {S=0;}
else if (var_Max != 0) {S=del_Max/var_Max;}
// Calculating V
V=var_Max;
NumOut(0,LCD_LINE6, H);
NumOut(0,LCD_LINE7, S);
NumOut(0,LCD_LINE8, V);
And once I've converted it to HSL how can I recognize the colour?
Here is my code that gets raw RGB data:
#define COLORSENSOR SENSOR_3
#define S_CLR IN_3
#define FLT_MAX
#define FLT_MIN
unsigned int rgbn[4];
void color_sensor_on() {
SetSensorColorFull(S_CLR);
ResetSensor(S_CLR);
}
task main(){
color_sensor_on();
while(true)
{
ReadSensorColorRaw(S_CLR, rgbn); //Raw data
RGBToHSL();
}
}
var_Max
) you should calculate L (as0.5(var_Max + var_Min)
)