Skip to content

[css-color-4] Conversion precision for hue in LCH model creates false results #5309

Closed
@snigo

Description

@snigo

One thing I have noticed with sRGB to Lab/LCH conversion as it produces chaotic and very incorrect hue value in LCH color model. Converting any shade of gray to Lab/LCH will result in components a and b very close to zero, but still not zero. It is absolutely fine for calculating chroma, as square root of those numbers will still result in number very close to zero, however calculating hue with Math.atan() gives very high range of (falsy) values whenever there is any difference between a and b, which always there!

Math.atan2(0.0000000005, -0.0000000001) * 180 / Math.PI; // 101.30993247402021
Math.atan2(0.0000000005, 0.0000000001) * 180 / Math.PI; // 78.69006752597979
Math.atan2(0.0000000005, 0.0000000003) * 180 / Math.PI; // 59.03624346792648

Rounding to 3 decimal places of a and b solves problem for any shade of gray resulting in correct value of 0, however creates incorrect results for some highly desaturated colors like hsl(0 1% 1%) or hsl(0 1% 99%), which in my opinion is better comparing to incorrect results for any shade of gray (including white and black)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions