Raw RGB data to Color? 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 &gt; var_Max) var_Max = var_G; 
if (var_B &gt; var_Max) var_Max = var_B;

float var_Min = var_R;
if (var_G &gt; var_Min) var_Min = var_G;
if (var_B &gt; 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();
  }
}
Not sure what language you are using, or calls you are making before this, but:

float var_Min = var_R; if (var_G &gt; var_Min) var_Min = var_G; if (var_B &gt; var_Min) var_Min = var_B;


Should be (note the use of &lt; rather than >)

float var_Min = var_R; if (var_G &lt; var_Min) var_Min = var_G; if (var_B &lt; var_Min) var_Min = var_B;


Your posted code is putting the maximum value into the minimum variable, messing up things down the line...

Another thing that will help is to not use the RawColorSensor call. Change your main task to this:

ColorSensorReadType csr;

task main(){
   csr.Port=S3;
   SetSensorColorFull(csr.Port);
   while(true){
       SysColorSensorRead(csr);
       RGBToHSL();
   }
}  


And the first few lines of your RGBToHSL call to this:

R = csr.NormalizedArray[0];
G = csr.NormalizedArray[1];
B = csr.NormalizedArray[2];


This will normalize the values between the values of 0,255 (or at least roughly). I've had more luck with this call since it also tries to calibrate the values.

After this, you'll need to find the color values you're using with a tool like http://www.colorpicker.com/ . For example, orange would have HSV (HSB on that site) of (40,100,100). (The scaling on these values may differ). So, to detect orange, check if:
Hue>30 AND Hue&lt;50 AND saturation>90 AND value>90.
Make sure there is play with your thresholds, otherwise the check will work once in a hundred tries.