mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-19 07:16:39 +00:00
Fix theme2hex with custom theme colors (#24282)
This commit is contained in:
parent
c52217c1ce
commit
0256da511d
@ -136,11 +136,18 @@ export function theme2hex(themeColor: string): string {
|
||||
}
|
||||
|
||||
const rgbFromColorName = colors[themeColor];
|
||||
if (!rgbFromColorName) {
|
||||
if (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;
|
||||
}
|
||||
return rgb2hex(rgbFromColorName);
|
||||
}
|
||||
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user