Worth noting that this is the “bad” way to clamp; Björn Ottosen outlines keeping hue consistent and clamping lightness and chroma, which culori has implemented in their clampChroma(… 'oklch') method. I instead took the lazier approach of just clamping to (0, 1) as a first pass.
Culori’s clampRGB() function also takes the same lazy approach, but loads a bunch of converter methods for a total of 3.6 kB for what ends up being the same end result.
Alternative method
The alternate way of doing it the “right” way has a couple caveats:
It adds ~ 3.8 kB of client weight. Which may not be much all things considered. Also worth noting that even though clampRGB() is a bit bloated, clampChroma() is not—it’s actually a complex calculation and does involve shifting a color through a couple color spaces to get the accuracy right.
This only happens when the value changes externally (e.g. the slider), so you could argue that true gamut adjustment is outside the scope of this library. When colors are adjusted on the picker this problem doesn’t exist
But if the alternate method is preferred, it’s an easy swap
Changes
Fixes #9. Clamps colors to the RGB range.
Worth noting that this is the “bad” way to clamp; Björn Ottosen outlines keeping hue consistent and clamping lightness and chroma, which culori has implemented in their
clampChroma(… 'oklch')
method. I instead took the lazier approach of just clamping to(0, 1)
as a first pass.Culori’s
clampRGB()
function also takes the same lazy approach, but loads a bunch of converter methods for a total of3.6 kB
for what ends up being the same end result.Alternative method
The alternate way of doing it the “right” way has a couple caveats:
3.8 kB
of client weight. Which may not be much all things considered. Also worth noting that even thoughclampRGB()
is a bit bloated,clampChroma()
is not—it’s actually a complex calculation and does involve shifting a color through a couple color spaces to get the accuracy right.But if the alternate method is preferred, it’s an easy swap
Reviewing