Skip to content

A local-first personal finance app

Compute color of tags text for dark mode (#5574)

POGMAN645342d47d33
diff --git a/packages/desktop-client/src/style/tags.ts b/packages/desktop-client/src/style/tags.ts
index 95aeacae1f..5f7af04d59 100644
--- a/packages/desktop-client/src/style/tags.ts
+++ b/packages/desktop-client/src/style/tags.ts
@@ -24,7 +24,25 @@ export function useTags() {
return tags;
}
+function getContrastedColor(hexcolor: string) {
+ // see: https://www.w3.org/TR/AERT/#color-contrast
+ const r = parseInt(hexcolor.substring(1, 3), 16);
+ const g = parseInt(hexcolor.substring(3, 5), 16);
+ const b = parseInt(hexcolor.substring(5, 7), 16);
+ const brightnessDiff = (r * 299 + g * 587 + b * 114) / 1000;
+
+ return brightnessDiff >= 125 ? 'black' : 'white';
+}
+
function getTagCSSColors(theme: Theme, color?: string | null) {
+ if (!color) {
+ return [
+ themeStyle.noteTagText,
+ themeStyle.noteTagBackground,
+ themeStyle.noteTagBackgroundHover,
+ ];
+ }
+
if (theme === 'auto') {
theme = window.matchMedia('(prefers-color-scheme: light)').matches
? 'light'
@@ -33,21 +51,15 @@ function getTagCSSColors(theme: Theme, color?: string | null) {
if (theme === 'light') {
return [
- color ? `${color} !important` : themeStyle.noteTagText,
- color
- ? `color-mix(in srgb, ${color} 15%, white)`
- : themeStyle.noteTagBackground,
- color
- ? `color-mix(in srgb, ${color} 25%, white)`
- : themeStyle.noteTagBackgroundHover,
+ `${color} !important`,
+ `color-mix(in srgb, ${color} 15%, white)`,
+ `color-mix(in srgb, ${color} 25%, white)`,
];
} else {
return [
- themeStyle.noteTagText,
- color ?? themeStyle.noteTagBackground,
- color
- ? `color-mix(in srgb, ${color} 85%, white)`
- : themeStyle.noteTagBackgroundHover,
+ getContrastedColor(color),
+ color,
+ `color-mix(in srgb, ${color} 85%, white)`,
];
}
}
diff --git a/upcoming-release-notes/5574.md b/upcoming-release-notes/5574.md
new file mode 100644
index 0000000000..2feb7f63a2
--- /dev/null
+++ b/upcoming-release-notes/5574.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [pogman-code]
+---
+
+Better readability of tags