Skip to content
Closed
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
5 changes: 3 additions & 2 deletions src/colorModels/hex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RgbaColor } from "../types";
import { clamp, round } from "../helpers";
import { ALPHA_PRECISION } from "../constants";
import { roundRgba } from "./rgb";

const hexMatcher = /^#([0-9a-f]{3,8})$/i;
Expand Down Expand Up @@ -38,7 +39,7 @@ export const parseHex = (hex: string): RgbaColor | null => {
r: digitAt(hex, 1) * 0x11,
g: digitAt(hex, 2) * 0x11,
b: digitAt(hex, 3) * 0x11,
a: length === 5 ? round((digitAt(hex, 4) * 0x11) / 255, 2) : 1,
a: length === 5 ? round((digitAt(hex, 4) * 0x11) / 255, ALPHA_PRECISION) : 1,
};
}

Expand All @@ -47,7 +48,7 @@ export const parseHex = (hex: string): RgbaColor | null => {
r: byteAt(hex, 1),
g: byteAt(hex, 3),
b: byteAt(hex, 5),
a: length === 9 ? round(byteAt(hex, 7) / 255, 2) : 1,
a: length === 9 ? round(byteAt(hex, 7) / 255, ALPHA_PRECISION) : 1,
};
}

Expand Down
15 changes: 12 additions & 3 deletions tests/colord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { colord, random, getFormat, Colord, AnyColor } from "../src/";
import { fixtures, lime, saturationLevels } from "./fixtures";
import { clampHue, round, roundHue } from "../src/helpers";
import { ALPHA_PRECISION } from "../src/constants";

