Transparently stupid

For a long time now it’s been possible to specify CSS colours as RGB triplets, using hexadecimal, decimal, or percentage notation. These all represent the colour white:

#ffffff
#fff
rgb(255, 255, 255)
rgb(100%, 100%, 100%)

Lots of flexibility – perhaps too much – and three different numbering systems to deal with. So when the W3C CSS working group decided to add an alpha channel option to RGB colours, clearly it would have made sense to allow the following (for a semi-opaque RGB colour):

#ffffff99
#fff9
rgba(255, 255, 255, 127)
rgba(100%, 100%, 100%, 50%)

But it seems that sense was in short supply at the time. Instead they opted to allow these:

rgba(255, 255, 255, 0.5)
rgba(100%, 100%, 100%, 0.5)

Note that there is no hexadecimal representation at all – an omission which, though a little arbitrary, shouldn’t cause too many problems. Perhaps more strange is that the two representations that are allowed can only use a floating point representation for the opacity – a number from 0.0 to 1.0.

I’m guessing that this was done for compatibility with the older CSS opacity definition – but if you’re adding new colour mappings to the spec anyway, why not expand it to allow for other representations? Surely if your R, G and B are represented as decimal numbers in the range 0-255, it’s only sensible to also allow your opacity to sit in the same range. Similarly with the percentage representation.

This may seem a little esoteric, but it can have real effects. For example I’m currently writing some code which interpolates from one colour to another, gradually changing between them so that some button, text or other object on screen can be made to gently (or not so gently) pulse with colour. The start and end colours get mapped to decimal rgba values, and interpolating any of the first three colour channels is quite easy. However for the alpha channel I’ve had to implement a separate code path to interpolate between two floating point numbers, rather than just being able to use the simpler code that I’d already written.

Perhaps there was a good reason for not allowing hexadecimal, decimal or percentage values for the alpha channel – but if so, I haven’t worked out what it is yet.

Leave a Reply

Your email address will not be published. Required fields are marked *