forked from home-assistant/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce dark mode and primary color picker (home-assistant#6430)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
- Loading branch information
1 parent
0d515e2
commit 4ca13c4
Showing
45 changed files
with
814 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
const expand_hex = (hex: string): string => { | ||
let result = ""; | ||
for (const val of hex) { | ||
result += val + val; | ||
} | ||
return result; | ||
}; | ||
|
||
const rgb_hex = (component: number): string => { | ||
const hex = Math.round(Math.min(Math.max(component, 0), 255)).toString(16); | ||
return hex.length === 1 ? `0${hex}` : hex; | ||
}; | ||
|
||
// Conversion between HEX and RGB | ||
|
||
export const hex2rgb = (hex: string): [number, number, number] => { | ||
hex = hex.replace("#", ""); | ||
if (hex.length === 3 || hex.length === 4) { | ||
hex = expand_hex(hex); | ||
} | ||
|
||
return [ | ||
parseInt(hex.substring(0, 2), 16), | ||
parseInt(hex.substring(2, 4), 16), | ||
parseInt(hex.substring(4, 6), 16), | ||
]; | ||
}; | ||
|
||
export const rgb2hex = (rgb: [number, number, number]): string => { | ||
return `#${rgb_hex(rgb[0])}${rgb_hex(rgb[1])}${rgb_hex(rgb[2])}`; | ||
}; | ||
|
||
// Conversion between LAB, XYZ and RGB from https://github.com/gka/chroma.js | ||
// Copyright (c) 2011-2019, Gregor Aisch | ||
|
||
// Constants for XYZ and LAB conversion | ||
const Xn = 0.95047; | ||
const Yn = 1; | ||
const Zn = 1.08883; | ||
|
||
const t0 = 0.137931034; // 4 / 29 | ||
const t1 = 0.206896552; // 6 / 29 | ||
const t2 = 0.12841855; // 3 * t1 * t1 | ||
const t3 = 0.008856452; // t1 * t1 * t1 | ||
|
||
const rgb_xyz = (r: number) => { | ||
r /= 255; | ||
if (r <= 0.04045) { | ||
return r / 12.92; | ||
} | ||
return ((r + 0.055) / 1.055) ** 2.4; | ||
}; | ||
|
||
const xyz_lab = (t: number) => { | ||
if (t > t3) { | ||
return t ** (1 / 3); | ||
} | ||
return t / t2 + t0; | ||
}; | ||
|
||
const xyz_rgb = (r: number) => { | ||
return 255 * (r <= 0.00304 ? 12.92 * r : 1.055 * r ** (1 / 2.4) - 0.055); | ||
}; | ||
|
||
const lab_xyz = (t: number) => { | ||
return t > t1 ? t * t * t : t2 * (t - t0); | ||
}; | ||
|
||
// Conversions between RGB and LAB | ||
|
||
const rgb2xyz = (rgb: [number, number, number]): [number, number, number] => { | ||
let [r, g, b] = rgb; | ||
r = rgb_xyz(r); | ||
g = rgb_xyz(g); | ||
b = rgb_xyz(b); | ||
const x = xyz_lab((0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / Xn); | ||
const y = xyz_lab((0.2126729 * r + 0.7151522 * g + 0.072175 * b) / Yn); | ||
const z = xyz_lab((0.0193339 * r + 0.119192 * g + 0.9503041 * b) / Zn); | ||
return [x, y, z]; | ||
}; | ||
|
||
export const rgb2lab = ( | ||
rgb: [number, number, number] | ||
): [number, number, number] => { | ||
const [x, y, z] = rgb2xyz(rgb); | ||
const l = 116 * y - 16; | ||
return [l < 0 ? 0 : l, 500 * (x - y), 200 * (y - z)]; | ||
}; | ||
|
||
export const lab2rgb = ( | ||
lab: [number, number, number] | ||
): [number, number, number] => { | ||
const [l, a, b] = lab; | ||
|
||
let y = (l + 16) / 116; | ||
let x = isNaN(a) ? y : y + a / 500; | ||
let z = isNaN(b) ? y : y - b / 200; | ||
|
||
y = Yn * lab_xyz(y); | ||
x = Xn * lab_xyz(x); | ||
z = Zn * lab_xyz(z); | ||
|
||
const r = xyz_rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z); // D65 -> sRGB | ||
const g = xyz_rgb(-0.969266 * x + 1.8760108 * y + 0.041556 * z); | ||
const b_ = xyz_rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z); | ||
|
||
return [r, g, b_]; | ||
}; | ||
|
||
export const lab2hex = (lab: [number, number, number]): string => { | ||
const rgb = lab2rgb(lab); | ||
return rgb2hex(rgb); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// From https://github.com/gka/chroma.js | ||
// Copyright (c) 2011-2019, Gregor Aisch | ||
|
||
export const labDarken = ( | ||
lab: [number, number, number], | ||
amount = 1 | ||
): [number, number, number] => { | ||
return [lab[0] - 18 * amount, lab[1], lab[2]]; | ||
}; | ||
|
||
export const labBrighten = ( | ||
lab: [number, number, number], | ||
amount = 1 | ||
): [number, number, number] => { | ||
return labDarken(lab, -amount); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const luminosity = (rgb: [number, number, number]): number => { | ||
// http://www.w3.org/TR/WCAG20/#relativeluminancedef | ||
const lum: [number, number, number] = [0, 0, 0]; | ||
for (let i = 0; i < rgb.length; i++) { | ||
const chan = rgb[i] / 255; | ||
lum[i] = chan <= 0.03928 ? chan / 12.92 : ((chan + 0.055) / 1.055) ** 2.4; | ||
} | ||
|
||
return 0.2126 * lum[0] + 0.7152 * lum[1] + 0.0722 * lum[2]; | ||
}; | ||
|
||
export const rgbContrast = ( | ||
color1: [number, number, number], | ||
color2: [number, number, number] | ||
) => { | ||
const lum1 = luminosity(color1); | ||
const lum2 = luminosity(color2); | ||
|
||
if (lum1 > lum2) { | ||
return (lum1 + 0.05) / (lum2 + 0.05); | ||
} | ||
|
||
return (lum2 + 0.05) / (lum1 + 0.05); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.