Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is HSL conversion exact?

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?

like image 682
Teddy Cross Avatar asked Jun 16 '26 07:06

Teddy Cross


1 Answers

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.

like image 62
Teddy Cross Avatar answered Jun 18 '26 00:06

Teddy Cross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!