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

`HslaColor` parsed to zero in hue value #116

Open bjarnef opened 10 months ago

bjarnef commented 10 months ago

We have something like this in a color picker wheere hue value is changed from a hue slider:

const newColor: HslaColor = {
      h: element.value,
      s: this.saturation,
      l: this.lightness,
      a: this.alpha / 100,
};

this.setColor(newColor);

Inside setColor() function:

  setColor(colorString: string | HslaColor) {

    const colord = new Colord(colorString);

    const { h, s, l, a } = colord.toHsl();

    this.hue = h;
    this.saturation = s;
    this.lightness = l;
    this.alpha = this.opacity ? a * 100 : 100;

    const hslaColor = colorString as HslaColor;

    // Workaround as hue isn't correct after changing hue slider, but Colord parse hue value as zero when color is black.
    if (hslaColor && hslaColor.h) {
      this.hue = hslaColor.h;
    }

    this._colord = colord;
    ...
}

Hue value from HslaColor object is e.g. 191, but after parsing in to Colord instance and use toHsl() it returns a hue of 0. I added a workaround if the parameter is an HslaColor object and then use in this h property instead, which has the value 191.

chrome_9dOIC9SrbN