An open source design system that's fully customizable and agent ready
TypeScript
fix(visual-gate): probe backgrounds keep their distance from the fixed probe text colour (#10125)
Cindy Zhangeb5e49661a26
diff --git a/.github/scripts/visual-gate/lib/probe-theme.mjs b/.github/scripts/visual-gate/lib/probe-theme.mjsindex 7ed597579c..1e4db136a8 100644--- a/.github/scripts/visual-gate/lib/probe-theme.mjs+++ b/.github/scripts/visual-gate/lib/probe-theme.mjs@@ -1,5 +1,7 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+import {PROBE_TOKENS} from './probe-axes.mjs';+/*** @file The probe theme: a theme that styles every declared theming target.*@@ -46,6 +48,53 @@ export function probeColor(seed, options = {}) {return `hsl(${hue.toFixed(1)} ${saturation}% ${lightness}%)`;}+/**+ * Parse `hsl(h s% l%)` or `rgb(r, g, b)` into 0–255 channels.+ * @param {string} color+ * @returns {[number, number, number]}+ */+function toRgb(color) {+ const rgb = /^rgb\(\s*([\d.]+),\s*([\d.]+),\s*([\d.]+)\s*\)$/.exec(color);+ if (rgb) {+ return [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])];+ }+ const hsl = /^hsl\(\s*([\d.]+)\s+([\d.]+)%\s+([\d.]+)%\s*\)$/.exec(color);+ if (!hsl) {+ throw new Error(`probe-theme: cannot read colour ${color}`);+ }+ const hue = Number(hsl[1]);+ const saturation = Number(hsl[2]) / 100;+ const lightness = Number(hsl[3]) / 100;+ const chroma = saturation * Math.min(lightness, 1 - lightness);+ const channel = n => {+ const k = (n + hue / 30) % 12;+ return 255 * (lightness - chroma * Math.max(-1, Math.min(k - 3, 9 - k, 1)));+ };+ return [channel(0), channel(8), channel(4)];+}++/**+ * How far apart two colours are as pixels: the Euclidean distance between+ * their 0–255 RGB channels. The gate compares pixels, so this is the distance+ * that decides whether it can tell two colours apart.+ * @param {string} first+ * @param {string} second+ * @returns {number}+ */+export function colorDistance(first, second) {+ const [a, b] = [toRgb(first), toRgb(second)];+ return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);+}++/**+ * The fixed probe text colour a child keeps when its own style does not set+ * one. A generated background stays at least `MIN_TEXT_DISTANCE` away from it,+ * or text that keeps the token draws in its own background's colour and the+ * gate cannot see it reach the pixels.+ */+const FIXED_TEXT_COLORS = [PROBE_TOKENS['--color-text-primary']];+const MIN_TEXT_DISTANCE = 60;+/*** Prop values from a doc's `type` union — `'a' | 'b' | 'c'` → ['a','b','c'].*@@ -158,8 +207,18 @@ export function buildProbeComponents(targets, propsByComponent, aliases = {}) {* @returns {{backgroundColor: string, color: string, borderColor: string, outlineColor: string}}*/export function paint(seed) {+ let backgroundColor = probeColor(seed);+ for (+ let attempt = 1;+ FIXED_TEXT_COLORS.some(+ text => colorDistance(backgroundColor, text) < MIN_TEXT_DISTANCE,+ );+ attempt += 1+ ) {+ backgroundColor = probeColor(`${seed}/background-${attempt}`);+ }return {- backgroundColor: probeColor(seed),+ backgroundColor,color: probeColor(`${seed}/text`, {lightness: 12}),borderColor: probeColor(`${seed}/border`, {lightness: 25}),outlineColor: probeColor(`${seed}/outline`, {lightness: 25}),diff --git a/.github/scripts/visual-gate/lib/probe-theme.test.mjs b/.github/scripts/visual-gate/lib/probe-theme.test.mjsindex 1750cbc307..e0e18e872f 100644--- a/.github/scripts/visual-gate/lib/probe-theme.test.mjs+++ b/.github/scripts/visual-gate/lib/probe-theme.test.mjs@@ -1,8 +1,17 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+import {readFileSync} from 'node:fs';+import {fileURLToPath} from 'node:url';import {describe, expect, it} from 'vitest';-import {buildProbeComponents, probeColor, unionValues} from './probe-theme.mjs';+import {+ buildProbeComponents,+ colorDistance,+ paint,+ probeColor,+ unionValues,+} from './probe-theme.mjs';+import {PROBE_TOKENS} from './probe-axes.mjs';describe('unionValues', () => {it('reads an inline string union', () => {@@ -119,3 +128,37 @@ describe('buildProbeComponents', () => {);});});++describe('paint', () => {+ // A background that matches the fixed probe text colour hides the text of a+ // child that keeps the token, so the gate cannot see that text reach the+ // pixels.+ const fixedText = PROBE_TOKENS['--color-text-primary'];++ it('keeps an open Collapsible content background distinct from the fixed probe text colour', () => {+ const {backgroundColor} = paint('collapsible-content.open');+ expect(colorDistance(backgroundColor, fixedText)).toBeGreaterThanOrEqual(+ 60,+ );+ });++ it('keeps every background in the generated probe theme distinct from the fixed probe text colour', () => {+ const source = readFileSync(+ fileURLToPath(+ new URL(+ '../../../../packages/themes/probe/src/probeTheme.ts',+ import.meta.url,+ ),+ ),+ 'utf8',+ );+ const backgrounds = [...source.matchAll(/backgroundColor: '([^']+)'/g)].map(+ match => match[1],+ );+ expect(backgrounds.length).toBeGreaterThan(100);+ const collisions = backgrounds.filter(+ color => colorDistance(color, fixedText) < 60,+ );+ expect(collisions).toEqual([]);+ });+});diff --git a/packages/themes/probe/src/probeTheme.ts b/packages/themes/probe/src/probeTheme.tsindex db41b74f39..9b8143698e 100644--- a/packages/themes/probe/src/probeTheme.ts+++ b/packages/themes/probe/src/probeTheme.ts@@ -412,7 +412,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(193.6 71% 25%)',},'variant:info': {- backgroundColor: 'hsl(55.2 85% 53%)',+ backgroundColor: 'hsl(36.8 74% 51%)',color: 'hsl(239.0 82% 12%)',borderColor: 'hsl(188.3 75% 25%)',outlineColor: 'hsl(252.4 91% 25%)',@@ -424,7 +424,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(78.0 91% 25%)',},'variant:warning': {- backgroundColor: 'hsl(60.1 91% 56%)',+ backgroundColor: 'hsl(132.5 76% 56%)',color: 'hsl(341.6 86% 12%)',borderColor: 'hsl(156.8 83% 25%)',outlineColor: 'hsl(331.6 88% 25%)',@@ -1356,7 +1356,7 @@ export const probeTheme = defineTheme({},code: {base: {- backgroundColor: 'hsl(60.9 94% 51%)',+ backgroundColor: 'hsl(31.7 73% 53%)',color: 'hsl(78.0 91% 12%)',borderColor: 'hsl(154.6 86% 25%)',outlineColor: 'hsl(60.5 83% 25%)',@@ -1434,7 +1434,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(119.2 94% 25%)',},'size:md': {- backgroundColor: 'hsl(73.7 93% 51%)',+ backgroundColor: 'hsl(275.6 82% 57%)',color: 'hsl(33.1 86% 12%)',borderColor: 'hsl(232.8 80% 25%)',outlineColor: 'hsl(35.6 77% 25%)',@@ -1494,7 +1494,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(8.4 83% 25%)',},open: {- backgroundColor: 'hsl(61.9 92% 57%)',+ backgroundColor: 'hsl(251.9 86% 50%)',color: 'hsl(344.5 93% 12%)',borderColor: 'hsl(91.5 89% 25%)',outlineColor: 'hsl(34.0 73% 25%)',@@ -1516,7 +1516,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(179.2 89% 25%)',},'chevronPosition:start': {- backgroundColor: 'hsl(72.1 87% 50%)',+ backgroundColor: 'hsl(248.5 76% 62%)',color: 'hsl(2.4 77% 12%)',borderColor: 'hsl(354.3 70% 25%)',outlineColor: 'hsl(198.5 73% 25%)',@@ -1528,7 +1528,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(194.0 81% 25%)',},open: {- backgroundColor: 'hsl(61.4 94% 58%)',+ backgroundColor: 'hsl(336.5 91% 48%)',color: 'hsl(352.8 88% 12%)',borderColor: 'hsl(175.6 82% 25%)',outlineColor: 'hsl(222.5 87% 25%)',@@ -1910,7 +1910,7 @@ export const probeTheme = defineTheme({},'date-time-input-time-segment': {base: {- backgroundColor: 'hsl(66.6 93% 55%)',+ backgroundColor: 'hsl(327.5 74% 46%)',color: 'hsl(144.2 75% 12%)',borderColor: 'hsl(237.4 74% 25%)',outlineColor: 'hsl(337.6 91% 25%)',@@ -1934,7 +1934,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(98.1 75% 25%)',},'status:warning': {- backgroundColor: 'hsl(52.4 84% 50%)',+ backgroundColor: 'hsl(294.3 87% 51%)',color: 'hsl(178.5 90% 12%)',borderColor: 'hsl(45.3 78% 25%)',outlineColor: 'hsl(10.2 83% 25%)',@@ -1968,7 +1968,7 @@ export const probeTheme = defineTheme({},dialog: {base: {- backgroundColor: 'hsl(63.5 79% 52%)',+ backgroundColor: 'hsl(21.8 71% 48%)',color: 'hsl(341.6 70% 12%)',borderColor: 'hsl(178.3 72% 25%)',outlineColor: 'hsl(145.4 84% 25%)',@@ -3250,7 +3250,7 @@ export const probeTheme = defineTheme({},'multi-selector-section-heading': {base: {- backgroundColor: 'hsl(63.2 80% 52%)',+ backgroundColor: 'hsl(152.7 79% 49%)',color: 'hsl(168.1 74% 12%)',borderColor: 'hsl(236.4 89% 25%)',outlineColor: 'hsl(210.9 85% 25%)',@@ -3302,7 +3302,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(47.2 81% 25%)',},'size:lg': {- backgroundColor: 'hsl(63.3 76% 53%)',+ backgroundColor: 'hsl(22.2 87% 48%)',color: 'hsl(81.8 76% 12%)',borderColor: 'hsl(113.7 82% 25%)',outlineColor: 'hsl(60.5 90% 25%)',@@ -3667,7 +3667,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(41.4 75% 25%)',},'variant:accent': {- backgroundColor: 'hsl(54.9 82% 50%)',+ backgroundColor: 'hsl(29.8 76% 63%)',color: 'hsl(72.7 79% 12%)',borderColor: 'hsl(332.1 80% 25%)',outlineColor: 'hsl(222.1 87% 25%)',@@ -3917,7 +3917,7 @@ export const probeTheme = defineTheme({},'segmented-control-item': {base: {- backgroundColor: 'hsl(56.9 92% 51%)',+ backgroundColor: 'hsl(114.3 92% 54%)',color: 'hsl(86.5 86% 12%)',borderColor: 'hsl(315.9 84% 25%)',outlineColor: 'hsl(88.5 77% 25%)',@@ -4083,7 +4083,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(252.8 75% 25%)',},'status:error': {- backgroundColor: 'hsl(73.9 91% 56%)',+ backgroundColor: 'hsl(238.0 90% 49%)',color: 'hsl(4.3 93% 12%)',borderColor: 'hsl(56.1 87% 25%)',outlineColor: 'hsl(223.6 74% 25%)',@@ -4455,7 +4455,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(174.9 73% 25%)',},'variant:error': {- backgroundColor: 'hsl(68.5 84% 54%)',+ backgroundColor: 'hsl(257.4 88% 62%)',color: 'hsl(281.9 80% 12%)',borderColor: 'hsl(63.1 87% 25%)',outlineColor: 'hsl(180.3 88% 25%)',@@ -4559,7 +4559,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(70.0 85% 25%)',},'size:md': {- backgroundColor: 'hsl(58.5 93% 49%)',+ backgroundColor: 'hsl(242.9 74% 58%)',color: 'hsl(187.4 72% 12%)',borderColor: 'hsl(347.9 83% 25%)',outlineColor: 'hsl(346.4 92% 25%)',@@ -4857,7 +4857,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(110.9 72% 25%)',},'type:label': {- backgroundColor: 'hsl(65.1 91% 45%)',+ backgroundColor: 'hsl(5.7 91% 60%)',color: 'hsl(339.4 73% 12%)',borderColor: 'hsl(229.2 75% 25%)',outlineColor: 'hsl(255.1 72% 25%)',@@ -5105,7 +5105,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(38.5 91% 25%)',},'status:warning': {- backgroundColor: 'hsl(61.2 92% 58%)',+ backgroundColor: 'hsl(251.6 79% 58%)',color: 'hsl(27.1 82% 12%)',borderColor: 'hsl(7.8 81% 25%)',outlineColor: 'hsl(90.2 75% 25%)',@@ -5169,13 +5169,13 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(272.3 76% 25%)',},'status:error': {- backgroundColor: 'hsl(58.3 79% 48%)',+ backgroundColor: 'hsl(45.8 71% 56%)',color: 'hsl(57.7 71% 12%)',borderColor: 'hsl(245.4 78% 25%)',outlineColor: 'hsl(108.0 79% 25%)',},'status:success': {- backgroundColor: 'hsl(63.3 82% 50%)',+ backgroundColor: 'hsl(260.2 93% 52%)',color: 'hsl(272.7 74% 12%)',borderColor: 'hsl(157.4 87% 25%)',outlineColor: 'hsl(321.9 91% 25%)',@@ -5329,7 +5329,7 @@ export const probeTheme = defineTheme({outlineColor: 'hsl(192.0 93% 25%)',},'format:date_time': {- backgroundColor: 'hsl(59.1 88% 56%)',+ backgroundColor: 'hsl(111.8 81% 52%)',color: 'hsl(143.9 82% 12%)',borderColor: 'hsl(185.6 89% 25%)',outlineColor: 'hsl(196.1 91% 25%)',@@ -5375,7 +5375,7 @@ export const probeTheme = defineTheme({},toast: {base: {- backgroundColor: 'hsl(59.6 86% 51%)',+ backgroundColor: 'hsl(238.9 77% 45%)',color: 'hsl(313.0 75% 12%)',borderColor: 'hsl(181.3 92% 25%)',outlineColor: 'hsl(231.0 85% 25%)',@@ -5661,7 +5661,7 @@ export const probeTheme = defineTheme({},'top-nav-mega-menu-featured-card': {base: {- backgroundColor: 'hsl(70.2 84% 49%)',+ backgroundColor: 'hsl(181.2 80% 53%)',color: 'hsl(36.2 84% 12%)',borderColor: 'hsl(15.0 77% 25%)',outlineColor: 'hsl(64.2 85% 25%)',@@ -5755,7 +5755,7 @@ export const probeTheme = defineTheme({},'trigger-menu': {base: {- backgroundColor: 'hsl(70.8 81% 51%)',+ backgroundColor: 'hsl(38.1 75% 58%)',color: 'hsl(358.2 75% 12%)',borderColor: 'hsl(139.7 78% 25%)',outlineColor: 'hsl(18.8 77% 25%)',