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