Working With Colors
You will occasionally want to validate the colour of something as part of your tests; the problem is that colour definitions on the web are not constant. Would it not be nice if there was an easy way to compare a HEX representation of a colour with a RGB representation of a colour, or a RGBA representation of a colour with a HSLA representation of a colour?
Worry not. There is a solution: the Color class!
First of all, you will need to import the class:
You can now start creating colour objects. Every colour object will need to be created from a string representation of your colour. Supported colour representations are:
The Color class also supports all of the base colour definitions specified in http://www.w3.org/TR/css3-color/#html4.
Sometimes browsers will return a colour value of “transparent” if no colour has been set on an element. The Color class also supports this:
You can now safely query an element to get its colour/background colour knowing that any response will be correctly parsed and converted into a valid Color object:
You can then directly compare colour objects:
Or you can convert the colour into one of the following formats and perform a static validation:
Colours are no longer a problem.