The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-2665: Wrong colors in dark mode in getTextColorForBackground function (#76592)
Jakub Chodorowiczfdb82ddafc06
diff --git a/.loki/reference/chrome_laptop_viz_TreemapChart_Dark_Theme.png b/.loki/reference/chrome_laptop_viz_TreemapChart_Dark_Theme.pngindex aee63b68476..b4d656a5bc9 100644Binary files a/.loki/reference/chrome_laptop_viz_TreemapChart_Dark_Theme.png and b/.loki/reference/chrome_laptop_viz_TreemapChart_Dark_Theme.png differdiff --git a/frontend/src/metabase/ui/colors/constants/base-colors.ts b/frontend/src/metabase/ui/colors/constants/base-colors.tsindex 1cdb77094d9..ce5e14d5c2c 100644--- a/frontend/src/metabase/ui/colors/constants/base-colors.ts+++ b/frontend/src/metabase/ui/colors/constants/base-colors.ts@@ -246,6 +246,7 @@ const baseColors = {/*** These base colors are only for use in defining `light.ts` and `dark.ts` themes.+ * One exemption are colors that are strictly non-theme dependent.** Don't use it for anything else or you will be fired.*/diff --git a/frontend/src/metabase/ui/colors/palette.ts b/frontend/src/metabase/ui/colors/palette.tsindex 749591f4efc..f63a3c4fe06 100644--- a/frontend/src/metabase/ui/colors/palette.ts+++ b/frontend/src/metabase/ui/colors/palette.ts@@ -1,6 +1,7 @@import Color from "color";import { colors, getColors } from "./colors";+import { getBaseColorsForThemeDefinitionOnly } from "./constants/base-colors";import type { ColorGetter, ColorName, ColorPalette } from "./types";export const ACCENT_COUNT = 8;@@ -138,19 +139,22 @@ export const getFocusColor = (// https://www.notion.so/Maz-notes-on-viz-settings-67aed0e4ddcc4d4a83028992c4301820?d=513f4f7fa9c143cb874c7e4525dfb1e9#277d6b3eeb464eac86088abd144fde9econst whiteTextColorPriorityFactor = 3;+// The text color must contrast with the given background,+// which is a fixed independent of the app's light/dark theme.+const baseColors = getBaseColorsForThemeDefinitionOnly();+const LIGHT_TEXT_COLOR = baseColors.orionAlphaInverse[80];+const DARK_TEXT_COLOR = baseColors.orionAlpha[80];+export const getTextColorForBackground = (backgroundColor: string,getColor: ColorGetter = color,) => {- const whiteTextContrast =- Color(getColor(backgroundColor)).contrast(- Color(getColor("text-primary-inverse")),- ) * whiteTextColorPriorityFactor;- const darkTextContrast = Color(getColor(backgroundColor)).contrast(- Color(getColor("text-primary")),- );-- return whiteTextContrast > darkTextContrast- ? getColor("text-primary-inverse")- : getColor("text-primary");+ const background = Color(getColor(backgroundColor));+ const lightTextContrast =+ background.contrast(Color(LIGHT_TEXT_COLOR)) * whiteTextColorPriorityFactor;+ const darkTextContrast = background.contrast(Color(DARK_TEXT_COLOR));++ return lightTextContrast > darkTextContrast+ ? LIGHT_TEXT_COLOR+ : DARK_TEXT_COLOR;};diff --git a/frontend/src/metabase/ui/colors/palette.unit.spec.ts b/frontend/src/metabase/ui/colors/palette.unit.spec.tsindex 7a9ee3bb870..d88fb38fc50 100644--- a/frontend/src/metabase/ui/colors/palette.unit.spec.ts+++ b/frontend/src/metabase/ui/colors/palette.unit.spec.ts@@ -1,5 +1,8 @@+import Color from "color";+import { colors } from "./colors";-import { color } from "./palette";+import { color, getTextColorForBackground } from "./palette";+import type { ColorGetter } from "./types";describe("palette", () => {it("should get a color from the palette", () => {@@ -19,3 +22,32 @@ describe("palette", () => {expect(color("accent1-light")).toBeDefined();});});++describe("getTextColorForBackground", () => {+ const DARK_FILL = "#8254ab";+ const LIGHT_FILL = "#ffffcc";++ const isLight = (c: string) => Color(c).luminosity() > 0.5;++ // `text-primary` and `text-primary-inverse` swap meanings between the light and dark themes+ const lightThemeGetColor: ColorGetter = (name) =>+ ({ "text-primary": "#1a1a1a", "text-primary-inverse": "#ffffff" })[name] ??+ name;+ const darkThemeGetColor: ColorGetter = (name) =>+ ({ "text-primary": "#ffffff", "text-primary-inverse": "#1a1a1a" })[name] ??+ name;++ it("returns light text on a dark fill and dark text on a light fill", () => {+ expect(isLight(getTextColorForBackground(DARK_FILL))).toBe(true);+ expect(isLight(getTextColorForBackground(LIGHT_FILL))).toBe(false);+ });++ it("returns the same text color regardless of the theme getColor", () => {+ expect(getTextColorForBackground(DARK_FILL, darkThemeGetColor)).toEqual(+ getTextColorForBackground(DARK_FILL, lightThemeGetColor),+ );+ expect(getTextColorForBackground(LIGHT_FILL, darkThemeGetColor)).toEqual(+ getTextColorForBackground(LIGHT_FILL, lightThemeGetColor),+ );+ });+});