omgovich / colord

👑 A tiny yet powerful tool for high-performance color manipulations and conversions
https://colord.omgovich.ru
MIT License
1.63k stars 48 forks source link

Set function? #78

Closed patrikengborg closed 2 years ago

patrikengborg commented 2 years ago

Hi,

This seems like an awesome library! I'm using Chroma.js right now, but I'm tempted to switch.

One thing I use in Chroma all the time is the color.set() function. It lets you change a single channel in a color, like: chroma('#036').set('hsl.l', 0.2).hex();

It's not a relative modification like lighten() and darken() i Colord.

Is there an equivalent for color.set() in Colord?

omgovich commented 2 years ago

Hi! In the modern JavaScript you don't need methods like set: you can just build a new color using destructurisation:

const oldHsl = color.toHsl()
colord({ ...hsl, l: 20 })
patrikengborg commented 2 years ago

Ok cool, for some reason I didn't think of that obvious solution, thanks.

What about modifying LCH colors.. I don't think I can just change the L value there, since a lightness change also involves the other channels in the color, if I understand LCH correctly, right?

So is there any way to change a LCH color to an absolute lightness value of 5%, without doing it relatively with lighten() or darken() ?

Sorry if this is a weird question or out of place. I'm currently trying to grasp LCH and how it works.

omgovich commented 2 years ago

You can do the same with LCH color if lch plugin is enabled:

import { colord, extend } from "colord";
import lchPlugin from "colord/plugins/lch";

extend([lchPlugin]);

const oldLch = colord({ l: 50, c: 0, h: 0 }).toLch()
colord({ ...oldLch, l: 100 }).toLch() // { l: 100, c: 0, h: 0, a: 1 }

colord's parser detects a color model automatically (based on the object fields). If an input object has l, c and h keys the parser consider the values as LCH.

patrikengborg commented 2 years ago

Thanks, I'll give it a try!