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, 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);