Update Appearance.tsx

This commit is contained in:
Ryan Alexander 2021-08-18 10:11:17 +10:00 committed by GitHub
parent 457097ade4
commit 98d1f06ce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -373,13 +373,10 @@ export function Component(props: Props) {
/>
</div>
<span
style={`color: ${
theme[x].toUpperCase() === "TRANSPARENT"
? getContrastingColour(
theme["primary-background"],
)
: getContrastingColour(theme[x])
}`}>
style={`color: ${getContrastingColour(
theme[x],
theme["primary-background"],
)}`}>
{x}
</span>
<div className={styles.override}>
@ -455,11 +452,15 @@ export const Appearance = connectState(Component, (state) => {
};
});
function getContrastingColour(hex: string) {
hex = hex.replace("#", "");
const r = parseInt(hex.substr(0, 2), 16);
const g = parseInt(hex.substr(2, 2), 16);
const b = parseInt(hex.substr(4, 2), 16);
const cc = (r * 299 + g * 587 + b * 114) / 1000;
return cc >= 75 ? "black" : "white";
function getContrastingColour(hex: string, fallback: string): string {
try {
hex = hex.replace("#", "");
const r = parseInt(hex.substr(0, 2), 16);
const g = parseInt(hex.substr(2, 2), 16);
const b = parseInt(hex.substr(4, 2), 16);
const cc = (r * 299 + g * 587 + b * 114) / 1000;
return cc >= 175 ? "black" : "white";
} catch (e) {
return getContrastingColour(fallback, "#fffff");
}
}