Fix theme2hex with custom theme colors (#24282)

This commit is contained in:
Adam Kapos 2025-02-18 14:04:46 +07:00 committed by GitHub
parent c52217c1ce
commit 0256da511d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View File

@ -136,11 +136,18 @@ export function theme2hex(themeColor: string): string {
}
const rgbFromColorName = colors[themeColor];
if (!rgbFromColorName) {
// We have a named color, and there's nothing in the table,
// so nothing further we can do with it.
// Compare/border/background color will all be the same.
return themeColor;
if (rgbFromColorName) {
return rgb2hex(rgbFromColorName);
}
return rgb2hex(rgbFromColorName);
const rgbMatch = themeColor.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/);
if (rgbMatch) {
const [, r, g, b] = rgbMatch.map(Number);
return rgb2hex([r, g, b]);
}
// We have a named color, and there's nothing in the table,
// so nothing further we can do with it.
// Compare/border/background color will all be the same.
return themeColor;
}

View File

@ -51,4 +51,16 @@ describe("Color Conversion Tests", () => {
expect(theme2hex("#ff0000")).toBe("#ff0000");
expect(theme2hex("unicorn")).toBe("unicorn");
});
it("should convert rgb theme color to hex", () => {
expect(theme2hex("rgb( 255, 0, 0)")).toBe("#ff0000");
expect(theme2hex("rgb(0,255, 0)")).toBe("#00ff00");
expect(theme2hex("rgb(0, 0,255 )")).toBe("#0000ff");
});
it("should convert rgba theme color to hex by ignoring alpha", () => {
expect(theme2hex("rgba( 255, 0, 0, 0.5)")).toBe("#ff0000");
expect(theme2hex("rgba(0,255, 0, 0.3)")).toBe("#00ff00");
expect(theme2hex("rgba(0, 0,255 , 0.7)")).toBe("#0000ff");
});
});