Skip to content

Commit

Permalink
little fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyleen77 committed Mar 10, 2024
1 parent 7efeeea commit d483e88
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']
```

</details>
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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\"",
Expand Down
35 changes: 17 additions & 18 deletions src/colorblender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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,
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/extensions/ansi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -38,4 +38,4 @@ const nameExtension: Extensions = (Class, converters): void => {
);
};

export default nameExtension;
export default ansiExtension;
2 changes: 1 addition & 1 deletion src/extensions/mix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 17 additions & 17 deletions test/harmony.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
});
});

0 comments on commit d483e88

Please sign in to comment.