it("Converts between HEX, RGB, HSL and HSV color models properly", () => {
for (const fixture of fixtures) {
Expand Down Expand Up @@ -126,7 +127,7 @@ it("Parses modern HSL functional notations", () => {

it("Supports HEX4 and HEX8 color models", () => {
expect(colord("#ffffffff").toRgb()).toMatchObject({ r: 255, g: 255, b: 255, a: 1 });
expect(colord("#80808080").toRgb()).toMatchObject({ r: 128, g: 128, b: 128, a: 0.5 });
expect(colord("#80808080").toRgb()).toMatchObject({ r: 128, g: 128, b: 128, a: 0.502 });
expect(colord("#AAAF").toRgb()).toMatchObject({ r: 170, g: 170, b: 170, a: 1 });
expect(colord("#5550").toRgb()).toMatchObject({ r: 85, g: 85, b: 85, a: 0 });
expect(colord({ r: 255, g: 255, b: 255, a: 1 }).toHex()).toBe("#ffffff");
Expand All @@ -146,7 +147,7 @@ it("Parses and serializes every HEX digit exactly", () => {
r: byte,
g: byte,
b: byte,
a: round(byte / 255, 2),
a: round(byte / 255, ALPHA_PRECISION),
});
}

Expand All @@ -164,7 +165,7 @@ it("Parses and serializes every HEX digit exactly", () => {
r: 0,
g: 0,
b: 0,
a: round(byte / 255, 2),
a: round(byte / 255, ALPHA_PRECISION),
});
}

Expand All @@ -181,6 +182,14 @@ it("Parses and serializes every HEX digit exactly", () => {
expect(colord("# 12345").isValid()).toBe(false);
});

it("Round-trips a HEX8 alpha channel without drifting", () => {
// The alpha byte of a HEX8 string is 8-bit; parsing must keep enough
// precision (ALPHA_PRECISION) to re-encode the very same byte.
["#476d5e55", "#80808080", "#0a141e7f", "#ffffff01", "#000000fe"].forEach((hex) => {
expect(colord(hex).toHex()).toBe(hex);
});
});

it("Ignores a case and extra whitespace", () => {
expect(colord(" #0a0a0a ").toRgb()).toMatchObject({ r: 10, g: 10, b: 10, a: 1 });
expect(colord("RGB( 10, 10, 10 )").toRgb()).toMatchObject({ r: 10, g: 10, b: 10, a: 1 });
Expand Down
12 changes: 6 additions & 6 deletions tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("cmyk", () => {
// https://en.wikipedia.org/wiki/CMYK_color_model
expect(colord("#999966").toCmykString()).toBe("device-cmyk(0% 0% 33% 40%)");
expect(colord("#99ffff").toCmykString()).toBe("device-cmyk(40% 0% 0% 0%)");
expect(colord("#00336680").toCmykString()).toBe("device-cmyk(100% 50% 0% 60% / 0.5)");
expect(colord("#00336680").toCmykString()).toBe("device-cmyk(100% 50% 0% 60% / 0.502)");
});

it("Supported by `getFormat`", () => {
Expand Down Expand Up @@ -196,7 +196,7 @@ describe("hwb", () => {
// https://en.wikipedia.org/wiki/HWB_color_model
expect(colord("#999966").toHwbString()).toBe("hwb(60 40% 40%)");
expect(colord("#99ffff").toHwbString()).toBe("hwb(180 60% 0%)");
expect(colord("#00336680").toHwbString()).toBe("hwb(210 0% 60% / 0.5)");
expect(colord("#00336680").toHwbString()).toBe("hwb(210 0% 60% / 0.502)");
});

it("Supports all valid CSS angle units", () => {
Expand Down Expand Up @@ -233,7 +233,7 @@ describe("lab", () => {
expect(colord("#00ff00").toLab()).toMatchObject({ l: 87.82, a: -79.29, b: 80.99, alpha: 1 });
expect(colord("#ffff00").toLab()).toMatchObject({ l: 97.61, a: -15.75, b: 93.39, alpha: 1 });
expect(colord("#aabbcc").toLab()).toMatchObject({ l: 74.97, a: -3.4, b: -10.7, alpha: 1 });
expect(colord("#33221180").toLab()).toMatchObject({ l: 15.05, a: 6.68, b: 14.59, alpha: 0.5 });
expect(colord("#33221180").toLab()).toMatchObject({ l: 15.05, a: 6.68, b: 14.59, alpha: 0.502 });
expect(colord("#d53987").toLab()).toMatchObject({ l: 50.93, a: 64.96, b: -6.38, alpha: 1 });
});

Expand Down Expand Up @@ -298,7 +298,7 @@ describe("lch", () => {
expect(colord("#c65d06").toLch()).toMatchObject({ l: 52.31, c: 72.21, h: 56.33, a: 1 });
expect(colord("#9d9318").toLch()).toMatchObject({ l: 60.31, c: 59.2, h: 95.46, a: 1 });
expect(colord("#68a639").toLch()).toMatchObject({ l: 62.22, c: 59.15, h: 126.15, a: 1 });
expect(colord("#62acef80").toLch()).toMatchObject({ l: 67.67, c: 42.18, h: 257.79, a: 0.5 });
expect(colord("#62acef80").toLch()).toMatchObject({ l: 67.67, c: 42.18, h: 257.79, a: 0.502 });
});

it("Keeps the LCH hue within [0, 360)", () => {
Expand All @@ -311,9 +311,9 @@ describe("lch", () => {

it("Converts a color to CIE LCH string (CSS functional notation)", () => {
// https://cielab.xyz/colorconv/
expect(colord("#00000080").toLchString()).toBe("lch(0% 0 0 / 0.5)");
expect(colord("#00000080").toLchString()).toBe("lch(0% 0 0 / 0.502)");
expect(colord("#ffffff").toLchString()).toBe("lch(100% 0 0)");
expect(colord("#c65d06ed").toLchString()).toBe("lch(52.31% 72.21 56.33 / 0.93)");
expect(colord("#c65d06ed").toLchString()).toBe("lch(52.31% 72.21 56.33 / 0.929)");
expect(colord("#aabbcc").toLchString()).toBe("lch(74.97% 11.22 252.37)");
});

Expand Down
Loading