Convert an LCH color to a Lab color
Turn chroma and hue degrees back into a and b.
function lchToLab([l, c, h]) { return [l, Math.cos((h * Math.PI) / 180) * c, Math.sin((h * Math.PI) / 180) * c]; }
Input & expected output
Input
[
50,
10,
0
]Expected output
[
50,
10,
0
]The edge case
Input
[
50,
10,
90
]Expected output
[
50,
6.123233995736766e-16,
10
]Know the limits
- Floating-point trigonometry leaves tiny residues such as 6e-16, as the edge case shows; round for display.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Convert an LCH color to a LAB color from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Curated implementation; explicit input contract, edge case and limits. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Convert Celsius to Fahrenheit — Apply the temperature conversion without rounding the result.
- Convert hex to RGB — Expand #rgb if needed and read the red, green and blue channels.
- Convert URL parameters to object — Read a query string into an object; repeated keys become arrays.