Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(colord): add toBase10 #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/colord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/colord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down