diff --git a/README.md b/README.md index 931bde2..8d22ac9 100644 --- a/README.md +++ b/README.md @@ -500,31 +500,31 @@ colorblender({ r: 167, g: 40, b: 13 }) ```typescript colorblender({ r: 167, g: 40, b: 13 }) .harmonies('analogous') - .map((c) => c.hex()); // ['#A70D3E', '#A7290D', '#A7760D'] + .map((c) => c.hex()); // ['#A70D3F', '#A7280D', '#A7750D'] colorblender({ r: 167, g: 40, b: 13 }) .harmonies('complementary') - .map((c) => c.hex()); // ['#A7290D', '#0D8BA7'] + .map((c) => c.hex()); // ['#A7280D', '#0D8CA7'] colorblender({ r: 167, g: 40, b: 13 }) .harmonies('split-complementary') - .map((c) => c.hex()); // ['#A7290D', '#0DA776', '#0D3EA7'] + .map((c) => c.hex()); // ['#A7280D', '#0DA775', '#0D3FA7'] colorblender({ r: 167, g: 40, b: 13 }) .harmonies('double-split-complementary') - .map((c) => c.hex()); // ['#A70D3E', '#A7290D', '#A7760D', '#0DA776', '#0D3EA7'] + .map((c) => c.hex()); // ['#A70D3F', '#A7280D', '#A7750D', '#0DA775', '#0D3FA7'] colorblender({ r: 167, g: 40, b: 13 }) .harmonies('tetradic') - .map((c) => c.hex()); // ['#A7290D', '#3EA70D', '#0D8BA7', '#760DA7'] + .map((c) => c.hex()); // ['#A7280D', '#3FA70D', '#0D8CA7', '#750DA7'] colorblender({ r: 167, g: 40, b: 13 }) .harmonies('triadic') - .map((c) => c.hex()); // ['#A7290D', '#0DA729', '#290DA7'] + .map((c) => c.hex()); // ['#A7280D', '#0DA728', '#280DA7'] colorblender({ r: 167, g: 40, b: 13 }) .harmonies('rectangle') - .map((c) => c.hex()); // ['#A7290D', '#8BA70D', '#0D8BA7', '#290DA7'] + .map((c) => c.hex()); // ['#A7280D', '#8CA70D', '#0D8CA7', '#280DA7'] ``` diff --git a/package.json b/package.json index 6852f76..433f69b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "colorblender", - "version": "2.3.1", + "version": "2.3.2", "description": "A powerful and fully typed color library.", "repository": { "type": "git", @@ -125,8 +125,7 @@ "scripts": { "build": "rm -rf ./dist/* && rollup -c --bundleConfigAsCjs", "start": "rollup -c -w --bundleConfigAsCjs", - "index": "nodemon dist/index.js", - "testfile": "tsx ./src/testfile.ts", + "index": "tsx ./src/colorblender.ts", "test": "jest", "test:watch": "jest --watchAll", "dev": "concurrently \"npm run start\" \"npm run test:watch\"", diff --git a/src/colorblender.ts b/src/colorblender.ts index c48039c..397b32d 100644 --- a/src/colorblender.ts +++ b/src/colorblender.ts @@ -51,10 +51,6 @@ export class Colorblender { } } - public _clampRatio(ratio: number): number { - return Math.min(1, Math.max(0, ratio)); - } - public _withAlpha( color: RgbColor | HslColor | HwbColor, ): RgbaColor | HslaColor | HwbaColor { @@ -209,56 +205,50 @@ export class Colorblender { /** * - * @param ratio The ratio to lighten the color (between 0 and 1). + * @param ratio The ratio to lighten the color. * @returns The color lightened. */ public lighten(ratio: number): Colorblender { - ratio = this._clampRatio(ratio); return colorblender(this._withAlpha(lighten(this._internalRgb, ratio))); } /** - * @param ratio The ratio to darken the color (between 0 and 1). + * @param ratio The ratio to darken the color. * @returns The color darkened. */ public darken(ratio: number): Colorblender { - ratio = this._clampRatio(ratio); return colorblender(this._withAlpha(darken(this._internalRgb, ratio))); } /** - * @param ratio The ratio to saturate the color (between 0 and 1). + * @param ratio The ratio to saturate the color. * @returns The color saturated. */ public saturate(ratio: number): Colorblender { - ratio = this._clampRatio(ratio); return colorblender(this._withAlpha(saturate(this._internalRgb, ratio))); } /** - * @param ratio The ratio to desaturate the color (between 0 and 1). + * @param ratio The ratio to desaturate the color. * @returns The color desaturated. */ public desaturate(ratio: number): Colorblender { - ratio = this._clampRatio(ratio); return colorblender(this._withAlpha(desaturate(this._internalRgb, ratio))); } /** - * @param ratio The ratio to fade the color (between 0 and 1). + * @param ratio The ratio to fade the color. * @returns The color faded. */ public fade(ratio: number): Colorblender { - ratio = this._clampRatio(ratio); return this.alpha(this._internalAlpha - this._internalAlpha * ratio); } /** - * @param ratio The ratio to opaquer the color (between 0 and 1). + * @param ratio The ratio to opaquer the color. * @returns The color opaqued. */ public opaquer(ratio: number): Colorblender { - ratio = this._clampRatio(ratio); return this.alpha(this._internalAlpha + this._internalAlpha * ratio); } @@ -270,11 +260,20 @@ export class Colorblender { } /** - * @param amount The amount to rotate the color (between 0 and 360). + * @param amount The amount to rotate the color. * @returns The color rotated. */ public rotate(amount = 15): Colorblender { - return this.hue(this.hue() + amount); + const hsl = rgbToHsl(this._internalRgb); + let newHue = hsl.h + amount; + newHue = newHue % 360; + if (newHue < 0) newHue += 360; + return colorblender({ + h: newHue, + s: hsl.s, + l: hsl.l, + a: this._internalAlpha, + }); } } diff --git a/src/extensions/ansi.ts b/src/extensions/ansi.ts index 2ea2e40..3bdbdf1 100644 --- a/src/extensions/ansi.ts +++ b/src/extensions/ansi.ts @@ -11,7 +11,7 @@ declare module '../colorblender' { } } -const nameExtension: Extensions = (Class, converters): void => { +const ansiExtension: Extensions = (Class, converters): void => { /** * @returns the ansi16 of the color. */ @@ -38,4 +38,4 @@ const nameExtension: Extensions = (Class, converters): void => { ); }; -export default nameExtension; +export default ansiExtension; diff --git a/src/extensions/mix.ts b/src/extensions/mix.ts index 7f6fa21..7448a57 100644 --- a/src/extensions/mix.ts +++ b/src/extensions/mix.ts @@ -26,7 +26,7 @@ const mixExtension: Extensions = (Class): void => { ): Colorblender { let mixed; - weight = this._clampRatio(weight); + weight = Math.min(1, Math.max(0, weight)); if (color instanceof Class) { mixed = mix(color.rgb(), this.rgb(), weight); diff --git a/test/harmony.test.ts b/test/harmony.test.ts index b5cb863..af0783e 100644 --- a/test/harmony.test.ts +++ b/test/harmony.test.ts @@ -8,53 +8,53 @@ extend([harmonyExtension]); describe('harmony extension', () => { it('should harmonies analogous', () => { expect(color1.harmonies('analogous').map((c) => c.hex())).toStrictEqual([ - '#A70D3E', - '#A7290D', - '#A7760D', + '#A70D3F', + '#A7280D', + '#A7750D', ]); }); it('should harmonies complementary', () => { expect(color1.harmonies('complementary').map((c) => c.hex())).toStrictEqual( - ['#A7290D', '#0D8BA7'], + ['#A7280D', '#0D8CA7'], ); }); it('should harmonies split complementary', () => { expect( color1.harmonies('split-complementary').map((c) => c.hex()), - ).toStrictEqual(['#A7290D', '#0DA776', '#0D3EA7']); + ).toStrictEqual(['#A7280D', '#0DA775', '#0D3FA7']); }); it('should harmonies double split complementary', () => { expect( color1.harmonies('double-split-complementary').map((c) => c.hex()), - ).toStrictEqual(['#A70D3E', '#A7290D', '#A7760D', '#0DA776', '#0D3EA7']); + ).toStrictEqual(['#A70D3F', '#A7280D', '#A7750D', '#0DA775', '#0D3FA7']); }); it('should harmonies tetradic', () => { expect(color1.harmonies('tetradic').map((c) => c.hex())).toStrictEqual([ - '#A7290D', - '#3EA70D', - '#0D8BA7', - '#760DA7', + '#A7280D', + '#3FA70D', + '#0D8CA7', + '#750DA7', ]); }); it('should harmonies triadic', () => { expect(color1.harmonies('triadic').map((c) => c.hex())).toStrictEqual([ - '#A7290D', - '#0DA729', - '#290DA7', + '#A7280D', + '#0DA728', + '#280DA7', ]); }); it('should harmonies rectangle', () => { expect(color1.harmonies('rectangle').map((c) => c.hex())).toStrictEqual([ - '#A7290D', - '#8BA70D', - '#0D8BA7', - '#290DA7', + '#A7280D', + '#8CA70D', + '#0D8CA7', + '#280DA7', ]); }); });