Color Sensor mode 4 (RGB)

In mode 4 (not 3) the colour sensor will return RGB colour values for reflected light. This means you can measure any color, from a palette of millions, and are not limited to recognising one of 7 standard colours like you are in mode 2. Any color can be obtained by mixing together the right amounts of red, green and blue light. Thus an RGB measurement will actually return three values (red light intensity, green light intensity, blue light intensity), meaning that the normal function Sensor.ReadPercent() cannot be used. Instead you must use the function Sensor.ReadRaw(port number, 2). This will return an array with three elements (elements 0, 1 and 2), each element containing an RGB color value. These values are not limited to (100,100,100) or (255,255,255) as you might expect. My experiments suggest that the maximum values (for a very bright white surface) are around (330,330,124). You can use code to scale each value to have a maximum value of 255 or 100 like this:

Sensor.Wait (3)  'make sure the sensor is ready for a mode change

Sensor.SetMode(3,4)  'will set color sensor on port 3 to mode 4: RGB values

While "True"

    LCD.StopUpdate()    'don't update the screen until all the text is ready

    LCD.Clear()

    R=Sensor.ReadRaw(3,3)  'Obtain from sensor on port 3

    'a matrix containing 3 values.

    'For a bright white surface, this returns about (350,350,140)

    LCD.Text(1,0,0, 1, "Raw RGB values:")

    LCD.Text(1,0,10, 1, "Max = 350,350,140")

    LCD.Text(1,0,20, 2, R[0]+","+R[1]+","+R[2])

    

    'scale each value so that bright white corresponds to about 255,255,255

    S[0]=Math.Round(R[0]*255/350)  'red

    S[1]=Math.Round(R[1]*255/350)  'green

    S[2]=Math.Round(R[2]*255/140)  'blue

    LCD.Text(1,0,45, 1, "Scaled to max")

    LCD.Text(1,0,55, 1, "255,255,255:")

    LCD.Text(1,0,65, 2, S[0]+","+S[1]+","+S[2])

  

    'scale each value so that bright white corresponds to about 100,100,100

    P[0]=Math.Round(R[0]/3.5)  'red

    P[1]=Math.Round(R[1]/3.5)  'green

    P[2]=Math.Round(R[2]/1.4)  'blue    

    LCD.Text(1,0,90, 1, "Scaled to max")

    LCD.Text(1,0,100, 1, "100,100,100:")

    LCD.Text(1,0,110, 2, P[0]+","+P[1]+","+P[2])

    LCD.Update()

    Program.Delay(100)  'pause 0.1 seconds

EndWhile

Now that you know how to obtain RBG values, you could write code to define a range of RGB values that would correspond to, for example, 'orange', or 50 shades of gray...