<p>Not sure what language you are using, or calls you are making before this, but:</p>

<pre><code>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;
</code></pre>

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

<pre><code>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;
</code></pre>

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

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

<pre><code>ColorSensorReadType csr;

task main(){
   csr.Port=S3;
   SetSensorColorFull(csr.Port);
   while(true){
       SysColorSensorRead(csr);
       RGBToHSL();
   }
}  
</code></pre>

<p>And the first few lines of your RGBToHSL call to this:</p>

<pre><code>R = csr.NormalizedArray[0];
G = csr.NormalizedArray[1];
B = csr.NormalizedArray[2];
</code></pre>

<p>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.</p>

<p>After this, you'll need to find the color values you're using with a tool like <a href="http://www.colorpicker.com/" rel="nofollow">http://www.colorpicker.com/</a> . 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.</p>

CC BY-SA 3.0
2015-04-01T06:50:41.317Z
I am using NXC language. Thanks for searched bug in my code. I forgot to mention that the raw value is greater than 255, maybe I had to divided it with 1024, because I read somewhere that these values increase in 1024.
Timbelion
CC BY-SA 3.0
2015-04-01T07:07:16.563Z
Should my variables to be float?
Timbelion
CC BY-SA 3.0
2015-04-01T07:14:41.333Z
When I cross the red color my values are: H35 S0.04 L0.80. Whats wrong??
Timbelion
CC BY-SA 3.0
2015-04-01T13:13:07.163Z
Hue looks good. Varianles should be float.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-01T13:16:46.847Z
Verify that the coloe sensor is in the right spot (about 1.5 studs away) and nothing like direct sunlight is hitting it.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-01T14:19:25.963Z
Still nothing !
Timbelion
CC BY-SA 3.0
2015-04-01T15:32:02.663Z
Please post the code that gets the raw data from the color sensor.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-02T07:24:15.823Z
I've post my code down.
Timbelion
CC BY-SA 3.0
2015-04-02T11:07:18.143Z
Check my most recent edits dealing with how to get better color sensor values.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-02T17:17:52.540Z
My S and L values are less then 1, H is going to 60
Timbelion
CC BY-SA 3.0
2015-04-02T23:17:54.320Z
Your code produces s and l values scaled from 0 to 1, that is what it should do.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-03T08:57:33.410Z
Yes I now that but how can I get it from 0 to 100(%)??
Timbelion
CC BY-SA 3.0
2015-04-03T09:08:47.497Z
My H and L values are wrong! S is good!
Timbelion
CC BY-SA 3.0
2015-04-03T10:31:23.387Z
Just multiply each by 100.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-06T18:12:19.840Z
Can you sand me HSV range for red and orange color that sensor can't mix.
Timbelion
CC BY-SA 3.0
2015-04-06T21:31:46.427Z
No; it's hardware specific. Make a program that prints out the HSV value that the sensor sees, scan the cube by hand, take note of the color, and make thresholds based on that.
Thaddeus Hughes
CC BY-SA 3.0
2015-04-11T11:40:19.533Z
values of red and orange are almost the same!!
Timbelion
CC BY-SA 3.0
2015-04-11T16:00:12.947Z
Yes; they aren't too far apart. If you find that problematic, use RGB.
Thaddeus Hughes
CC BY-SA 3.0
2015-03-31T22:53:25.080Z
2015-04-02T11:06:25.547Z
2015-04-02T11:06:25.547Z
Thaddeus Hughes
<p>I'm trying to convert the raw RGB values from a Colour Sensor into HSL but it doesn't seem to be working:</p>

<pre><code>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);
</code></pre>

<p>And once I've converted it to HSL how can I recognize the colour?</p>

<p>Here is my code that gets raw RGB data:</p>

<pre><code>#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();
  }
}
</code></pre>

