@cognitap/radiant 0.0.3 → 0.0.5
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/CHANGELOG.md +69 -0
- package/README.md +28 -15
- package/dist/library/Card.d.ts.map +1 -1
- package/dist/library/Card.js +8 -6
- package/dist/library/Card.js.map +1 -1
- package/dist/library/Checkbox.js +2 -2
- package/dist/library/Checkbox.js.map +1 -1
- package/dist/library/Collapsible.d.ts.map +1 -1
- package/dist/library/Collapsible.js +5 -4
- package/dist/library/Collapsible.js.map +1 -1
- package/dist/library/Dialog.d.ts.map +1 -1
- package/dist/library/Dialog.js +8 -6
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/Field.js +4 -4
- package/dist/library/Field.js.map +1 -1
- package/dist/library/Input.d.ts.map +1 -1
- package/dist/library/Input.js +4 -3
- package/dist/library/Input.js.map +1 -1
- package/dist/library/Link.d.ts.map +1 -1
- package/dist/library/Link.js +0 -1
- package/dist/library/Link.js.map +1 -1
- package/dist/library/RadioGroup.js +2 -2
- package/dist/library/RadioGroup.js.map +1 -1
- package/dist/library/ThemeScope.js +1 -1
- package/dist/library/ThemeScope.js.map +1 -1
- package/dist/library/actionStyles.js +2 -2
- package/dist/library/actionStyles.js.map +1 -1
- package/dist/library/cn.d.ts +2 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +22 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/color/colorFamilies.d.ts +3 -0
- package/dist/library/color/colorFamilies.d.ts.map +1 -0
- package/dist/library/color/colorFamilies.js +20 -0
- package/dist/library/color/colorFamilies.js.map +1 -0
- package/dist/library/color/generateColorWall.d.ts +54 -0
- package/dist/library/color/generateColorWall.d.ts.map +1 -0
- package/dist/library/color/generateColorWall.js +231 -0
- package/dist/library/color/generateColorWall.js.map +1 -0
- package/dist/library/color/index.d.ts +5 -0
- package/dist/library/color/index.d.ts.map +1 -0
- package/dist/library/color/index.js +3 -0
- package/dist/library/color/index.js.map +1 -0
- package/dist/library/color/oklch.d.ts +17 -0
- package/dist/library/color/oklch.d.ts.map +1 -0
- package/dist/library/color/oklch.js +79 -0
- package/dist/library/color/oklch.js.map +1 -0
- package/dist/library/color/recommendColorPalette.d.ts +65 -0
- package/dist/library/color/recommendColorPalette.d.ts.map +1 -0
- package/dist/library/color/recommendColorPalette.js +263 -0
- package/dist/library/color/recommendColorPalette.js.map +1 -0
- package/dist/library/index.d.ts +10 -2
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +5 -1
- package/dist/library/index.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/defineTheme.d.ts.map +1 -1
- package/dist/library/themes/defineTheme.js +5 -1
- package/dist/library/themes/defineTheme.js.map +1 -1
- package/dist/library/themes/index.d.ts +4 -0
- package/dist/library/themes/index.d.ts.map +1 -1
- package/dist/library/themes/index.js +2 -0
- package/dist/library/themes/index.js.map +1 -1
- package/dist/library/themes/themeDocument.d.ts +16 -0
- package/dist/library/themes/themeDocument.d.ts.map +1 -0
- package/dist/library/themes/themeDocument.js +332 -0
- package/dist/library/themes/themeDocument.js.map +1 -0
- package/dist/library/themes/themeDocumentTypes.d.ts +80 -0
- package/dist/library/themes/themeDocumentTypes.d.ts.map +1 -0
- package/dist/library/themes/themeDocumentTypes.js +2 -0
- package/dist/library/themes/themeDocumentTypes.js.map +1 -0
- package/dist/library/themes/tokens.d.ts +7 -0
- package/dist/library/themes/tokens.d.ts.map +1 -1
- package/dist/library/themes/tokens.js +99 -0
- package/dist/library/themes/tokens.js.map +1 -1
- package/dist/library/themes/updateThemeAppearance.d.ts +29 -0
- package/dist/library/themes/updateThemeAppearance.d.ts.map +1 -0
- package/dist/library/themes/updateThemeAppearance.js +138 -0
- package/dist/library/themes/updateThemeAppearance.js.map +1 -0
- package/dist/library/typography/index.d.ts +72 -0
- package/dist/library/typography/index.d.ts.map +1 -0
- package/dist/library/typography/index.js +160 -0
- package/dist/library/typography/index.js.map +1 -0
- package/dist/library/typography/types.d.ts +78 -0
- package/dist/library/typography/types.d.ts.map +1 -0
- package/dist/library/typography/types.js +2 -0
- package/dist/library/typography/types.js.map +1 -0
- package/docs/system.md +20 -0
- package/docs/theming.md +616 -0
- package/package.json +17 -5
- package/src/Card.tsx +8 -6
- package/src/Checkbox.tsx +2 -2
- package/src/Collapsible.tsx +5 -4
- package/src/Dialog.tsx +8 -6
- package/src/Field.tsx +4 -4
- package/src/Input.tsx +4 -3
- package/src/Link.tsx +0 -1
- package/src/RadioGroup.tsx +2 -2
- package/src/ThemeScope.tsx +1 -1
- package/src/actionStyles.ts +2 -2
- package/src/cn.ts +25 -1
- package/src/color/colorFamilies.ts +19 -0
- package/src/color/generateColorWall.ts +358 -0
- package/src/color/index.ts +11 -0
- package/src/color/oklch.ts +95 -0
- package/src/color/recommendColorPalette.ts +375 -0
- package/src/index.ts +40 -0
- package/src/styles.css +1 -0
- package/src/themes/baseline.css +131 -0
- package/src/themes/defineTheme.ts +5 -1
- package/src/themes/index.ts +25 -0
- package/src/themes/themeDocument.ts +370 -0
- package/src/themes/themeDocumentTypes.ts +88 -0
- package/src/themes/tokens.ts +137 -0
- package/src/themes/updateThemeAppearance.ts +172 -0
- package/src/typography/index.ts +199 -0
- package/src/typography/types.ts +92 -0
- package/src/typography.css +336 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { colorFamilies as families } from './colorFamilies.js'
|
|
2
|
+
import type {
|
|
3
|
+
ThemeBrandSource,
|
|
4
|
+
ThemeColorSection,
|
|
5
|
+
ThemePalette
|
|
6
|
+
} from '../themes/themeDocumentTypes.js'
|
|
7
|
+
import { freezeTree } from '../themes/tokens.js'
|
|
8
|
+
import { clamp, hueDifference, measureHex, srgbMapper, wrapHue } from './oklch.js'
|
|
9
|
+
|
|
10
|
+
export type ColorCalibrationPreset = 'classic' | 'balanced' | 'expressive'
|
|
11
|
+
/** Stable IDs for independently choosing the full wall and exact-brand ramp. */
|
|
12
|
+
export const colorCalibrationPresets = freezeTree([
|
|
13
|
+
{
|
|
14
|
+
id: 'classic',
|
|
15
|
+
name: 'Classic',
|
|
16
|
+
description: 'Original hue-aware curves and linear anchor reshaping.'
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
id: 'balanced',
|
|
20
|
+
name: 'Balanced',
|
|
21
|
+
description: 'Restrained brand shades, coordinated families and smooth anchor reshaping.'
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: 'expressive',
|
|
25
|
+
name: 'Expressive',
|
|
26
|
+
description: 'Fuller color families and a more vivid, smoothly reshaped brand ramp.'
|
|
27
|
+
}
|
|
28
|
+
] as const)
|
|
29
|
+
const profiles = {
|
|
30
|
+
classic: {
|
|
31
|
+
center: 0.7,
|
|
32
|
+
slope: 0.7,
|
|
33
|
+
low: 0.6,
|
|
34
|
+
high: 0.86,
|
|
35
|
+
exponent: 1,
|
|
36
|
+
cap: 0.255,
|
|
37
|
+
envelope: 0.8,
|
|
38
|
+
tint: 0.012,
|
|
39
|
+
alignment: 0.6,
|
|
40
|
+
radius: 2.2
|
|
41
|
+
},
|
|
42
|
+
balanced: {
|
|
43
|
+
center: 0.7,
|
|
44
|
+
slope: 0.75,
|
|
45
|
+
low: 0.62,
|
|
46
|
+
high: 0.9,
|
|
47
|
+
exponent: 0.95,
|
|
48
|
+
cap: 0.26,
|
|
49
|
+
envelope: 0.4,
|
|
50
|
+
tint: 0.008,
|
|
51
|
+
alignment: 0.25,
|
|
52
|
+
radius: 2.2
|
|
53
|
+
},
|
|
54
|
+
expressive: {
|
|
55
|
+
center: 0.62,
|
|
56
|
+
slope: 0.9,
|
|
57
|
+
low: 0.6,
|
|
58
|
+
high: 0.92,
|
|
59
|
+
exponent: 1.05,
|
|
60
|
+
cap: 0.32,
|
|
61
|
+
envelope: 0.3,
|
|
62
|
+
tint: 0.016,
|
|
63
|
+
alignment: 0.5,
|
|
64
|
+
radius: 2.6
|
|
65
|
+
}
|
|
66
|
+
} as const
|
|
67
|
+
|
|
68
|
+
export interface ColorWallInput {
|
|
69
|
+
readonly version?: 1
|
|
70
|
+
/** Original opaque six-digit sRGB hex; retained verbatim at the exact anchor. */
|
|
71
|
+
readonly brand: string
|
|
72
|
+
readonly gamut?: 'srgb'
|
|
73
|
+
readonly fidelity?: 'anchored' | 'unanchored'
|
|
74
|
+
/** Independently select coordinated families and the brand ramp. Both default to balanced. */
|
|
75
|
+
readonly wallPreset?: ColorCalibrationPreset
|
|
76
|
+
readonly rampPreset?: ColorCalibrationPreset
|
|
77
|
+
/** 0–1 wall chroma. Omit to derive restraint from the source. */
|
|
78
|
+
readonly vividness?: number
|
|
79
|
+
/** 0–1 brand-ramp chroma, independent of wall vividness; never changes the exact anchor. */
|
|
80
|
+
readonly brandVividness?: number
|
|
81
|
+
readonly lightness?: 'hue-aware' | 'shared'
|
|
82
|
+
/** 0.75–1.35. */
|
|
83
|
+
readonly distribution?: number
|
|
84
|
+
/** 0–0.035 peak OKLCH chroma. */
|
|
85
|
+
readonly neutralTint?: number
|
|
86
|
+
/** 0–1 alignment of the nearest named hue family toward the brand. */
|
|
87
|
+
readonly hueAlignment?: number
|
|
88
|
+
/** Finite degrees, wrapped to [0, 360); used when source C < 0.02. */
|
|
89
|
+
readonly neutralHue?: number
|
|
90
|
+
}
|
|
91
|
+
export interface ColorWall {
|
|
92
|
+
/** Materialised, JSON-safe inputs; replay with generateColorWall(wall.input). */
|
|
93
|
+
readonly input: Required<ColorWallInput>
|
|
94
|
+
readonly neutralInput: boolean
|
|
95
|
+
/** Color authoring data; semantic values and modes remain independently owned. */
|
|
96
|
+
readonly color: {
|
|
97
|
+
readonly brand: ThemeBrandSource
|
|
98
|
+
readonly fidelity: 'anchored' | 'unanchored'
|
|
99
|
+
readonly palettes: readonly ThemePalette[]
|
|
100
|
+
readonly anchor?: ThemeColorSection['anchor']
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const stops = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950] as const
|
|
105
|
+
const lightness = [0.985, 0.965, 0.93, 0.87, 0.79, 0.7, 0.61, 0.52, 0.43, 0.34, 0.24]
|
|
106
|
+
|
|
107
|
+
function option<T>(value: unknown, fallback: T, allowed: readonly T[], key: string): T {
|
|
108
|
+
if (value === undefined) return fallback
|
|
109
|
+
if (!allowed.includes(value as T))
|
|
110
|
+
throw new TypeError(`colorWall.${key} must be one of: ${allowed.join(', ')}.`)
|
|
111
|
+
return value as T
|
|
112
|
+
}
|
|
113
|
+
function number(value: unknown, fallback: number, low: number, high: number, key: string): number {
|
|
114
|
+
if (value === undefined) return fallback
|
|
115
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < low || value > high) {
|
|
116
|
+
throw new TypeError(`colorWall.${key} must be a finite number from ${low} to ${high}.`)
|
|
117
|
+
}
|
|
118
|
+
return value === 0 ? 0 : value
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Three-point monotone Hermite mapping; endpoints also cover exact black/white. */
|
|
122
|
+
function reshapeLightness(
|
|
123
|
+
x0: number,
|
|
124
|
+
x1: number,
|
|
125
|
+
x2: number,
|
|
126
|
+
y0: number,
|
|
127
|
+
y1: number,
|
|
128
|
+
y2: number,
|
|
129
|
+
x: number,
|
|
130
|
+
endpoint: boolean
|
|
131
|
+
) {
|
|
132
|
+
if (endpoint) return y0 + ((x - x0) / (x2 - x0)) * (y2 - y0)
|
|
133
|
+
const h0 = x1 - x0,
|
|
134
|
+
h1 = x2 - x1
|
|
135
|
+
const d0 = (y1 - y0) / h0,
|
|
136
|
+
d1 = (y2 - y1) / h1
|
|
137
|
+
const m0 = clamp(((2 * h0 + h1) * d0 - h0 * d1) / (h0 + h1), 0, 3 * d0)
|
|
138
|
+
const m2 = clamp(((2 * h1 + h0) * d1 - h1 * d0) / (h0 + h1), 0, 3 * d1)
|
|
139
|
+
const w1 = 2 * h1 + h0,
|
|
140
|
+
w2 = h1 + 2 * h0
|
|
141
|
+
const m1 = d0 <= 0 || d1 <= 0 ? 0 : (w1 + w2) / (w1 / d0 + w2 / d1)
|
|
142
|
+
const upper = x > x1
|
|
143
|
+
const lo = upper ? x1 : x0,
|
|
144
|
+
hi = upper ? x2 : x1
|
|
145
|
+
const a = upper ? y1 : y0,
|
|
146
|
+
b = upper ? y2 : y1
|
|
147
|
+
const ma = upper ? m1 : m0,
|
|
148
|
+
mb = upper ? m2 : m1
|
|
149
|
+
const t = clamp((x - lo) / (hi - lo)),
|
|
150
|
+
span = hi - lo
|
|
151
|
+
return (
|
|
152
|
+
(2 * t ** 3 - 3 * t ** 2 + 1) * a +
|
|
153
|
+
(t ** 3 - 2 * t ** 2 + t) * span * ma +
|
|
154
|
+
(-2 * t ** 3 + 3 * t ** 2) * b +
|
|
155
|
+
(t ** 3 - t ** 2) * span * mb
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Deterministic, sRGB-first calibration v1. No browser, React, AI or semantic-role selection. */
|
|
160
|
+
export function generateColorWall(input: ColorWallInput): ColorWall {
|
|
161
|
+
if (!input || typeof input !== 'object' || Array.isArray(input))
|
|
162
|
+
throw new TypeError('colorWall input must be an object.')
|
|
163
|
+
const allowed = [
|
|
164
|
+
'version',
|
|
165
|
+
'brand',
|
|
166
|
+
'gamut',
|
|
167
|
+
'fidelity',
|
|
168
|
+
'wallPreset',
|
|
169
|
+
'rampPreset',
|
|
170
|
+
'vividness',
|
|
171
|
+
'brandVividness',
|
|
172
|
+
'lightness',
|
|
173
|
+
'distribution',
|
|
174
|
+
'neutralTint',
|
|
175
|
+
'hueAlignment',
|
|
176
|
+
'neutralHue'
|
|
177
|
+
]
|
|
178
|
+
for (const key of Object.keys(input)) {
|
|
179
|
+
if (!allowed.includes(key)) throw new TypeError(`Unknown colorWall input: ${key}.`)
|
|
180
|
+
}
|
|
181
|
+
if (typeof input.brand !== 'string' || !/^#[0-9a-f]{6}$/i.test(input.brand)) {
|
|
182
|
+
throw new TypeError(
|
|
183
|
+
'colorWall.brand must be an opaque six-digit sRGB hex color, such as #7C3AED. Alpha and wide-gamut sources are not supported by version 1.'
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
const source = measureHex(input.brand),
|
|
187
|
+
mapper = srgbMapper()
|
|
188
|
+
const neutralInput = source.C < 0.02
|
|
189
|
+
const wallPreset = option(
|
|
190
|
+
input.wallPreset,
|
|
191
|
+
'balanced',
|
|
192
|
+
['classic', 'balanced', 'expressive'] as const,
|
|
193
|
+
'wallPreset'
|
|
194
|
+
)
|
|
195
|
+
const rampPreset = option(
|
|
196
|
+
input.rampPreset,
|
|
197
|
+
'balanced',
|
|
198
|
+
['classic', 'balanced', 'expressive'] as const,
|
|
199
|
+
'rampPreset'
|
|
200
|
+
)
|
|
201
|
+
const wallProfile = profiles[wallPreset],
|
|
202
|
+
brandProfile = profiles[rampPreset]
|
|
203
|
+
const mood = Math.sqrt(clamp(source.C / 0.28))
|
|
204
|
+
const classicVividness = neutralInput
|
|
205
|
+
? 0.35
|
|
206
|
+
: clamp(source.C / Math.max(0.001, mapper.maxChroma(source.L, source.h)), 0.1, 0.92)
|
|
207
|
+
const settings: Required<ColorWallInput> = {
|
|
208
|
+
version: option(input.version, 1, [1], 'version'),
|
|
209
|
+
brand: input.brand,
|
|
210
|
+
gamut: option(input.gamut, 'srgb', ['srgb'], 'gamut'),
|
|
211
|
+
fidelity: option(input.fidelity, 'anchored', ['anchored', 'unanchored'], 'fidelity'),
|
|
212
|
+
wallPreset,
|
|
213
|
+
rampPreset,
|
|
214
|
+
vividness: number(
|
|
215
|
+
input.vividness,
|
|
216
|
+
wallPreset === 'classic'
|
|
217
|
+
? classicVividness
|
|
218
|
+
: wallPreset === 'balanced'
|
|
219
|
+
? 0.45 + 0.4 * mood
|
|
220
|
+
: 0.6 + 0.3 * mood,
|
|
221
|
+
0,
|
|
222
|
+
1,
|
|
223
|
+
'vividness'
|
|
224
|
+
),
|
|
225
|
+
brandVividness: number(
|
|
226
|
+
input.brandVividness,
|
|
227
|
+
rampPreset === 'classic'
|
|
228
|
+
? classicVividness
|
|
229
|
+
: rampPreset === 'balanced'
|
|
230
|
+
? 0.18 + 0.65 * mood
|
|
231
|
+
: 0.28 + 0.62 * mood,
|
|
232
|
+
0,
|
|
233
|
+
1,
|
|
234
|
+
'brandVividness'
|
|
235
|
+
),
|
|
236
|
+
lightness: option(input.lightness, 'hue-aware', ['hue-aware', 'shared'], 'lightness'),
|
|
237
|
+
distribution: number(input.distribution, 1, 0.75, 1.35, 'distribution'),
|
|
238
|
+
neutralTint: number(input.neutralTint, wallProfile.tint, 0, 0.035, 'neutralTint'),
|
|
239
|
+
hueAlignment: number(input.hueAlignment, wallProfile.alignment, 0, 1, 'hueAlignment'),
|
|
240
|
+
neutralHue: wrapHue(
|
|
241
|
+
number(input.neutralHue, 255, -Number.MAX_VALUE, Number.MAX_VALUE, 'neutralHue')
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
const hue = neutralInput ? settings.neutralHue : source.h
|
|
245
|
+
const nearestFamily = families.reduce((a, b) =>
|
|
246
|
+
Math.abs(hueDifference(hue, a[1])) < Math.abs(hueDifference(hue, b[1])) ? a : b
|
|
247
|
+
)
|
|
248
|
+
const rotation = hueDifference(hue, nearestFamily[1]) * settings.hueAlignment
|
|
249
|
+
function tone(
|
|
250
|
+
base: number,
|
|
251
|
+
h: number,
|
|
252
|
+
profile: (typeof profiles)[ColorCalibrationPreset]
|
|
253
|
+
): number {
|
|
254
|
+
const middle =
|
|
255
|
+
settings.lightness === 'shared'
|
|
256
|
+
? 0.7
|
|
257
|
+
: clamp(
|
|
258
|
+
profile.center + profile.slope * (mapper.cuspLightness(h) - profile.center),
|
|
259
|
+
profile.low,
|
|
260
|
+
profile.high
|
|
261
|
+
)
|
|
262
|
+
return base >= 0.7
|
|
263
|
+
? middle +
|
|
264
|
+
(0.985 - middle) *
|
|
265
|
+
((base - 0.7) / (0.985 - 0.7)) ** (settings.distribution * profile.exponent)
|
|
266
|
+
: middle -
|
|
267
|
+
(middle - 0.24) *
|
|
268
|
+
((0.7 - base) / (0.7 - 0.24)) ** (settings.distribution * profile.exponent)
|
|
269
|
+
}
|
|
270
|
+
function ramp(
|
|
271
|
+
h: number,
|
|
272
|
+
kind: 'chromatic' | 'neutral' | 'achromatic',
|
|
273
|
+
profile = wallProfile,
|
|
274
|
+
strength = settings.vividness
|
|
275
|
+
) {
|
|
276
|
+
return lightness.map((base, i) => {
|
|
277
|
+
const L = kind === 'chromatic' ? tone(base, h, profile) : base
|
|
278
|
+
const envelope = Math.sin(Math.PI * (1 - L))
|
|
279
|
+
const C =
|
|
280
|
+
kind === 'neutral'
|
|
281
|
+
? settings.neutralTint * envelope ** 0.8
|
|
282
|
+
: kind === 'achromatic'
|
|
283
|
+
? 0
|
|
284
|
+
: Math.min(
|
|
285
|
+
mapper.maxChroma(L, h) * strength,
|
|
286
|
+
profile.cap * envelope ** profile.envelope
|
|
287
|
+
)
|
|
288
|
+
return { ...mapper.map(L, C, h), stop: stops[i] }
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
const regular = ramp(
|
|
292
|
+
hue,
|
|
293
|
+
neutralInput ? 'achromatic' : 'chromatic',
|
|
294
|
+
brandProfile,
|
|
295
|
+
settings.brandVividness
|
|
296
|
+
)
|
|
297
|
+
const closest = regular.reduce((a, b) =>
|
|
298
|
+
Math.abs(a.L - source.L) <= Math.abs(b.L - source.L) ? a : b
|
|
299
|
+
)
|
|
300
|
+
const anchor = regular.indexOf(closest)
|
|
301
|
+
const brand = regular.map((color, i) => {
|
|
302
|
+
if (settings.fidelity === 'unanchored') return color
|
|
303
|
+
if (i === anchor) return { ...source, stop: stops[i] }
|
|
304
|
+
const top = Math.max(regular[0].L, source.L),
|
|
305
|
+
bottom = Math.min(regular.at(-1)!.L, source.L)
|
|
306
|
+
const L =
|
|
307
|
+
settings.rampPreset === 'classic'
|
|
308
|
+
? i < anchor
|
|
309
|
+
? source.L + ((top - source.L) * (color.L - closest.L)) / (regular[0].L - closest.L)
|
|
310
|
+
: source.L -
|
|
311
|
+
((source.L - bottom) * (closest.L - color.L)) / (closest.L - regular.at(-1)!.L)
|
|
312
|
+
: reshapeLightness(
|
|
313
|
+
regular.at(-1)!.L,
|
|
314
|
+
closest.L,
|
|
315
|
+
regular[0].L,
|
|
316
|
+
bottom,
|
|
317
|
+
source.L,
|
|
318
|
+
top,
|
|
319
|
+
color.L,
|
|
320
|
+
anchor === 0 || anchor === regular.length - 1
|
|
321
|
+
)
|
|
322
|
+
const weight = Math.exp(-(((i - anchor) / brandProfile.radius) ** 2))
|
|
323
|
+
const C =
|
|
324
|
+
settings.rampPreset === 'classic'
|
|
325
|
+
? Math.max(0, color.C + (source.C - closest.C) * weight)
|
|
326
|
+
: neutralInput
|
|
327
|
+
? 0
|
|
328
|
+
: mapper.maxChroma(L, hue) *
|
|
329
|
+
clamp(
|
|
330
|
+
(color.C / Math.max(0.001, mapper.maxChroma(color.L, hue))) * (1 - weight) +
|
|
331
|
+
(source.C / Math.max(0.001, mapper.maxChroma(source.L, hue))) * weight
|
|
332
|
+
)
|
|
333
|
+
return { ...mapper.map(L, C, hue), stop: color.stop }
|
|
334
|
+
})
|
|
335
|
+
function palette(name: string, colors: typeof regular): ThemePalette {
|
|
336
|
+
return {
|
|
337
|
+
id: name.toLowerCase(),
|
|
338
|
+
name,
|
|
339
|
+
swatches: colors.map(({ stop, value }) => ({ stop, value }))
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return freezeTree({
|
|
343
|
+
input: settings,
|
|
344
|
+
neutralInput,
|
|
345
|
+
color: {
|
|
346
|
+
brand: { value: input.brand, gamut: 'srgb' as const },
|
|
347
|
+
fidelity: settings.fidelity,
|
|
348
|
+
...(settings.fidelity === 'anchored'
|
|
349
|
+
? { anchor: { paletteId: 'brand', stop: stops[anchor] } }
|
|
350
|
+
: {}),
|
|
351
|
+
palettes: [
|
|
352
|
+
palette('Brand', brand),
|
|
353
|
+
...families.map(([name, h]) => palette(name, ramp(wrapHue(h + rotation), 'chromatic'))),
|
|
354
|
+
palette('Neutral', ramp(hue, 'neutral'))
|
|
355
|
+
]
|
|
356
|
+
}
|
|
357
|
+
})
|
|
358
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { generateColorWall, colorCalibrationPresets } from './generateColorWall.js'
|
|
2
|
+
export type { ColorWall, ColorWallInput, ColorCalibrationPreset } from './generateColorWall.js'
|
|
3
|
+
export { recommendColorPalette } from './recommendColorPalette.js'
|
|
4
|
+
export type {
|
|
5
|
+
ColorPalette,
|
|
6
|
+
ColorPaletteOptions,
|
|
7
|
+
PaletteModeColors,
|
|
8
|
+
PaletteColorPair,
|
|
9
|
+
PaletteContrastCheck,
|
|
10
|
+
WorkingPaletteRole
|
|
11
|
+
} from './recommendColorPalette.js'
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// sRGB ↔ Oklab matrices: https://bottosson.github.io/posts/oklab/
|
|
2
|
+
// Transfer functions: https://www.w3.org/TR/css-color-4/#color-conversion-code
|
|
3
|
+
// This mapper deliberately reduces chroma at fixed L/h; it is not browser gamut mapping.
|
|
4
|
+
type Triple = readonly [number, number, number]
|
|
5
|
+
export interface OklchColor {
|
|
6
|
+
readonly value: string
|
|
7
|
+
readonly L: number
|
|
8
|
+
readonly C: number
|
|
9
|
+
readonly h: number
|
|
10
|
+
}
|
|
11
|
+
export const clamp = (n: number, low = 0, high = 1) => Math.max(low, Math.min(high, n))
|
|
12
|
+
export const wrapHue = (h: number) => ((h % 360) + 360) % 360
|
|
13
|
+
export const hueDifference = (a: number, b: number) => wrapHue(a - b + 180) - 180
|
|
14
|
+
const linear = (n: number) => (n <= 0.04045 ? n / 12.92 : ((n + 0.055) / 1.055) ** 2.4)
|
|
15
|
+
const encoded = (n: number) =>
|
|
16
|
+
n <= 0.0031308 ? 12.92 * n : 1.055 * Math.max(0, n) ** (1 / 2.4) - 0.055
|
|
17
|
+
|
|
18
|
+
export function measureHex(hex: string): OklchColor {
|
|
19
|
+
const [r, g, b] = [1, 3, 5].map(i => linear(parseInt(hex.slice(i, i + 2), 16) / 255))
|
|
20
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
|
|
21
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
|
|
22
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
|
|
23
|
+
const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s
|
|
24
|
+
const a = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s
|
|
25
|
+
const bAxis = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s
|
|
26
|
+
const C = Math.hypot(a, bAxis)
|
|
27
|
+
return { value: hex, L, C, h: C < 1e-6 ? 0 : wrapHue((Math.atan2(bAxis, a) * 180) / Math.PI) }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function linearRgb(L: number, C: number, h: number): Triple {
|
|
31
|
+
const angle = (h * Math.PI) / 180
|
|
32
|
+
const a = C * Math.cos(angle),
|
|
33
|
+
b = C * Math.sin(angle)
|
|
34
|
+
const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3
|
|
35
|
+
const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3
|
|
36
|
+
const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3
|
|
37
|
+
return [
|
|
38
|
+
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
|
|
39
|
+
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
|
|
40
|
+
-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Per-generation caches: bounded by this wall, without history-dependent global state. */
|
|
45
|
+
export function srgbMapper() {
|
|
46
|
+
const chromas = new Map<string, number>(),
|
|
47
|
+
cusps = new Map<number, number>()
|
|
48
|
+
function maxChroma(L: number, h: number): number {
|
|
49
|
+
if (L <= 0 || L >= 1) return 0
|
|
50
|
+
const key = `${L},${h}`
|
|
51
|
+
const cached = chromas.get(key)
|
|
52
|
+
if (cached !== undefined) return cached
|
|
53
|
+
let low = 0,
|
|
54
|
+
high = 0.5
|
|
55
|
+
for (let i = 0; i < 25; i++) {
|
|
56
|
+
const mid = (low + high) / 2
|
|
57
|
+
if (linearRgb(L, mid, h).every(n => n >= -1e-8 && n <= 1 + 1e-8)) low = mid
|
|
58
|
+
else high = mid
|
|
59
|
+
}
|
|
60
|
+
chromas.set(key, low)
|
|
61
|
+
return low
|
|
62
|
+
}
|
|
63
|
+
function cuspLightness(h: number): number {
|
|
64
|
+
const cached = cusps.get(h)
|
|
65
|
+
if (cached !== undefined) return cached
|
|
66
|
+
let bestL = 0.7,
|
|
67
|
+
bestC = 0
|
|
68
|
+
// Fixed sample locations and tie order are part of algorithm version 1.
|
|
69
|
+
for (let i = 20; i <= 98; i++) {
|
|
70
|
+
const L = i / 100,
|
|
71
|
+
C = maxChroma(L, h)
|
|
72
|
+
if (C > bestC) {
|
|
73
|
+
bestC = C
|
|
74
|
+
bestL = L
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
cusps.set(h, bestL)
|
|
78
|
+
return bestL
|
|
79
|
+
}
|
|
80
|
+
function map(L: number, C: number, h: number): OklchColor {
|
|
81
|
+
const rgb = linearRgb(clamp(L), Math.min(C, maxChroma(L, h)), h)
|
|
82
|
+
const value =
|
|
83
|
+
'#' +
|
|
84
|
+
rgb
|
|
85
|
+
.map(n =>
|
|
86
|
+
Math.round(clamp(encoded(n)) * 255)
|
|
87
|
+
.toString(16)
|
|
88
|
+
.padStart(2, '0')
|
|
89
|
+
)
|
|
90
|
+
.join('')
|
|
91
|
+
.toUpperCase()
|
|
92
|
+
return measureHex(value)
|
|
93
|
+
}
|
|
94
|
+
return { maxChroma, cuspLightness, map }
|
|
95
|
+
}
|