Question

I have yet to find an algorithm/implementation that can convert rgb(0, 96, 255) to HSL and back, without ending up with a slightly different value (rgb(0, 98, 255)). This means everyone's algorithms are broken (including Wolfram|Alpha)—or that HSL cannot represent every RGB color.

Which is correct?

Was it helpful?

Solution

n.m. is correct—"HSL can represent every RGB color, but HSL rounded to nearest integer cannot."

What I ended up doing with the color class I was writing was to internally store the color as unrounded HSL. Then the getter methods for RGB and HSL just rounded them when called. This enabled doing things like changing hue and saturation, even on RGB colors, without getting an inaccurate conversion.

There's still no way to accurately convert rounded RGB and rounded HSL, so the solution is to simply never convert rounded RGB to/from rounded HSL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top