DEV Community

Discussion on: Understanding the Color type in Go

Collapse
 
mokiat profile image
Momchil Atanasov

Hi! Thanks for looking into this and for the feedback.

It's been a while since I wrote this article. What I meant by float in the article is that pre-multiplied doesn't mean "just multiply the two values" but instead it implies that the alpha is treated as a fraction.

My initial code does use float to do exactly that in order to show the most straight-forward and easy implementation, but as can be seen in the Official implementation section, I do mention that the official implementation does NOT use float. Here is a quote from my article:

There is clearly a major difference between the two implementations. How is it that the official implementation avoids the usage of float32 values and what are all the bit-wise operations doing?

The subsequent section (Iterative optimization) covers exaclty that - going from the floating point code (which is easy to comprehend) and getting to the official code (which uses bit-shifting and is more optimal).

As for the color.Model interface, you are right, I did not cover it in this article and in hindsight maybe I should have. So thanks for adding that in your comment - readers will find it useful.

It provides an easy way to convert from one color to the next, though one needs to be careful, since in some cases it could be sub-optimal - from what I recall, it uses multiple interface indirections and requires that source colors be converted to the Color format and afterwards be convcerted to the target Model format. In some cases there might be a more trivial direct conversion (e.g. color.NRGBA to hdrcolor.RGB).