Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(quill-charts): pick label text color by apca contrast (#122030)

Sam Pennington23a56b9d4ad3
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index ff89b3a830d5..8048a7593a90 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -199,7 +199,7 @@ snapshots:
charts-heatmap--cell-labels--dark:
hash: v1.k794b7964.19b11a43aa9280e0b85498c1e87397d128cb1a1c9f1451419ab32de324783441.xEjoCIC6MhETYkz5iStffBD3Oc77sUugobUIKzq3EME
charts-heatmap--cell-labels--light:
- hash: v1.k794b7964.2cc87c7069cfc6f370663f581b6866d795754e5a3604bf5c9827ca81a605f2d0.QBtjIUNJc3gvAOkojmyfhJUQwB1HB0zUkm3rdoUr3D0
+ hash: v1.k794b7964.079b5cc67a8b51e1235f402297fcb1fcddbf60fbec81cdbb18d4bb76385cca67.kVs5ctRg_j0CW1xt0dbltRs8E7nvEE8u6oeDuiafyow
charts-heatmap--cell-labels-dropped-when-cells-are-small--dark:
hash: v1.k794b7964.8f0bf7a9fc58c93f4dc73333e7debc16ac9e8beb1397753f8bf7876528c6be5e.l9lHu-Urnd94-CElJYYehON5QJ85IHWjuWrcd6QzZAg
charts-heatmap--cell-labels-dropped-when-cells-are-small--light:
@@ -4091,7 +4091,7 @@ snapshots:
insights-retentionheatmap--realistic-curve--dark:
hash: v1.k794b7964.4f02030d0fd563d4d43cbcc27370f193baa7ace7644a5b0c22472d1c8a4ea4e1.LWPOR-QMZyhQUNC-x0RxzmEfG_nF3OcNbe68TtmBWAo
insights-retentionheatmap--realistic-curve--light:
- hash: v1.k794b7964.706c85b05f246352dc0440d0cf163336bbde11303f8bdd6719835f4b79bd9927.aaAit22MSaqvCwpgBcq5E0fMjjbaajpztd_42xZqHJo
+ hash: v1.k794b7964.686aefd17a89d1e73ac12efe7f06feb9f0c5a4b59b73c87ae7214cc9efcebb3d.ieDv1jPpvILFcSmVac3h_QmJkSq92Xh5vQNxvT28pmc
insights-retentionlinechart--default--dark:
hash: v1.k794b7964.ec5fcf8d9e4c3a49e94ffe769cb6147257f0e8fa0639f615e8d0f8a35d471492.jTfjU2lenc3lmyfgHtnetORnAO8bK6N9RMhAUqpTR7E
insights-retentionlinechart--default--light:
@@ -13455,9 +13455,9 @@ snapshots:
scenes-app-mcp-analytics-dashboard-cards--daily-tool-breakdown--light:
hash: v1.k794b7964.ef1d01e79c64ba3a610716272257daa8015654e54cc4ee09d6a9fa8ec91f6d41.wJlkho2QkeMjd3FbjVsFK37nxaziOX6bUnlEGyPtXHg
scenes-app-mcp-analytics-dashboard-cards--error-rate-by-tool--dark:
- hash: v1.k794b7964.3e9e147230095c6602e7cb5c25414cfab376bef36d895cde3ca3a709c18c6ae3.F1OrEv_affEy1PYJgo6RjuZCfSr2Bi6iYIhbICb3_94
+ hash: v1.k794b7964.3f656ffe6d8226b9f4b3f7a5251213c8543a1eab0556d2641c34b59f0f9cba8b.ohYeZoJUaMMj0N6IwVBtxAg0mFsVIy9O7l4c_JlAB7w
scenes-app-mcp-analytics-dashboard-cards--error-rate-by-tool--light:
- hash: v1.k794b7964.0c5f4e1cb28d098e43a7cb57082fc15f04cdf31ad6b842ac1f6590d95102dc14.Y5SYKK2ptHxU-rb2O9zDPruRrI7LFMmcP6AREWQABoQ
+ hash: v1.k794b7964.fe471dcf12c63b12241de6f9f29bddc63820281fbc857317a7fcd71e4b496b90.CNzpuJ2s8g8tWPT5ZMru2m8oYxIlk00e7mUNNO5bt-E
scenes-app-mcp-analytics-dashboard-cards--flagged-sessions--dark:
hash: v1.k794b7964.3cb2da2576280c91d6c8ded46d37cb622ff5a7ed1c5806a44c0fccf9efee0c0f.siDD7CL4vymuXEIyfWny7zcxf7Lo9I8GeuZ3SIODDrY
scenes-app-mcp-analytics-dashboard-cards--flagged-sessions--light:
@@ -15271,9 +15271,9 @@ snapshots:
scenes-app-surveys--survey-results--light:
hash: v1.k794b7964.87758b68551b35277bd0b576abc586689fb10db7008d23bd8af6a5e11c388e81.CWamhYwGuu-xmoseaC2gYIzTF0NmCcoSzSEDsvfDpkk
scenes-app-surveys--survey-summary-export--dark:
- hash: v1.k794b7964.939714ca04ee24ff16b27316ff41c6bdc44c84b3a73151a9264015400adad28d.631SJTGu5VHJuYY6FBAZs2M2V-GCPuPxAZa8IMYHS9s
+ hash: v1.k794b7964.248a55693556d924297d9a78344afe313cb10b30a18f04bd792e435877046b1e.r8ETvvcyvYdI8mYlE2QyzZClgwLTcazbNT8TcnlHtI8
scenes-app-surveys--survey-summary-export--light:
- hash: v1.k794b7964.117d4c4bf8074f5c074d913daee28b97c37d086d9152f96f60c328f4178ad1ed.rkLsbZ23kLk_NZ8p91LVl5LZ8vqs_iUK2INZP5-S4wM
+ hash: v1.k794b7964.fa67712a95fba6a01faa0b770538185585d4f3b85850425378f49a2036114c75.VO7wl076eCdnhi6Te2WjvOhs-OxFTBZwN6gcGQG2ctI
scenes-app-surveys--surveys-global-settings--dark:
hash: v1.k794b7964.476a85556bb374416ca1ae0cccfb23f8cc6f6a2e0e71d3f7d0e37a2ad3d463aa.lCKgctegJe2jQZ7Bf7sXK6YhuGh17KhGftYqoY3s5PM
scenes-app-surveys--surveys-global-settings--light:
diff --git a/packages/quill/packages/charts/src/charts/Heatmap/HeatmapCellLabels.tsx b/packages/quill/packages/charts/src/charts/Heatmap/HeatmapCellLabels.tsx
index f24c4c4872bd..e17a3afe76d1 100644
--- a/packages/quill/packages/charts/src/charts/Heatmap/HeatmapCellLabels.tsx
+++ b/packages/quill/packages/charts/src/charts/Heatmap/HeatmapCellLabels.tsx
@@ -1,7 +1,7 @@
import React, { useMemo } from 'react'
import { useChartLayout } from '../../core/chart-context'
-import { mixColors, perceivedLuminance, toSrgbColor } from '../../core/color-utils'
+import { mixColors, prefersLightText, toSrgbColor } from '../../core/color-utils'
import { FONT_FAMILY, measureLabelWidth } from '../../utils/text-measure'
import type { HeatmapCellDatum } from './Heatmap'
import type { HeatmapResolvedCellStyles } from './heatmap-cells'
@@ -21,8 +21,6 @@ const LABEL_FONT = `500 ${LABEL_FONT_SIZE}px ${FONT_FAMILY}`
const LABEL_PADDING_X = 4
// Under this the text touches the cell edges and reads as one smear across rows.
const MIN_CELL_HEIGHT = LABEL_FONT_SIZE + 4
-// Below this the fill is dark enough for white text; at or above it, dark text stays readable.
-const DARK_FILL_LUMINANCE = 0.55
// Not `theme.axisColor`: a dark theme makes that light gray, which vanishes on a bright cell.
const DARK_TEXT = '#111111'
@@ -114,7 +112,7 @@ export function HeatmapCellLabels({
text,
x: rect.x + rect.width / 2,
y: rect.y + rect.height / 2,
- color: perceivedLuminance(fill) < DARK_FILL_LUMINANCE ? '#ffffff' : DARK_TEXT,
+ color: prefersLightText(fill) ? '#ffffff' : DARK_TEXT,
})
}
}
diff --git a/packages/quill/packages/charts/src/core/color-utils.test.ts b/packages/quill/packages/charts/src/core/color-utils.test.ts
index 75094239abd5..61f4973caa07 100644
--- a/packages/quill/packages/charts/src/core/color-utils.test.ts
+++ b/packages/quill/packages/charts/src/core/color-utils.test.ts
@@ -1,4 +1,4 @@
-import { mixColors, resolveCssColor } from './color-utils'
+import { mixColors, prefersLightText, resolveCssColor } from './color-utils'
describe('mixColors', () => {
it.each([
@@ -71,3 +71,17 @@ describe('resolveCssColor', () => {
expect(resolveCssColor('var(--missing)', root)).toBe('var(--missing)')
})
})
+
+// A luminance cutoff or WCAG 2 picks black on the mid tones here, where white reads better.
+describe('prefersLightText', () => {
+ it.each([
+ { color: '#f14f58', expected: true },
+ { color: '#a56eff', expected: true },
+ { color: '#1d4aff', expected: true },
+ { color: '#e4a604', expected: false },
+ { color: '#41cbc4', expected: false },
+ { color: 'not-a-color', expected: false },
+ ])('$color -> $expected', ({ color, expected }) => {
+ expect(prefersLightText(color)).toBe(expected)
+ })
+})
diff --git a/packages/quill/packages/charts/src/core/color-utils.ts b/packages/quill/packages/charts/src/core/color-utils.ts
index c561c0ba1c6b..fffa5fb6f71b 100644
--- a/packages/quill/packages/charts/src/core/color-utils.ts
+++ b/packages/quill/packages/charts/src/core/color-utils.ts
@@ -74,15 +74,19 @@ export function toSrgbColor(color: string): string {
return a === 0 ? color : d3Rgb(r, g, b, a / 255).toString()
}
-/** Perceived luminance of a color, 0 (black) to 1 (white), on the ITU-R BT.601 weighting.
- * Use it to pick light or dark text over a fill. Unparseable input returns 1, so a caller
- * falls back to dark text. */
-export function perceivedLuminance(color: string): number {
+// APCA lightness of black text: 0 after the soft black clamp, raised to the text exponent.
+const APCA_BLACK_TEXT = (0.022 ** 1.414) ** 0.57
+
+/** Whether white text reads better than black over a fill, by APCA (the WCAG 3 draft contrast).
+ * A luminance cutoff or WCAG 2 picks black on mid tones such as #f14f58, where white reads
+ * better. Unparseable input returns false, so a caller falls back to dark text. */
+export function prefersLightText(color: string): boolean {
const c = d3Rgb(color)
if (Number.isNaN(c.r)) {
- return 1
+ return false
}
- return (0.299 * c.r + 0.587 * c.g + 0.114 * c.b) / 255
+ const y = 0.2126729 * (c.r / 255) ** 2.4 + 0.7151522 * (c.g / 255) ** 2.4 + 0.072175 * (c.b / 255) ** 2.4
+ return 1 - y ** 0.65 > y ** 0.56 - APCA_BLACK_TEXT
}
/** Fill color for the bar at `index`: the per-bar override (`bars[index].color`) when set, else the
diff --git a/packages/quill/packages/charts/src/overlays/ValueLabels.tsx b/packages/quill/packages/charts/src/overlays/ValueLabels.tsx
index a7c07e786141..40ed7c926a64 100644
--- a/packages/quill/packages/charts/src/overlays/ValueLabels.tsx
+++ b/packages/quill/packages/charts/src/overlays/ValueLabels.tsx
@@ -1,7 +1,7 @@
import React, { useMemo } from 'react'
import { useChartHover, useChartLayout } from '../core/chart-context'
-import { barColorAt, perceivedLuminance } from '../core/color-utils'
+import { barColorAt, prefersLightText } from '../core/color-utils'
import { resolveYScaleForSeries } from '../core/scales'
import type { ChartDimensions, ChartScales, ResolvedSeries, ResolveValueFn } from '../core/types'
import { getTextMeasureCtx } from '../utils/text-measure'
@@ -592,7 +592,7 @@ export function ValueLabels({
fontSize: resolvedFontSize,
color:
background || c.centerAnchor
- ? perceivedLuminance(c.color) < 0.5
+ ? prefersLightText(c.color)
? 'white'
: 'black'
: theme.axisColor,