Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T06:50:41.317Z
Answers.Text: 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.
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T07:07:16.563Z
Answers.Text: Should my variables to be float?
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T07:14:41.333Z
Answers.Text: When I cross the red color my values are: H35 S0.04 L0.80. Whats wrong??
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T13:13:07.163Z
Answers.Text: Hue looks good. Varianles should be float.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T13:16:46.847Z
Answers.Text: Verify that the coloe sensor is in the right spot (about 1.5 studs away) and nothing like direct sunlight is hitting it.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T14:19:25.963Z
Answers.Text: Still nothing !
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-01T15:32:02.663Z
Answers.Text: Please post the code that gets the raw data from the color sensor.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T07:24:15.823Z
Answers.Text: I've post my code down.
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T11:07:18.143Z
Answers.Text: Check my most recent edits dealing with how to get better color sensor values.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T17:17:52.540Z
Answers.Text: My S and L values are less then 1, H is going to 60
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-02T23:17:54.320Z
Answers.Text: Your code produces s and l values scaled from 0 to 1, that is what it should do.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-03T08:57:33.410Z
Answers.Text: Yes I now that but how can I get it from 0 to 100(%)??
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-03T09:08:47.497Z
Answers.Text: My H and L values are wrong! S is good!
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-03T10:31:23.387Z
Answers.Text: Just multiply each by 100.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-06T18:12:19.840Z
Answers.Text: Can you sand me HSV range for red and orange color that sensor can't mix.
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-06T21:31:46.427Z
Answers.Text: 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.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T11:40:19.533Z
Answers.Text: values of red and orange are almost the same!!
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T16:00:12.947Z
Answers.Text: Yes; they aren't too far apart. If you find that problematic, use RGB.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-31T22:53:25.080Z
Answers.LastActivityDate: 2015-04-02T11:06:25.547Z
Answers.LastEditDate: 2015-04-02T11:06:25.547Z
Answers.UserDisplayName: Thaddeus Hughes
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-31T22:58:21.023Z
Comments.Text: 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
Comments.UserDisplayName: Zhaph - Ben Duguid
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-31T23:05:47.410Z
Comments.Text: 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)`)
Comments.UserDisplayName: Zhaph - Ben Duguid
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-01T17:39:26.687Z
Comments.Text: I'm voting to close this question as off-topic because this belongs on http://codereview.stackexchange.com/
Comments.UserDisplayName: Nick2253
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-01T20:46:59.273Z
Comments.Text: @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 ;)
Comments.UserDisplayName: Zhaph - Ben Duguid
Tags: <nxt><programming><nxc><sensor-colour>
Title: Raw RGB data to Color?
UserDisplayName: Timbelion
ThreadText: # 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 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.  
  
  ---
  
  