Build a list of Colour values and check if a specific colour is in the palette.
← Operators and Expressions · Ref: Q1207
EK9 Colour uses hex literals (#RRGGBB). Store colours in a List and use the 'contains' operator for membership testing.
PALETTE
palette <- [#FF0000, #00FF00, #0000FF]
MEMBERSHIP CHECK
if palette contains targetColour stdout.println("Colour found in palette")
The 'contains' operator checks element membership in a List. It uses the == operator internally, so it works with any type that supports equality. Colour literals and the == operator make palette management simple and type-safe.
See Q1159 for list contains. See Q1190 for colour equality. See Q1101 for is in with lists.
Example
defines module qa.operators.colourpalette defines program ColourPaletteDemo() stdout <- Stdout() // === BUILD PALETTE === red <- #FF0000 green <- #00FF00 blue <- #0000FF palette <- [red, green, blue] // === MEMBERSHIP CHECK === targetColour <- #FF0000 if palette contains targetColour stdout.println(`${targetColour} is in the palette`) // === COLOUR NOT IN PALETTE === yellow <- #FFFF00 if not (palette contains yellow) stdout.println(`${yellow} is not in the palette`) // === CHECK MULTIPLE COLOURS === candidates <- [#FF0000, #FFFF00, #0000FF, #FF00FF] for candidate in candidates if palette contains candidate stdout.println(`${candidate}: in palette`) else stdout.println(`${candidate}: not in palette`) // === ADD TO PALETTE AND RECHECK === extendedPalette <- palette + yellow if extendedPalette contains yellow stdout.println(`Yellow added to extended palette`) // === STREAM OUTPUT === stdout.println("Full palette:") cat palette > stdout
Other ways to ask this
- Create a palette of colours and test membership using the contains operator.
- I need to verify whether a given colour exists in a predefined set.
- In CSS I'd compare hex codes as strings. How does EK9 check colour membership in a list?
- Given a list of Colour literals, check if a target colour is present.
Coming from another language?
Java: list.contains(color) with java.awt.Color. Python: color in palette with tuples or custom class. JavaScript: array.includes(color) with string hex codes. CSS: string comparison. EK9: palette contains targetColour — operator syntax, typed Colour values, no string comparison needed.
Keywords: palette, collection, rgb, colour, check, membership, hex, contains, list, literal