> For the complete documentation index, see [llms.txt](https://docs.revrobotics.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-1/programming-color-sensors/detecting-color.md).

# Detecting Color

We've asked our robot to gather a lot of data with the color sensor. Now let's have it use that information to output an actual color name rather than just a value!

## Detecting Common Colors

Recall when we learned about using [`if/else` statements while working with the touch sensor.](/duo-control/hello-robot-blocks/part-1/programming-touch-sensors.md#touch-sensor-basics)&#x20;

Let's first set up the skeleton of our if/else statement for determining different colors:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FCAY5FAtKeIJrxpEIsw0P%2Fimage.png?alt=media&amp;token=f964a937-63ab-464a-aa74-25d203a59f0d" alt=""><figcaption><p>Detecting color if/else statement</p></figcaption></figure>

Once we've added our ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FmUPoKSjWU5hh9028bYLg%2Fimage.png?alt=media\&token=af62a05b-fed7-4fc8-a27c-98edc6d3cd3f) block we'll click the gear to add the needed "else if" pieces to have enough for all our colors. To each we will add a ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FhUrBwe2JuOx1zc9lE43t%2Fimage.png?alt=media\&token=60203c4e-f7c8-4211-9fd7-344d8390bc41) block from the Logic menu. Next we can add our ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FVI9eizizd8MZrbDykj9n%2Fimage.png?alt=media\&token=57f6a771-f4de-4f80-8446-c888275d24f7) variable and a ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FzUEaBtEgx4uaiWk5YrvK%2Fimage.png?alt=media\&token=5aba31bf-0292-4162-99e5-f881b5701920) block to each side of this logic statement. We want for each if/else our robot to check if the read hue is LESS THAN a set number!

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FqLXYmrU1oVQ8QfUi5Rzj%2Fimage.png?alt=media&amp;token=4f2a0636-c6df-4b55-a969-65092cf36f63" alt=""><figcaption><p>Is the seen hue less than _?</p></figcaption></figure>

Last we can add our telemetry for our robot to read out information to the Driver Hub based on what it detects:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FD1gSf4Um7rMpMLyfEuc5%2Fimage.png?alt=media&amp;token=90605a70-d7ef-499f-9794-cd028f5a5097" alt=""><figcaption><p>Added telemetry blocks</p></figcaption></figure>

Each check will be for a certain color that is within the specified range. "Key" can be changed to "Color" on the telemetry blocks. We can add the colors in first:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FHU5UTVssq1Aeo9G47SG2%2Fimage.png?alt=media&amp;token=1d7e7ba2-4651-4654-9c5a-88a0b6b1e217" alt=""><figcaption><p>If/else statement with colors added</p></figcaption></figure>

Next we will add our values for the hue ranges. For example, a color that's hue is between 90-149 should appear as green.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FtmmvfYxrvUGuoIrbp2By%2Fimage.png?alt=media&amp;token=3d2a1c39-fa9b-441d-8d85-001b350a2dad" alt=""><figcaption><p>Completed if/else statement</p></figcaption></figure>

The exact hue values may need to be adjusted slightly, but those used above are based on the default conversion of HSV to RGB when using hue to identify color.&#x20;

You'll notice that "red" is detected for values under 30 and above 350. This is intentional as red is the beginning and end of the RGB spectrum!

Let's snap our If/Else statement into our loop below the "Alpha" telemetry call.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FYA2F7ujDpyOxoeEpuOAA%2Fimage.png?alt=media&amp;token=d73dfc4d-0570-4474-80a8-307c3c89a7e8" alt=""><figcaption><p>If/Else statement added to the full program.</p></figcaption></figure>

Save your OpMode and give it a try! You can adjust the values as you need to better reflect the colors available or changes due to lighting in the room.