CC BY-SA 3.0
2015-03-31T22:58:21.023Z
While I think [Thaddeus Hughes](http://bricks.stackexchange.com/a/6183/56) has the answer it might help if you could also post some sample inputs and outputs (i.e. "for R255, G128, B0 I'm getting H0, S0, V0", etc.) - are you getting an HSV of 0? or some wildly different values
Zhaph - Ben Duguid
CC BY-SA 3.0
2015-03-31T23:05:47.410Z
Also, note that [HSV and HSL are not quite the same](http://en.wikipedia.org/wiki/HSL_and_HSV), and instead of calculating V (as `var_Max`) you should calculate L (as `0.5(var_Max + var_Min)`)
Zhaph - Ben Duguid
CC BY-SA 3.0
2015-04-01T17:39:26.687Z
I'm voting to close this question as off-topic because this belongs on http://codereview.stackexchange.com/
Nick2253
CC BY-SA 3.0
2015-04-01T20:46:59.273Z
@Nick2253 - I think this would be off topic at Code Review: [They explicitly don't want](http://codereview.stackexchange.com/help/on-topic) questions about "Troubleshooting, debugging, or understanding code snippets." - they go on to require that code samples in questions "work as intended" (but need improvement). It's more on-topic at StackOverflow (I have some code and it doesn't work), but we should be able to answer this here ;)
Zhaph - Ben Duguid
<nxt><programming><nxc><sensor-colour>
Raw RGB data to Color?
Timbelion
# Raw RGB data to Color?

(Asked by: username_0 on 2015-03-31)

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();
  }
}

**Tags:** nxt, programming, nxc, sensor-colour

> **Comments:**
> **username_1**: While I think [username_3](http://bricks.stackexchange.com/a/6183/56) has the answer it might help if you could also post some sample inputs and outputs (i.e. "for R255, G128, B0 I'm getting H0, S0, V0", etc.) - are you getting an HSV of 0? or some wildly different values  
> **username_1**: Also, note that [HSV and HSL are not quite the same](http://en.wikipedia.org/wiki/HSL_and_HSV), and instead of calculating V (as `var_Max`) you should calculate L (as `0.5(var_Max + var_Min)`)  
> **username_2**: I'm voting to close this question as off-topic because this belongs on http://codereview.stackexchange.com/  
> **username_1**: @username_2 - I think this would be off topic at Code Review: [They explicitly don't want](http://codereview.stackexchange.com/help/on-topic) questions about "Troubleshooting, debugging, or understanding code snippets." - they go on to require that code samples in questions "work as intended" (but need improvement). It's more on-topic at StackOverflow (I have some code and it doesn't work), but we should be able to answer this here ;)  

---

## 1 answers

### Answer by username_3 on 2015-03-31. Score: 2 | Accepted answer

Not sure what language you are using, or calls you are making before this, but:
float var_Min = var_R; if (var_G > var_Min) var_Min = var_G; if (var_B > var_Min) var_Min = var_B;

Should be (note the use of < rather than >)
float var_Min = var_R; if (var_G < var_Min) var_Min = var_G; if (var_B < 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<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.

> **Comments:**
> **username_0**: I am using NXC language. Thanks for searched bug in my code. I forgot to mention that the raw value is greater than 255, maybe I had to divided it with 1024, because I read somewhere that these values increase in 1024.  
> **username_0**: Should my variables to be float?  
> **username_0**: When I cross the red color my values are: H35 S0.04 L0.80. Whats wrong??  
> **username_3**: Hue looks good. Varianles should be float.  
> **username_3**: Verify that the coloe sensor is in the right spot (about 1.5 studs away) and nothing like direct sunlight is hitting it.  
> **username_0**: Still nothing !  
> **username_3**: Please post the code that gets the raw data from the color sensor.  
> **username_0**: I've post my code down.  
> **username_3**: Check my most recent edits dealing with how to get better color sensor values.  
> **username_0**: My S and L values are less then 1, H is going to 60  
> **username_3**: Your code produces s and l values scaled from 0 to 1, that is what it should do.  
> **username_0**: Yes I now that but how can I get it from 0 to 100(%)??  
> **username_0**: My H and L values are wrong! S is good!  
> **username_3**: Just multiply each by 100.  
> **username_0**: Can you sand me HSV range for red and orange color that sensor can't mix.  
> **username_3**: No; it's hardware specific. Make a program that prints out the HSV value that the sensor sees, scan the cube by hand, take note of the color, and make thresholds based on that.  
> **username_0**: values of red and orange are almost the same!!  
> **username_3**: Yes; they aren't too far apart. If you find that problematic, use RGB.  

---

