Colour can be detected in many ways. with colour filters and photo diodes PD , with controlled light source and reference PD for clear. loss can be calibrated out for clear. Scattering of light source can cause errors so it is important to use small black tube , say heatshrink over 5mm PD in a fixture to block stray light. Imagine white light say from several sources like small LED's is transparent with same colour and blocked by complementary colour.
Narrow band colour filter over PD can pass with a specific Bandpass spectral width depending on cost/ quality of filter. another approach uses a tiny prism to split beam into different PD's to detect amplitude of spectrum in each colour. Another approach uses an integrated RGB LED to scan thru each colour to generate a response in the PD which has a band reject filter for IR and UV if present. Testing with a continously variable colour at constant current can give a different signature for each condition. Then you can choose fixed combinations to optimize the discrimination of colours to as accurate as you want. Stable emitter source is critical and this can be achieved by a closed loop with PD to detect emitter LED output over time temperature and voltage variations using RGB with compensation for each colour.
If this sounds too complex to solve into a simple design.. consider the board spectrum Photo sensor CCD or luminous intensity chip and test for various light sources at different wavelengths to determine signal to noise ratio of result or.. error in colour recognition vs RGB source. Keep in mind each RGB LED is not perfect narrow band source. For super high accuracy broad spectrum pulsed Xenon source (flash) with comparison between thru tube and reflection from mirror will help normalize the results.
Once you know analog voltage vs intensity, you can design a series of comparators to provide a logic level for alarm for each threshold.
A digital approach is more accurate
https://www.sparkfun.com/products/8618
But an analog solution is possible. Self calibration is key in analog and PD's are very consistent and accurate ( Diode with reverse bias and Resistor to sense current) but LEDs are not very consisten, so feedback loop to regulate emitter is essential.
precision designs are worth a couple thousand dollars
https://media.digikey.com/pdf/Data Sheets/Omron PDFs/E3MC RGB Color Sensor.pdf
but cheap designs take skill.
- - - Updated - - -
Here is a cheap interesting solution
ApplicationNote with design
**broken link removed**