@linktr.ee/messaging-react 3.24.2-rc-1785435591 → 3.25.0-rc-1785440433
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{Card-Cgn2G-VJ.js → Card-DcyixIfP.js} +2 -2
- package/dist/{Card-Cgn2G-VJ.js.map → Card-DcyixIfP.js.map} +1 -1
- package/dist/{Card-CRhkz5Jp.cjs → Card-_fDH4x4V.cjs} +2 -2
- package/dist/{Card-CRhkz5Jp.cjs.map → Card-_fDH4x4V.cjs.map} +1 -1
- package/dist/assets/index.css +1 -1
- package/dist/{index-Daw6ObfN.js → index-DSQieSNh.js} +892 -799
- package/dist/index-DSQieSNh.js.map +1 -0
- package/dist/index-quV01XX7.cjs +2 -0
- package/dist/index-quV01XX7.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +2 -2
- package/src/components/LinkAttachment/LinkAttachment.stories.tsx +17 -9
- package/src/components/LinkAttachment/LinkAttachment.test.tsx +104 -56
- package/src/components/LinkAttachment/components/Composer/Card.tsx +1 -1
- package/src/components/LinkAttachment/components/Received/Card.tsx +1 -1
- package/src/components/LinkAttachment/components/Sent/Card.tsx +1 -1
- package/src/components/LinkAttachment/components/_shared/CardBody.tsx +12 -11
- package/src/components/LinkAttachment/components/_shared/CardShell.tsx +17 -1
- package/src/components/LinkAttachment/components/_shared/CardThumbnail.tsx +10 -1
- package/src/components/LinkAttachment/components/_shared/accentSurface.test.ts +151 -0
- package/src/components/LinkAttachment/components/_shared/accentSurface.ts +211 -0
- package/src/components/LinkAttachment/components/_shared/chinContrast.test.ts +64 -124
- package/src/components/LinkAttachment/components/_shared/chinContrast.ts +68 -71
- package/src/components/LinkAttachment/components/_shared/useChinPalette.ts +35 -31
- package/src/components/LinkAttachment/types.ts +2 -2
- package/src/styles.css +8 -41
- package/dist/index-BwfUbPpY.cjs +0 -2
- package/dist/index-BwfUbPpY.cjs.map +0 -1
- package/dist/index-Daw6ObfN.js.map +0 -1
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { accentSurface } from './accentSurface'
|
|
4
|
+
import {
|
|
5
|
+
contrastRatio,
|
|
6
|
+
inkFor,
|
|
7
|
+
INK_ON_DARK,
|
|
8
|
+
INK_ON_LIGHT,
|
|
9
|
+
WCAG_AA_NORMAL_TEXT,
|
|
10
|
+
} from './chinContrast'
|
|
11
|
+
|
|
12
|
+
/** OKLab a/b of a hex — hue angle and chroma both come off this pair. */
|
|
13
|
+
function oklabAb(hex: string): [number, number] {
|
|
14
|
+
const [red, green, blue] = [1, 3, 5].map((index) => {
|
|
15
|
+
const channel = Number.parseInt(hex.slice(index, index + 2), 16) / 255
|
|
16
|
+
return channel <= 0.04045
|
|
17
|
+
? channel / 12.92
|
|
18
|
+
: ((channel + 0.055) / 1.055) ** 2.4
|
|
19
|
+
})
|
|
20
|
+
const long = Math.cbrt(
|
|
21
|
+
0.4122214708 * red + 0.5363325363 * green + 0.0514459929 * blue
|
|
22
|
+
)
|
|
23
|
+
const medium = Math.cbrt(
|
|
24
|
+
0.2119034982 * red + 0.6806995451 * green + 0.1073969566 * blue
|
|
25
|
+
)
|
|
26
|
+
const short = Math.cbrt(
|
|
27
|
+
0.0883024619 * red + 0.2817188376 * green + 0.6299787005 * blue
|
|
28
|
+
)
|
|
29
|
+
return [
|
|
30
|
+
1.9779984951 * long - 2.428592205 * medium + 0.4505937099 * short,
|
|
31
|
+
0.0259040371 * long + 0.7827717662 * medium - 0.808675766 * short,
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function hueOf(hex: string): number {
|
|
36
|
+
const [a, b] = oklabAb(hex)
|
|
37
|
+
return ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function chromaOf(hex: string): number {
|
|
41
|
+
return Math.hypot(...oklabAb(hex))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Hues far enough apart to be visibly different colours. */
|
|
45
|
+
const SAMPLES = [
|
|
46
|
+
'#7008E7', // violet — chroma well outside sRGB at both stops
|
|
47
|
+
'#2FCB5F', // green
|
|
48
|
+
'#FFF099', // pale yellow
|
|
49
|
+
'#00B6FF', // cyan
|
|
50
|
+
'#738883', // the Figma mock's desaturated green
|
|
51
|
+
'#FCF5E3', // the near-white from the MES-1388 report
|
|
52
|
+
'#1e2330', // already as dark as the sender fallback
|
|
53
|
+
'#767676', // fully neutral, zero chroma
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
describe('accentSurface', () => {
|
|
57
|
+
it('always produces a surface its own ink can clear AA on', () => {
|
|
58
|
+
// The whole point of deriving rather than admitting: no extracted colour is
|
|
59
|
+
// ever rejected for contrast, and none is ever illegible.
|
|
60
|
+
for (const sample of SAMPLES) {
|
|
61
|
+
for (const [variant, ink] of [
|
|
62
|
+
['dark', INK_ON_DARK],
|
|
63
|
+
['light', INK_ON_LIGHT],
|
|
64
|
+
] as const) {
|
|
65
|
+
const surface = accentSurface(sample, variant)
|
|
66
|
+
expect(surface, `${sample} on ${variant}`).toBeDefined()
|
|
67
|
+
expect(
|
|
68
|
+
contrastRatio(surface as string, ink),
|
|
69
|
+
`${sample} on ${variant}`
|
|
70
|
+
).toBeGreaterThanOrEqual(WCAG_AA_NORMAL_TEXT)
|
|
71
|
+
// And the ink the card will actually pick agrees with that side.
|
|
72
|
+
expect(inkFor(surface as string)).toBe(ink)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('keeps the image hue so different links stay distinguishable', () => {
|
|
78
|
+
for (const sample of SAMPLES) {
|
|
79
|
+
// Hue is only meaningful above a little chroma: `#FCF5E3` and `#767676`
|
|
80
|
+
// are near-neutral, where the angle is numerically unstable and invisible.
|
|
81
|
+
if (chromaOf(sample) < 0.03) continue
|
|
82
|
+
const expected = hueOf(sample)
|
|
83
|
+
for (const variant of ['dark', 'light'] as const) {
|
|
84
|
+
const surface = accentSurface(sample, variant) as string
|
|
85
|
+
const drift = Math.abs(hueOf(surface) - expected)
|
|
86
|
+
expect(
|
|
87
|
+
Math.min(drift, 360 - drift),
|
|
88
|
+
`${sample} ${variant}`
|
|
89
|
+
).toBeLessThan(2)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('separates the two sides, so each is legible under its own ink', () => {
|
|
95
|
+
// Sent lands dark, received pale — the same link, the same hue, opposite
|
|
96
|
+
// ends of the lightness axis because the inks are opposites.
|
|
97
|
+
for (const sample of SAMPLES) {
|
|
98
|
+
const sent = accentSurface(sample, 'dark') as string
|
|
99
|
+
const received = accentSurface(sample, 'light') as string
|
|
100
|
+
expect(contrastRatio(sent, INK_ON_DARK)).toBeGreaterThan(
|
|
101
|
+
contrastRatio(sent, INK_ON_LIGHT)
|
|
102
|
+
)
|
|
103
|
+
expect(contrastRatio(received, INK_ON_LIGHT)).toBeGreaterThan(
|
|
104
|
+
contrastRatio(received, INK_ON_DARK)
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('holds AA at every hue, at chroma beyond what sRGB can show', () => {
|
|
110
|
+
// Sweeps the failure mode that a fixed lightness stop could plausibly have:
|
|
111
|
+
// a hue whose maximum chroma pushes the derived colour out of gamut.
|
|
112
|
+
for (let hue = 0; hue < 360; hue += 15) {
|
|
113
|
+
const vivid = `#${[0, 120, 240]
|
|
114
|
+
.map((offset) =>
|
|
115
|
+
Math.round(127 + 127 * Math.cos(((hue + offset) * Math.PI) / 180))
|
|
116
|
+
.toString(16)
|
|
117
|
+
.padStart(2, '0')
|
|
118
|
+
)
|
|
119
|
+
.join('')}`
|
|
120
|
+
for (const [variant, ink] of [
|
|
121
|
+
['dark', INK_ON_DARK],
|
|
122
|
+
['light', INK_ON_LIGHT],
|
|
123
|
+
] as const) {
|
|
124
|
+
const surface = accentSurface(vivid, variant)
|
|
125
|
+
expect(
|
|
126
|
+
contrastRatio(surface as string, ink),
|
|
127
|
+
`hue ${hue} on ${variant}`
|
|
128
|
+
).toBeGreaterThanOrEqual(WCAG_AA_NORMAL_TEXT)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('accepts the shapes the extractor and CSS disagree about', () => {
|
|
134
|
+
// `accent_color` arrives from the ugc-kit query as a bare hex often enough
|
|
135
|
+
// that a missing `#` must not silently drop the colour.
|
|
136
|
+
expect(accentSurface('7008E7', 'dark')).toBe(
|
|
137
|
+
accentSurface('#7008E7', 'dark')
|
|
138
|
+
)
|
|
139
|
+
expect(accentSurface(' #7008E7 ', 'dark')).toBe(
|
|
140
|
+
accentSurface('#7008E7', 'dark')
|
|
141
|
+
)
|
|
142
|
+
expect(accentSurface('#70E', 'dark')).toBeDefined()
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('has no surface when there was no colour to derive from', () => {
|
|
146
|
+
expect(accentSurface(undefined, 'dark')).toBeUndefined()
|
|
147
|
+
expect(accentSurface('', 'dark')).toBeUndefined()
|
|
148
|
+
expect(accentSurface('rebeccapurple', 'dark')).toBeUndefined()
|
|
149
|
+
expect(accentSurface('#ff', 'light')).toBeUndefined()
|
|
150
|
+
})
|
|
151
|
+
})
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns the colour extracted from an attachment image into a card surface that
|
|
3
|
+
* the card's own text can survive.
|
|
4
|
+
*
|
|
5
|
+
* `ugc-kit`'s `ImageAnalysis` (`getDominantColorFromImageUrl`) answers only
|
|
6
|
+
* "what colour is this image?" — it has no notion of contrast or readability.
|
|
7
|
+
* Painting that colour straight onto the card is what broke: the extracted
|
|
8
|
+
* colour is whatever the photo happened to be, so roughly half of them left the
|
|
9
|
+
* title illegible against the side's ink, and refusing those threw away the
|
|
10
|
+
* colour the feature exists to add (MES-1349, MES-1388).
|
|
11
|
+
*
|
|
12
|
+
* So the hue is kept and the *lightness* is replaced with a fixed stop chosen
|
|
13
|
+
* per side — dark behind the sender's white ink, pale behind the receiver's
|
|
14
|
+
* near-black. This is the approach already used in `linktr.ee-profiles`
|
|
15
|
+
* (`ui/src/utils/rgbToTheme.ts`), whose `getPaletteFromColor` generates a
|
|
16
|
+
* 16-stop OKLCH ramp from one colour and has callers pick a background stop and
|
|
17
|
+
* a text stop from it. We need exactly one stop per side rather than a whole
|
|
18
|
+
* theme, so the ramp is collapsed to a single lightness target.
|
|
19
|
+
*
|
|
20
|
+
* OKLCH rather than HSL because its lightness axis is perceptually uniform: one
|
|
21
|
+
* L value gives the same *apparent* lightness — and therefore the same contrast
|
|
22
|
+
* — across every hue. In HSL, `50%` lightness yellow and `50%` blue differ by
|
|
23
|
+
* several stops of contrast, so no single target would hold.
|
|
24
|
+
*
|
|
25
|
+
* The conversion maths is ported from that same profiles module rather than
|
|
26
|
+
* invented here; the toolkit has no colour dependency and adding one for two
|
|
27
|
+
* matrix multiplies is not worth the install.
|
|
28
|
+
*/
|
|
29
|
+
import type { LinkAttachmentVariant } from './CardShell'
|
|
30
|
+
import {
|
|
31
|
+
contrastRatio,
|
|
32
|
+
INK_ON_DARK,
|
|
33
|
+
INK_ON_LIGHT,
|
|
34
|
+
WCAG_AA_NORMAL_TEXT,
|
|
35
|
+
} from './chinContrast'
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Perceived-lightness stop each side paints at, in OKLCH L (0..1) — profiles'
|
|
39
|
+
* `color12` / `color2` equivalent. Both sit far enough from their ink to clear
|
|
40
|
+
* AA at any hue and chroma: swept across 72 hues at maximum chroma the worst
|
|
41
|
+
* case is 13.17:1 sent and 14.72:1 received, against a 4.5:1 floor.
|
|
42
|
+
*/
|
|
43
|
+
const SURFACE_LIGHTNESS: Record<LinkAttachmentVariant, number> = {
|
|
44
|
+
dark: 0.3,
|
|
45
|
+
light: 0.9,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Ink each side draws, so the derived surface can be checked against it. */
|
|
49
|
+
const INK_BY_VARIANT: Record<LinkAttachmentVariant, string> = {
|
|
50
|
+
dark: INK_ON_DARK,
|
|
51
|
+
light: INK_ON_LIGHT,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const SHORT_HEX_LENGTH = 3
|
|
55
|
+
const HEX_PATTERN = /^#?(?:[0-9a-f]{3}|[0-9a-f]{6})$/i
|
|
56
|
+
|
|
57
|
+
/** Channels are allowed a half-step outside 0..255 before rounding clips them. */
|
|
58
|
+
const GAMUT_EPSILON = 0.5
|
|
59
|
+
/** Bisection steps for the chroma search — 25 resolves well past 8-bit output. */
|
|
60
|
+
const GAMUT_STEPS = 25
|
|
61
|
+
|
|
62
|
+
type Rgb = [number, number, number]
|
|
63
|
+
|
|
64
|
+
function parseHex(hex: string): Rgb | undefined {
|
|
65
|
+
if (typeof hex !== 'string' || !HEX_PATTERN.test(hex.trim())) return undefined
|
|
66
|
+
|
|
67
|
+
const digits = hex.trim().replace(/^#/, '')
|
|
68
|
+
const expanded =
|
|
69
|
+
digits.length === SHORT_HEX_LENGTH
|
|
70
|
+
? digits.replace(/./g, (digit) => digit + digit)
|
|
71
|
+
: digits
|
|
72
|
+
|
|
73
|
+
return [
|
|
74
|
+
Number.parseInt(expanded.slice(0, 2), 16),
|
|
75
|
+
Number.parseInt(expanded.slice(2, 4), 16),
|
|
76
|
+
Number.parseInt(expanded.slice(4, 6), 16),
|
|
77
|
+
]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function toHex(rgb: Rgb): string {
|
|
81
|
+
return `#${rgb
|
|
82
|
+
.map((channel) =>
|
|
83
|
+
Math.round(Math.min(255, Math.max(0, channel)))
|
|
84
|
+
.toString(16)
|
|
85
|
+
.padStart(2, '0')
|
|
86
|
+
)
|
|
87
|
+
.join('')}`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** sRGB transfer function and its inverse (gamma <-> linear light). */
|
|
91
|
+
function gammaToLinear(channel: number): number {
|
|
92
|
+
return channel <= 0.04045
|
|
93
|
+
? channel / 12.92
|
|
94
|
+
: ((channel + 0.055) / 1.055) ** 2.4
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function linearToGamma(channel: number): number {
|
|
98
|
+
return channel <= 0.0031308
|
|
99
|
+
? 12.92 * channel
|
|
100
|
+
: 1.055 * channel ** (1 / 2.4) - 0.055
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** sRGB -> OKLCH, as `[lightness, chroma, hue degrees]`. */
|
|
104
|
+
function rgbToOklch(rgb: Rgb): [number, number, number] {
|
|
105
|
+
const red = gammaToLinear(rgb[0] / 255)
|
|
106
|
+
const green = gammaToLinear(rgb[1] / 255)
|
|
107
|
+
const blue = gammaToLinear(rgb[2] / 255)
|
|
108
|
+
|
|
109
|
+
const long = Math.cbrt(
|
|
110
|
+
0.4122214708 * red + 0.5363325363 * green + 0.0514459929 * blue
|
|
111
|
+
)
|
|
112
|
+
const medium = Math.cbrt(
|
|
113
|
+
0.2119034982 * red + 0.6806995451 * green + 0.1073969566 * blue
|
|
114
|
+
)
|
|
115
|
+
const short = Math.cbrt(
|
|
116
|
+
0.0883024619 * red + 0.2817188376 * green + 0.6299787005 * blue
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
const lightness =
|
|
120
|
+
0.2104542553 * long + 0.793617785 * medium - 0.0040720468 * short
|
|
121
|
+
const a = 1.9779984951 * long - 2.428592205 * medium + 0.4505937099 * short
|
|
122
|
+
const b = 0.0259040371 * long + 0.7827717662 * medium - 0.808675766 * short
|
|
123
|
+
|
|
124
|
+
return [
|
|
125
|
+
lightness,
|
|
126
|
+
Math.hypot(a, b),
|
|
127
|
+
((Math.atan2(b, a) * 180) / Math.PI + 360) % 360,
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** OKLCH -> sRGB. Channels may land outside 0..255 when out of gamut. */
|
|
132
|
+
function oklchToRgb([lightness, chroma, hue]: [number, number, number]): Rgb {
|
|
133
|
+
const radians = (hue * Math.PI) / 180
|
|
134
|
+
const a = chroma * Math.cos(radians)
|
|
135
|
+
const b = chroma * Math.sin(radians)
|
|
136
|
+
|
|
137
|
+
const long = (lightness + 0.3963377774 * a + 0.2158037573 * b) ** 3
|
|
138
|
+
const medium = (lightness - 0.1055613458 * a - 0.0638541728 * b) ** 3
|
|
139
|
+
const short = (lightness - 0.0894841775 * a - 1.291485548 * b) ** 3
|
|
140
|
+
|
|
141
|
+
return [
|
|
142
|
+
255 *
|
|
143
|
+
linearToGamma(
|
|
144
|
+
4.0767416621 * long - 3.3077115913 * medium + 0.2309699292 * short
|
|
145
|
+
),
|
|
146
|
+
255 *
|
|
147
|
+
linearToGamma(
|
|
148
|
+
-1.2684380046 * long + 2.6097574011 * medium - 0.3413193965 * short
|
|
149
|
+
),
|
|
150
|
+
255 *
|
|
151
|
+
linearToGamma(
|
|
152
|
+
-0.0041960863 * long - 0.7034186147 * medium + 1.707614701 * short
|
|
153
|
+
),
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function isInGamut(rgb: Rgb): boolean {
|
|
158
|
+
return rgb.every(
|
|
159
|
+
(channel) => channel >= -GAMUT_EPSILON && channel <= 255 + GAMUT_EPSILON
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The card surface for an extracted colour: the image's hue at this side's
|
|
165
|
+
* lightness stop, or `undefined` when there was no colour to work from.
|
|
166
|
+
*
|
|
167
|
+
* Chroma is reduced — never the hue — when the requested lightness puts the
|
|
168
|
+
* colour outside sRGB, which is what stops a saturated yellow or green from
|
|
169
|
+
* clipping into a different colour on the way to 8-bit output. Vivid hues
|
|
170
|
+
* therefore desaturate rather than shift.
|
|
171
|
+
*
|
|
172
|
+
* The AA check at the end is a guard, not a branch anyone expects to take: the
|
|
173
|
+
* lightness stops were chosen to clear it everywhere. It exists so that a future
|
|
174
|
+
* change to `SURFACE_LIGHTNESS` or to the ink cannot quietly reintroduce an
|
|
175
|
+
* illegible card — it degrades to the plain bubble fill instead.
|
|
176
|
+
*/
|
|
177
|
+
export function accentSurface(
|
|
178
|
+
accentColor: string | undefined,
|
|
179
|
+
variant: LinkAttachmentVariant
|
|
180
|
+
): string | undefined {
|
|
181
|
+
if (accentColor == null) return undefined
|
|
182
|
+
|
|
183
|
+
const rgb = parseHex(accentColor)
|
|
184
|
+
if (!rgb) return undefined
|
|
185
|
+
|
|
186
|
+
const [, extractedChroma, hue] = rgbToOklch(rgb)
|
|
187
|
+
const lightness = SURFACE_LIGHTNESS[variant]
|
|
188
|
+
|
|
189
|
+
let chroma = 0
|
|
190
|
+
if (isInGamut(oklchToRgb([lightness, extractedChroma, hue]))) {
|
|
191
|
+
chroma = extractedChroma
|
|
192
|
+
} else {
|
|
193
|
+
// Largest chroma that still fits sRGB at this lightness and hue.
|
|
194
|
+
let low = 0
|
|
195
|
+
let high = extractedChroma
|
|
196
|
+
for (let step = 0; step < GAMUT_STEPS; step++) {
|
|
197
|
+
const middle = (low + high) / 2
|
|
198
|
+
if (isInGamut(oklchToRgb([lightness, middle, hue]))) {
|
|
199
|
+
chroma = middle
|
|
200
|
+
low = middle
|
|
201
|
+
} else {
|
|
202
|
+
high = middle
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const surface = toHex(oklchToRgb([lightness, chroma, hue]))
|
|
208
|
+
return contrastRatio(surface, INK_BY_VARIANT[variant]) >= WCAG_AA_NORMAL_TEXT
|
|
209
|
+
? surface
|
|
210
|
+
: undefined
|
|
211
|
+
}
|
|
@@ -1,162 +1,102 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
|
|
3
|
+
import { AUDIO_BG, AUDIO_BG_CLASS } from './CardThumbnail'
|
|
3
4
|
import {
|
|
4
|
-
|
|
5
|
-
CHIN_TEXT_DARK,
|
|
6
|
-
CHIN_TEXT_LIGHT,
|
|
7
|
-
WCAG_AA_NORMAL_TEXT,
|
|
8
|
-
chinTextColor,
|
|
5
|
+
BUBBLE_BG_BY_VARIANT,
|
|
9
6
|
contrastRatio,
|
|
10
|
-
|
|
7
|
+
inkFor,
|
|
8
|
+
INK_ON_DARK,
|
|
9
|
+
INK_ON_LIGHT,
|
|
11
10
|
relativeLuminance,
|
|
11
|
+
WCAG_AA_NORMAL_TEXT,
|
|
12
12
|
} from './chinContrast'
|
|
13
13
|
|
|
14
14
|
describe('relativeLuminance', () => {
|
|
15
|
-
it('anchors the sRGB endpoints', () => {
|
|
16
|
-
expect(relativeLuminance('#
|
|
17
|
-
expect(relativeLuminance('#000000')).toBeCloseTo(0,
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('applies gamma expansion rather than a linear average', () => {
|
|
21
|
-
// Mid-grey is ~21.6% luminance, not 50% — a Rec. 601 style weighted
|
|
22
|
-
// average (the trap the app-local helpers fall into) would say ~0.5.
|
|
23
|
-
expect(relativeLuminance('#808080')).toBeCloseTo(0.2159, 4)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('weights green above red above blue', () => {
|
|
27
|
-
expect(relativeLuminance('#00FF00')).toBeCloseTo(0.7152, 4)
|
|
28
|
-
expect(relativeLuminance('#FF0000')).toBeCloseTo(0.2126, 4)
|
|
29
|
-
expect(relativeLuminance('#0000FF')).toBeCloseTo(0.0722, 4)
|
|
15
|
+
it('anchors on the sRGB endpoints', () => {
|
|
16
|
+
expect(relativeLuminance('#ffffff')).toBeCloseTo(1, 5)
|
|
17
|
+
expect(relativeLuminance('#000000')).toBeCloseTo(0, 5)
|
|
30
18
|
})
|
|
31
19
|
|
|
32
|
-
it('accepts
|
|
33
|
-
expect(relativeLuminance('#fff')).
|
|
34
|
-
expect(relativeLuminance('
|
|
20
|
+
it('accepts short hex and a missing leading hash', () => {
|
|
21
|
+
expect(relativeLuminance('#fff')).toBeCloseTo(1, 5)
|
|
22
|
+
expect(relativeLuminance('ffffff')).toBeCloseTo(1, 5)
|
|
23
|
+
expect(relativeLuminance(' #fff ')).toBeCloseTo(1, 5)
|
|
35
24
|
})
|
|
36
25
|
|
|
37
|
-
it('
|
|
38
|
-
for (const
|
|
39
|
-
expect(relativeLuminance(
|
|
26
|
+
it('reports NaN for anything unparseable', () => {
|
|
27
|
+
for (const input of ['', '#ff', '#gggggg', 'rebeccapurple', '#ffffff00']) {
|
|
28
|
+
expect(relativeLuminance(input)).toBeNaN()
|
|
40
29
|
}
|
|
41
30
|
})
|
|
42
31
|
})
|
|
43
32
|
|
|
44
33
|
describe('contrastRatio', () => {
|
|
45
|
-
it('
|
|
46
|
-
expect(contrastRatio('#000000', '#
|
|
47
|
-
expect(contrastRatio('#738883', '#738883')).toBeCloseTo(1, 10)
|
|
34
|
+
it('matches the WCAG maximum for black on white', () => {
|
|
35
|
+
expect(contrastRatio('#000000', '#ffffff')).toBeCloseTo(21, 2)
|
|
48
36
|
})
|
|
49
37
|
|
|
50
|
-
it('is symmetric
|
|
51
|
-
expect(contrastRatio('#
|
|
52
|
-
contrastRatio(
|
|
38
|
+
it('is symmetric and self-referential', () => {
|
|
39
|
+
expect(contrastRatio('#1e2330', '#ffffff')).toBeCloseTo(
|
|
40
|
+
contrastRatio('#ffffff', '#1e2330'),
|
|
41
|
+
5
|
|
53
42
|
)
|
|
43
|
+
expect(contrastRatio('#1e2330', '#1e2330')).toBeCloseTo(1, 5)
|
|
54
44
|
})
|
|
55
45
|
|
|
56
|
-
it('
|
|
57
|
-
expect(contrastRatio('nope', '#
|
|
58
|
-
expect(contrastRatio('#
|
|
46
|
+
it('propagates NaN when either side is unparseable', () => {
|
|
47
|
+
expect(contrastRatio('nope', '#ffffff')).toBeNaN()
|
|
48
|
+
expect(contrastRatio('#ffffff', 'nope')).toBeNaN()
|
|
59
49
|
})
|
|
60
50
|
})
|
|
61
51
|
|
|
62
|
-
describe('
|
|
63
|
-
it('
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
it('picks light text on dark accents', () => {
|
|
73
|
-
expect(chinTextColor(CHIN_FALLBACK_BG)).toBe(CHIN_TEXT_LIGHT)
|
|
74
|
-
expect(chinTextColor('#7008E7')).toBe(CHIN_TEXT_LIGHT)
|
|
75
|
-
expect(chinTextColor('#000000')).toBe(CHIN_TEXT_LIGHT)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('falls back to light text for missing or malformed input', () => {
|
|
79
|
-
expect(chinTextColor()).toBe(CHIN_TEXT_LIGHT)
|
|
80
|
-
expect(chinTextColor(undefined)).toBe(CHIN_TEXT_LIGHT)
|
|
81
|
-
expect(chinTextColor('')).toBe(CHIN_TEXT_LIGHT)
|
|
82
|
-
expect(chinTextColor('#12345')).toBe(CHIN_TEXT_LIGHT)
|
|
83
|
-
expect(chinTextColor('teal')).toBe(CHIN_TEXT_LIGHT)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('clears AA for every accent, including the greys either side of the crossover', () => {
|
|
87
|
-
// The invariant, not the individual verdicts: whichever colour the rule
|
|
88
|
-
// returns must clear 4.5:1. `#767676` is the tightest point on the grey
|
|
89
|
-
// axis (4.54 vs 4.52) and #737373 / #797979 straddle it.
|
|
90
|
-
const accents = [
|
|
91
|
-
'#738883',
|
|
92
|
-
'#AA9F93',
|
|
93
|
-
CHIN_FALLBACK_BG,
|
|
94
|
-
'#767676',
|
|
95
|
-
'#737373',
|
|
96
|
-
'#797979',
|
|
97
|
-
'#FFFFFF',
|
|
98
|
-
'#000000',
|
|
99
|
-
'#FF0000',
|
|
100
|
-
'#00FF00',
|
|
101
|
-
'#0000FF',
|
|
102
|
-
'#FFF099',
|
|
103
|
-
'#7008E7',
|
|
104
|
-
'#F2F1EF',
|
|
105
|
-
'#121110',
|
|
106
|
-
]
|
|
107
|
-
|
|
108
|
-
for (const accent of accents) {
|
|
109
|
-
expect(
|
|
110
|
-
contrastRatio(accent, chinTextColor(accent))
|
|
111
|
-
).toBeGreaterThanOrEqual(WCAG_AA_NORMAL_TEXT)
|
|
52
|
+
describe('the fallback fills stay legible', () => {
|
|
53
|
+
it('clears AA on both sides, so a card with no accent is always readable', () => {
|
|
54
|
+
for (const variant of ['dark', 'light'] as const) {
|
|
55
|
+
const fill = BUBBLE_BG_BY_VARIANT[variant]
|
|
56
|
+
const ink = inkFor(fill)
|
|
57
|
+
expect(ink).toBeDefined()
|
|
58
|
+
expect(contrastRatio(fill, ink as string)).toBeGreaterThanOrEqual(
|
|
59
|
+
WCAG_AA_NORMAL_TEXT
|
|
60
|
+
)
|
|
112
61
|
}
|
|
113
62
|
})
|
|
114
63
|
|
|
115
|
-
it('
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const accent = `#${hex(r)}${hex(g)}${hex(b)}`
|
|
123
|
-
expect(
|
|
124
|
-
contrastRatio(accent, chinTextColor(accent))
|
|
125
|
-
).toBeGreaterThanOrEqual(WCAG_AA_NORMAL_TEXT)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
64
|
+
it('uses the plain message bubble fills', () => {
|
|
65
|
+
// Keep in sync with `--str-chat__own-message-bubble-background-color` and
|
|
66
|
+
// `--str-chat__message-bubble-background-color` in styles.css.
|
|
67
|
+
expect(BUBBLE_BG_BY_VARIANT).toEqual({
|
|
68
|
+
dark: '#1e2330',
|
|
69
|
+
light: '#f1f0ee',
|
|
70
|
+
})
|
|
129
71
|
})
|
|
72
|
+
})
|
|
130
73
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
)
|
|
74
|
+
describe('inkFor', () => {
|
|
75
|
+
it('inks a dark surface light and a light surface dark', () => {
|
|
76
|
+
expect(inkFor('#1e2330')).toBe(INK_ON_DARK)
|
|
77
|
+
expect(inkFor('#f1f0ee')).toBe(INK_ON_LIGHT)
|
|
136
78
|
})
|
|
137
|
-
})
|
|
138
79
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
expect(
|
|
80
|
+
it('inks the audio strip dark, which is the MES-1388 regression', () => {
|
|
81
|
+
// `AUDIO_BG_CLASS` is imposed on the shell of any variant, so a sent audio
|
|
82
|
+
// card drew the dark variant's white ink on near-white at ~1.13:1.
|
|
83
|
+
expect(contrastRatio(AUDIO_BG, INK_ON_DARK)).toBeLessThan(
|
|
84
|
+
WCAG_AA_NORMAL_TEXT
|
|
85
|
+
)
|
|
86
|
+
const ink = inkFor(AUDIO_BG)
|
|
87
|
+
expect(ink).toBe(INK_ON_LIGHT)
|
|
88
|
+
expect(contrastRatio(AUDIO_BG, ink as string)).toBeGreaterThanOrEqual(
|
|
89
|
+
WCAG_AA_NORMAL_TEXT
|
|
90
|
+
)
|
|
143
91
|
})
|
|
144
92
|
|
|
145
|
-
it('
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
|
|
149
|
-
// chin is sitting on. Normalizing on the way out keeps them in step.
|
|
150
|
-
expect(paintableAccent('738883')).toBe('#738883')
|
|
151
|
-
expect(paintableAccent('fff')).toBe('#fff')
|
|
152
|
-
expect(paintableAccent(' 738883 ')).toBe('#738883')
|
|
153
|
-
expect(paintableAccent(' #738883 ')).toBe('#738883')
|
|
93
|
+
it('keeps the audio hex and its tailwind class in step', () => {
|
|
94
|
+
// Tailwind only emits utilities it can find as complete literals, so the
|
|
95
|
+
// class cannot be built from the constant — hence the pair.
|
|
96
|
+
expect(AUDIO_BG_CLASS).toBe(`bg-[${AUDIO_BG}]`)
|
|
154
97
|
})
|
|
155
98
|
|
|
156
|
-
it('
|
|
157
|
-
expect(
|
|
158
|
-
expect(paintableAccent('')).toBeUndefined()
|
|
159
|
-
expect(paintableAccent('#12345')).toBeUndefined()
|
|
160
|
-
expect(paintableAccent('rgb(1,2,3)')).toBeUndefined()
|
|
99
|
+
it('has no ink for an unparseable surface', () => {
|
|
100
|
+
expect(inkFor('rebeccapurple')).toBeUndefined()
|
|
161
101
|
})
|
|
162
102
|
})
|