Answers.Body: <p>Since you are trying to solve a Rubik's cube, check out the <a href="http://mindcuber.com/mindcuber/mindcuber-source.html" rel="nofollow">source code for the MindCuber</a>. The solution there is that the values are not actually converted into colors. Rather the raw values are converted to HSV and then sorted by hue. The actual value does not matter, just how close it is to the other values.</p>
  
  <p>The Mindcuber also has a white calibration. This is used to identify the white values and removed them from the list before sorting which is useful since white doesn't have a meaningful hue value.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T18:14:56.267Z
Answers.LastActivityDate: 2015-06-23T18:14:56.267Z
Answers.UserDisplayName: David Lechner
Answers.Body: <p>The following works if you just want the builtin six/seven colors, without any calibration.</p>
  
  <pre><code>task main() {
    SetColorSensorFull(S3);
    while(true){
      int value=Sensor(S3);
      // value now contains 1 for black, 2 for blue, 3 for green, 4 for yello, 5 for red, 6 for white, and something else for an unknown color.
    }
  </code></pre>
  
  <p>However, if you want to do proportional control, want more colors, or aren't satisfied with the builtin thresholds, you'll need to use the RGB or HSL values. Here's RGB (Threshold is of course, my own defined struct and my solution to this problem):</p>
  
  <pre><code>struct Threshold{
    int minR, minG, minB;
    unsigned int maxR, maxG, maxB;
  };
  
  bool InRange(ColorSensorReadType &amp;csr, Threshold &amp;thresh){
    bool in=true;
    in&amp;=csr.NormalizedArray[0]&lt;=thresh.maxR;
    in&amp;=csr.NormalizedArray[1]&lt;=thresh.maxG;
    in&amp;=csr.NormalizedArray[2]&lt;=thresh.maxB;
    in&amp;=csr.NormalizedArray[0]&gt;=thresh.minR;
    in&amp;=csr.NormalizedArray[1]&gt;=thresh.minG;
    in&amp;=csr.NormalizedArray[2]&gt;=thresh.minB;
    return in;
  }
  
  Threshold Red={180,20,20,250,130,130},
  Green={90,160,90,160,255,160},
  Blue={40,100,160,80,150,200},
  White={210,210,210,300,300,300},
  GroundBlack={0,0,0,150,150,150},
  GroundGreen={120,180,120,170,220,160};
  
  task main(){
    SetSensorColorFull(S2);
    ColorSensorReadType csr;
    csr.Port=S2;
  
    while(true){
      SysColorSensorRead(csr);
      ClearScreen();
      NumOut(0,LCD_LINE1,csr.NormalizedArray[0]);
      NumOut(0,LCD_LINE2,csr.NormalizedArray[1]);
      NumOut(0,LCD_LINE3,csr.NormalizedArray[2]);
  
      if(InRange(csr,Blue)){
        TextOut(0,LCD_LINE5,"BLUE!");
      }else if(InRange(csr,Green)){
        TextOut(0,LCD_LINE5,"GREEN!");
      }else if(InRange(csr,White)){
        TextOut(0,LCD_LINE5,"WHITE!");
      }
  
      Wait(10);
    }
  }
  </code></pre>
  
  <p>If you want to transform into HSV values, try this:</p>
  
  <pre><code>struct Threshold{
    int minR, minG, minB;
    unsigned int maxR, maxG, maxB;
  };
  
  struct HSVThreshold{
    int minH, minS, minV;
    unsigned int maxH, maxS, maxV;
  };
  
  struct HSVColor{
    int hue, sat, val;
  }
  void RGBToHSV(ColorSensorReadType &amp;csr, HSVColor &amp;color){
    int maxColor=csr.NormalizedArray[0], minColor=csr.NormalizedArray[0];
    for (int i=1;i&lt;3;i++) {
      if (maxColor&lt;csr.NormalizedArray[i]) maxColor = csr.NormalizedArray[i];
      if (minColor&gt;csr.NormalizedArray[i]) minColor = csr.NormalizedArray[i];
    }
    if (minColor==maxColor) {
      color.val=minColor;
      color.sat=0;
      color.hue=0;
    } else {
      int d = (r==minColor) ? g-b : ((b==minColor) ? r-g : b-r);
      int h = (r==minColor) ? 3 : ((b==minColor) ? 1 : 5);
      color.hue=60*(h-d/(maxColor-minColor));
      color.sat=(maxColor-minColor)/maxColor;
      color.val=maxColor;
    }
  }
  
  bool InRange(ColorSensorReadType &amp;csr, Threshold &amp;thresh){
    bool in=true;
    in&amp;=csr.NormalizedArray[0]&lt;=thresh.maxR;
    in&amp;=csr.NormalizedArray[1]&lt;=thresh.maxG;
    in&amp;=csr.NormalizedArray[2]&lt;=thresh.maxB;
    in&amp;=csr.NormalizedArray[0]&gt;=thresh.minR;
    in&amp;=csr.NormalizedArray[1]&gt;=thresh.minG;
    in&amp;=csr.NormalizedArray[2]&gt;=thresh.minB;
    return in;
  }
  bool InRange(HSVColor color, HSVThreshold &amp;thresh){
    bool in=true;
    in&amp;=color.hue&lt;=thresh.maxH;
    in&amp;=color.sat&lt;=thresh.maxS;
    in&amp;=color.val&lt;=thresh.maxV;
    in&amp;=color.hue&gt;=thresh.minH;
    in&amp;=color.sat&gt;=thresh.minS;
    in&amp;=color.val&gt;=thresh.minV;
    return in;
  }
  
  Threshold Red={180,20,20,250,130,130},
  Green={90,160,90,160,255,160},
  Blue={40,100,160,80,150,200},
  White={210,210,210,300,300,300},
  GroundBlack={0,0,0,150,150,150},
  GroundGreen={120,180,120,170,220,160};
  
  // Define your own HSVThresholds here
  
  task main(){
    SetSensorColorFull(S2);
    ColorSensorReadType csr;
    csr.Port=S2;
  
    while(true){
      SysColorSensorRead(csr);
      HSVColor color;
      RGBToHSV(csr,color);
  
      ClearScreen();
      NumOut(0,LCD_LINE1,color.hue);
      NumOut(0,LCD_LINE2,color.sat);
      NumOut(0,LCD_LINE3,color.val);
  
      Wait(10);
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-31T20:01:56.117Z
Answers.Text: Currently I am working Lego robot that solves 2x2x2 Rubik's cube, Thanks!
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-31T20:55:57.453Z
Answers.Text: You can add more thresholds and tweak them in order to get even more colors.
Answers.UserDisplayName: Thaddeus Hughes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-31T13:47:50.080Z
Answers.LastActivityDate: 2015-03-31T13:47:50.080Z
Answers.UserDisplayName: Thaddeus Hughes
Answers.Body: <p>I'm not entirely sure if you need the raw data, but if you're just looking to read the different colour values, there is a much easier way.</p>
  
  <p>To turn on and set up the sensor, use <code>SetSensorColorFull(S3);</code>. Then to retrieve the value just use <code>Sensor(S3)</code>. The value returned will be an integer from 1 to 6, corresponding to the following colour values:</p>
  
  <pre><code>1 = black
  2 = blue
  3 = green
  4 = yellow
  5 = red
  6 = white
  </code></pre>
  
  <p>Here is a sample program that will read the current value from the colour sensor and print the colour name on the NXT screen:</p>
  
  
  
  <pre><code>task main() {
      string sColor;
      int nColor;
  
      // Turn on the sensor
      SetSensorColorFull(S3);
  
      while (1) {
  
          // Read the numerical color value from the sensor
          nColor = Sensor(S3);
  
          // Determine the color name from its number
          switch(nColor) {
              case 1: sColor = "Black";
              case 2: sColor = "Blue";
              case 3: sColor = "Green";
              case 4: sColor = "Yellow";
              case 5: sColor = "Red";
              case 6: sColor = "White";
              default: sColor = "???";
          }
  
          // Print the color name to the LCD screen
          ClearScreen();
          TextOut(0, 56, NumToStr(nColor) + ". " + sColor);
  
          // Wait until the color changes
          while( Sensor(S3) == nColor ); 
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-31T19:45:46.510Z
Answers.Text: How can i get orange color??
Answers.UserDisplayName: Timbelion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-31T10:43:51.053Z
Answers.LastActivityDate: 2015-03-31T10:50:11.993Z
Answers.LastEditDate: 2015-03-31T10:50:11.993Z
Answers.UserDisplayName: shea
Body: <p>Currently I am working Lego robot that solves 2x2x2 Rubik's cube. I am programming Lego Mindstorms NXT 2.0 color sensor in BricxCC.</p>
  
  <p>I already have RGB data with the command <code>ReadSensorColorRaw</code>. I heard that it is easier to get the colour from HSL format. I've tried a lot of algorithms, but none succeeded. How I can fix it to work?'</p>
  
  <p>Also I heard that I need to calibrate white balance. How can I do this?? </p>
  
Tags: <mindstorms><nxt><programming><nxc><sensor-colour>
Title: Programming NXT color sensor with NXC?
UserDisplayName: Timbelion
ThreadText: # Programming NXT color sensor with NXC?
  
  (Asked by: username_0 on 2015-03-31)
  
  Currently I am working Lego robot that solves 2x2x2 Rubik's cube. I am programming Lego Mindstorms NXT 2.0 color sensor in BricxCC.
  
  I already have RGB data with the command `ReadSensorColorRaw`. I heard that it is easier to get the colour from HSL format. I've tried a lot of algorithms, but none succeeded. How I can fix it to work?'
  
  Also I heard that I need to calibrate white balance. How can I do this??
  
  **Tags:** mindstorms, nxt, programming, nxc, sensor-colour
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2015-06-23. Score: 1
  
  Since you are trying to solve a Rubik's cube, check out the [source code for the MindCuber](http://mindcuber.com/mindcuber/mindcuber-source.html). The solution there is that the values are not actually converted into colors. Rather the raw values are converted to HSV and then sorted by hue. The actual value does not matter, just how close it is to the other values.
  
  The Mindcuber also has a white calibration. This is used to identify the white values and removed them from the list before sorting which is useful since white doesn't have a meaningful hue value.
  
  
  ---
  
  ### Answer by username_2 on 2015-03-31. Score: 0
  
  The following works if you just want the builtin six/seven colors, without any calibration.
  
  ```
  task main() {
    SetColorSensorFull(S3);
    while(true){
      int value=Sensor(S3);
      // value now contains 1 for black, 2 for blue, 3 for green, 4 for yello, 5 for red, 6 for white, and something else for an unknown color.
    }
  
  ```
  
  However, if you want to do proportional control, want more colors, or aren't satisfied with the builtin thresholds, you'll need to use the RGB or HSL values. Here's RGB (Threshold is of course, my own defined struct and my solution to this problem):
  
  ```
  struct Threshold{
    int minR, minG, minB;
    unsigned int maxR, maxG, maxB;
  };
  
  bool InRange(ColorSensorReadType &csr, Threshold &thresh){
    bool in=true;
    in&=csr.NormalizedArray[0]<=thresh.maxR;
    in&=csr.NormalizedArray[1]<=thresh.maxG;
    in&=csr.NormalizedArray[2]<=thresh.maxB;
    in&=csr.NormalizedArray[0]>=thresh.minR;
    in&=csr.NormalizedArray[1]>=thresh.minG;
    in&=csr.NormalizedArray[2]>=thresh.minB;
    return in;
  }
  
  Threshold Red={180,20,20,250,130,130},
  Green={90,160,90,160,255,160},
  Blue={40,100,160,80,150,200},
  White={210,210,210,300,300,300},
  GroundBlack={0,0,0,150,150,150},
  GroundGreen={120,180,120,170,220,160};
  
  task main(){
    SetSensorColorFull(S2);
    ColorSensorReadType csr;
    csr.Port=S2;
  
    while(true){
      SysColorSensorRead(csr);
      ClearScreen();
      NumOut(0,LCD_LINE1,csr.NormalizedArray[0]);
      NumOut(0,LCD_LINE2,csr.NormalizedArray[1]);
      NumOut(0,LCD_LINE3,csr.NormalizedArray[2]);
  
      if(InRange(csr,Blue)){
        TextOut(0,LCD_LINE5,"BLUE!");
      }else if(InRange(csr,Green)){
        TextOut(0,LCD_LINE5,"GREEN!");
      }else if(InRange(csr,White)){
        TextOut(0,LCD_LINE5,"WHITE!");
      }
  
      Wait(10);
    }
  }
  
  ```
  
  If you want to transform into HSV values, try this:
  
  ```
  struct Threshold{
    int minR, minG, minB;
    unsigned int maxR, maxG, maxB;
  };
  
  struct HSVThreshold{
    int minH, minS, minV;
    unsigned int maxH, maxS, maxV;
  };
  
  struct HSVColor{
    int hue, sat, val;
  }
  void RGBToHSV(ColorSensorReadType &csr, HSVColor &color){
    int maxColor=csr.NormalizedArray[0], minColor=csr.NormalizedArray[0];
    for (int i=1;i<3;i++) {
      if (maxColor<csr.NormalizedArray[i]) maxColor = csr.NormalizedArray[i];
      if (minColor>csr.NormalizedArray[i]) minColor = csr.NormalizedArray[i];
    }
    if (minColor==maxColor) {
      color.val=minColor;
      color.sat=0;
      color.hue=0;
    } else {
      int d = (r==minColor) ? g-b : ((b==minColor) ? r-g : b-r);
      int h = (r==minColor) ? 3 : ((b==minColor) ? 1 : 5);
      color.hue=60*(h-d/(maxColor-minColor));
      color.sat=(maxColor-minColor)/maxColor;
      color.val=maxColor;
    }
  }
  
  bool InRange(ColorSensorReadType &csr, Threshold &thresh){
    bool in=true;
    in&=csr.NormalizedArray[0]<=thresh.maxR;
    in&=csr.NormalizedArray[1]<=thresh.maxG;
    in&=csr.NormalizedArray[2]<=thresh.maxB;
    in&=csr.NormalizedArray[0]>=thresh.minR;
    in&=csr.NormalizedArray[1]>=thresh.minG;
    in&=csr.NormalizedArray[2]>=thresh.minB;
    return in;
  }
  bool InRange(HSVColor color, HSVThreshold &thresh){
    bool in=true;
    in&=color.hue<=thresh.maxH;
    in&=color.sat<=thresh.maxS;
    in&=color.val<=thresh.maxV;
    in&=color.hue>=thresh.minH;
    in&=color.sat>=thresh.minS;
    in&=color.val>=thresh.minV;
    return in;
  }
  
  Threshold Red={180,20,20,250,130,130},
  Green={90,160,90,160,255,160},
  Blue={40,100,160,80,150,200},
  White={210,210,210,300,300,300},
  GroundBlack={0,0,0,150,150,150},
  GroundGreen={120,180,120,170,220,160};
  
  // Define your own HSVThresholds here
  
  task main(){
    SetSensorColorFull(S2);
    ColorSensorReadType csr;
    csr.Port=S2;
  
    while(true){
      SysColorSensorRead(csr);
      HSVColor color;
      RGBToHSV(csr,color);
  
      ClearScreen();
      NumOut(0,LCD_LINE1,color.hue);
      NumOut(0,LCD_LINE2,color.sat);
      NumOut(0,LCD_LINE3,color.val);
  
      Wait(10);
    }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Currently I am working Lego robot that solves 2x2x2 Rubik's cube, Thanks!  
  > **username_2**: You can add more thresholds and tweak them in order to get even more colors.  
  
  ---
  
  ### Answer by username_3 on 2015-03-31. Score: 0
  
  I'm not entirely sure if you need the raw data, but if you're just looking to read the different colour values, there is a much easier way.
  
  To turn on and set up the sensor, use `SetSensorColorFull(S3);`. Then to retrieve the value just use `Sensor(S3)`. The value returned will be an integer from 1 to 6, corresponding to the following colour values:
  
  ```
  1 = black
  2 = blue
  3 = green
  4 = yellow
  5 = red
  6 = white
  
  ```
  
  Here is a sample program that will read the current value from the colour sensor and print the colour name on the NXT screen:
  
  ```
  task main() {
      string sColor;
      int nColor;
  
      // Turn on the sensor
      SetSensorColorFull(S3);
  
      while (1) {
  
          // Read the numerical color value from the sensor
          nColor = Sensor(S3);
  
          // Determine the color name from its number
          switch(nColor) {
              case 1: sColor = "Black";
              case 2: sColor = "Blue";
              case 3: sColor = "Green";
              case 4: sColor = "Yellow";
              case 5: sColor = "Red";
              case 6: sColor = "White";
              default: sColor = "???";
          }
  
          // Print the color name to the LCD screen
          ClearScreen();
          TextOut(0, 56, NumToStr(nColor) + ". " + sColor);
  
          // Wait until the color changes
          while( Sensor(S3) == nColor ); 
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: How can i get orange color??  
  
  ---
  
  