From 8ace6c6779ee9fb2456411418ed469245a975d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aura=20Rom=C3=A1n?= Date: Tue, 8 Aug 2023 08:36:33 +0200 Subject: [PATCH 1/2] feat(colord): add `toBase10` --- src/colord.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/colord.ts b/src/colord.ts index cbefe3e..a5b145f 100644 --- a/src/colord.ts +++ b/src/colord.ts @@ -51,11 +51,22 @@ export class Colord { /** * Same as calling `brightness() >= 0.5`. - * */ + */ public isLight(): boolean { return getBrightness(this.rgba) >= 0.5; } + /** + * Returns the base 10 representation of a color. + * When the alpha channel value of the color is less than 1, + * it outputs RGBA (32bits) instead of RGB (24bits). + */ + public toBase10(): number { + return this.rgba.a < 1 + ? (this.rgba.r << 24) + (this.rgba.g << 16) + (this.rgba.b << 8) + round(this.rgba.a * 255) + : (this.rgba.r << 16) + (this.rgba.g << 8) + this.rgba.b; + } + /** * Returns the hexadecimal representation of a color. * When the alpha channel value of the color is less than 1, From 72b419aaab95fd7ad5b11a71e23e7bc868020453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aura=20Rom=C3=A1n?= Date: Tue, 8 Aug 2023 10:37:30 +0200 Subject: [PATCH 2/2] tests: add output test for `colord.base10()` Since `.base10()` is similar to `toHex()`, we can use the same string to retrieve the number without modifying `lime`, which can lead to higher complexity in the tests as `colord` does not support numbers as input and would require an `if (typeof lime[format] === 'number') continue;` or an `if (format === 'base10') continue;` check otherwise. --- tests/colord.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/colord.test.ts b/tests/colord.test.ts index 2df4310..4edc57e 100644 --- a/tests/colord.test.ts +++ b/tests/colord.test.ts @@ -23,8 +23,10 @@ it("Converts between HEX, RGB, HSL and HSV color models properly", () => { }); it("Parses and converts a color", () => { + const base10 = parseInt((lime.hex as string).slice(1), 16); for (const format in lime) { const instance = colord(lime[format] as AnyColor); + expect(instance.toBase10()).toBe(base10); expect(instance.toHex()).toBe(lime.hex); expect(instance.toRgb()).toMatchObject(lime.rgba); expect(instance.toRgbString()).toBe(lime.rgbString);