svelte-color-select icon indicating copy to clipboard operation
svelte-color-select copied to clipboard

Clamp RGB to sRGB gamut

Open drwpow opened this issue 2 years ago • 0 comments

Changes

Fixes #9. Clamps colors to the RGB range.

CleanShot 2023-09-03 at 16 01 06

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

Reviewing

  • See comments

drwpow avatar Sep 03 '23 22:09 drwpow