Skip to content
Merged
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
14 changes: 14 additions & 0 deletions pkgs/id/web/src/cursor-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ describe("isLightColor", () => {
expect(isLightColor("ffffff")).toBe(true);
expect(isLightColor("000000")).toBe(false);
});

it("expands 3-digit shorthand hex per CSS rules", () => {
expect(isLightColor("#fff")).toBe(true);
expect(isLightColor("#ff0")).toBe(true);
expect(isLightColor("#000")).toBe(false);
expect(isLightColor("abc")).toBe(isLightColor("#aabbcc"));
});

it("treats malformed colors as dark instead of deciding via NaN", () => {
expect(isLightColor("")).toBe(false);
expect(isLightColor("red")).toBe(false);
expect(isLightColor("#gggggg")).toBe(false);
expect(isLightColor("#ffff")).toBe(false);
});
});

describe("getColorForClient", () => {
Expand Down
13 changes: 12 additions & 1 deletion pkgs/id/web/src/cursor-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@ export function getStrobeDurationMs(opacity: number): number {
* Uses relative luminance calculation.
*/
export function isLightColor(color: string): boolean {
const hex = color.replace("#", "");
let hex = color.replace("#", "");
// CSS shorthand hex (#fff) doubles each digit; without expansion the
// slices below misalign and the last component parses as NaN (#846).
if (/^[0-9a-fA-F]{3}$/.test(hex)) {
hex = hex
.split("")
.map((digit) => digit + digit)
.join("");
}
// Anything that is not exactly six hex digits is not a color we can
// judge; treat it as dark rather than letting a NaN luminance decide.
if (!/^[0-9a-fA-F]{6}$/.test(hex)) return false;
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
Expand Down
Loading