Skip to content

Commit

Permalink
fix: setHue not work as expect (#36)
Browse files Browse the repository at this point in the history
* fix: setHue should be same

* chore: adjust logic
  • Loading branch information
zombieJ authored Jul 19, 2024
1 parent ad79bc8 commit 57344b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/FastColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ export class FastColor {

if (!input) {
// Do nothing since already initialized
} else if (input instanceof FastColor) {
this.r = input.r;
this.g = input.g;
this.b = input.b;
this.a = input.a;
this._h = input._h;
this._s = input._s;
this._l = input._l;
this._v = input._v;
} else if (typeof input === 'string') {
const trimStr = input.trim();

Expand All @@ -132,6 +123,15 @@ export class FastColor {
} else if (matchPrefix('hsv') || matchPrefix('hsb')) {
this.fromHsvString(trimStr);
}
} else if (input instanceof FastColor) {
this.r = input.r;
this.g = input.g;
this.b = input.b;
this.a = input.a;
this._h = input._h;
this._s = input._s;
this._l = input._l;
this._v = input._v;
} else if (matchFormat('rgb')) {
this.r = limitRange((input as RGB).r);
this.g = limitRange((input as RGB).g);
Expand Down Expand Up @@ -168,9 +168,9 @@ export class FastColor {
}

setHue(value: number) {
const hsl = this.toHsl();
hsl.h = value;
return new FastColor(hsl);
const hsv = this.toHsv();
hsv.h = value;
return new FastColor(hsv);
}

// ======================= Getter =======================
Expand Down Expand Up @@ -241,6 +241,7 @@ export class FastColor {

/**
* Returns the perceived brightness of the color, from 0-255.
* Note: this is not the b of HSB
* @see http://www.w3.org/TR/AERT#color-contrast
*/
getBrightness(): number {
Expand Down
8 changes: 8 additions & 0 deletions tests/hsv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ describe('hsv', () => {

expect(base.toHsv()).toEqual(turn.toHsv());
});

it('setHue should be stable', () => {
const base = new FastColor('#1677ff');
expect(base.getValue()).toBe(1);

const turn = base.setHue(233);
expect(turn.getValue()).toBe(1);
});
});

0 comments on commit 57344b2

Please sign in to comment.