Skip to content

Commit 63c07f9

Browse files
committed
fix: add _hsl_s and _hsv_s, limit h to 0-360
1 parent 84ebaa9 commit 63c07f9

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

src/FastColor.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export class FastColor {
8686

8787
// HSV privates
8888
private _h?: number;
89-
private _s?: number;
89+
private _hsl_s?: number;
90+
private _hsv_s?: number;
9091
private _l?: number;
9192
private _v?: number;
9293

@@ -143,7 +144,8 @@ export class FastColor {
143144
this.b = input.b;
144145
this.a = input.a;
145146
this._h = input._h;
146-
this._s = input._s;
147+
this._hsl_s = input._hsl_s;
148+
this._hsv_s = input._hsv_s;
147149
this._l = input._l;
148150
this._v = input._v;
149151
} else if (matchFormat('rgb')) {
@@ -235,28 +237,28 @@ export class FastColor {
235237
}
236238

237239
getHSVSaturation(): number {
238-
if (typeof this._s === 'undefined') {
240+
if (typeof this._hsv_s === 'undefined') {
239241
const delta = this.getMax() - this.getMin();
240242
if (delta === 0) {
241-
this._s = 0;
243+
this._hsv_s = 0;
242244
} else {
243-
this._s = delta / this.getMax();
245+
this._hsv_s = delta / this.getMax();
244246
}
245247
}
246-
return this._s;
248+
return this._hsv_s;
247249
}
248250

249251
getHSLSaturation(): number {
250-
if (typeof this._s === 'undefined') {
252+
if (typeof this._hsl_s === 'undefined') {
251253
const delta = this.getMax() - this.getMin();
252254
if (delta === 0) {
253-
this._s = 0;
255+
this._hsl_s = 0;
254256
} else {
255257
const l = this.getLightness();
256-
this._s = (delta/255) / (1 - Math.abs(2 * l - 1));
258+
this._hsl_s = (delta/255) / (1 - Math.abs(2 * l - 1));
257259
}
258260
}
259-
return this._s;
261+
return this._hsl_s;
260262
}
261263

262264
getLightness(): number {
@@ -502,8 +504,8 @@ export class FastColor {
502504
}
503505

504506
private fromHsl({ h, s, l, a }: OptionalA<HSL>): void {
505-
this._h = h = h % 360;
506-
this._s = s;
507+
this._h = h = ((h % 360) + 360) % 360;
508+
this._hsl_s = s;
507509
this._l = l;
508510
this.a = typeof a === 'number' ? a : 1;
509511

@@ -550,8 +552,8 @@ export class FastColor {
550552
}
551553

552554
private fromHsv({ h, s, v, a }: OptionalA<HSV>): void {
553-
this._h = h % 360;
554-
this._s = s;
555+
this._h = h = ((h % 360) + 360) % 360;
556+
this._hsv_s = s;
555557
this._v = v;
556558
this.a = typeof a === 'number' ? a : 1;
557559

tests/hsl.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,12 @@ describe('hsl', () => {
8787
expect(minCap.getLightness()).toBe(0);
8888
expect(maxCap.getLightness()).toBe(1);
8989
});
90+
91+
it('normalizes H outside range for HSL (object input)', () => {
92+
// -60 -> 300 (magenta)
93+
expect(new FastColor({ h: -60, s: 1, l: 0.5 }).toHexString()).toBe('#ff00ff');
94+
// 420 -> 60 (yellow)
95+
expect(new FastColor({ h: 420, s: 1, l: 0.5 }).toHexString()).toBe('#ffff00');
96+
});
9097
});
9198

tests/hsv.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,11 @@ describe('hsv', () => {
5555
const turn = base.setHue(233);
5656
expect(turn.getValue()).toBe(1);
5757
});
58+
59+
it('normalizes H outside range for HSV (object input)', () => {
60+
// -60 -> 300 (magenta)
61+
expect(new FastColor({ h: -60, s: 1, v: 1 }).toHexString()).toBe('#ff00ff');
62+
// 420 -> 60 (yellow)
63+
expect(new FastColor({ h: 420, s: 1, v: 1 }).toHexString()).toBe('#ffff00');
64+
});
5865
});

0 commit comments

Comments
 (0)