@contui/ui 0.0.0-stage → 0.1.0

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.
Files changed (427) hide show
  1. package/LICENSE +30 -0
  2. package/README.md +150 -2
  3. package/cli/contui.mjs +250 -0
  4. package/dist/charts/area-chart.d.ts +8 -0
  5. package/dist/charts/area-chart.js +9 -0
  6. package/dist/charts/bar-chart.d.ts +9 -0
  7. package/dist/charts/bar-chart.js +10 -0
  8. package/dist/charts/bubble-chart.d.ts +60 -0
  9. package/dist/charts/bubble-chart.js +392 -0
  10. package/dist/charts/bullet-chart.d.ts +53 -0
  11. package/dist/charts/bullet-chart.js +123 -0
  12. package/dist/charts/candlestick-chart.d.ts +77 -0
  13. package/dist/charts/candlestick-chart.js +558 -0
  14. package/dist/charts/cartesian.d.ts +121 -0
  15. package/dist/charts/cartesian.js +625 -0
  16. package/dist/charts/chart-card.d.ts +80 -0
  17. package/dist/charts/chart-card.js +114 -0
  18. package/dist/charts/donut-chart.d.ts +40 -0
  19. package/dist/charts/donut-chart.js +88 -0
  20. package/dist/charts/funnel-chart.d.ts +16 -0
  21. package/dist/charts/funnel-chart.js +164 -0
  22. package/dist/charts/horizontal-bar-chart.d.ts +67 -0
  23. package/dist/charts/horizontal-bar-chart.js +481 -0
  24. package/dist/charts/index.d.ts +21 -0
  25. package/dist/charts/index.js +32 -0
  26. package/dist/charts/legend.d.ts +23 -0
  27. package/dist/charts/legend.js +22 -0
  28. package/dist/charts/lib/candlestick-window.d.ts +15 -0
  29. package/dist/charts/lib/candlestick-window.js +23 -0
  30. package/dist/charts/lib/color.d.ts +39 -0
  31. package/dist/charts/lib/color.js +83 -0
  32. package/dist/charts/lib/format.d.ts +11 -0
  33. package/dist/charts/lib/format.js +33 -0
  34. package/dist/charts/lib/frame.d.ts +6 -0
  35. package/dist/charts/lib/frame.js +6 -0
  36. package/dist/charts/lib/layout.d.ts +11 -0
  37. package/dist/charts/lib/layout.js +71 -0
  38. package/dist/charts/lib/path.d.ts +35 -0
  39. package/dist/charts/lib/path.js +203 -0
  40. package/dist/charts/lib/scale.d.ts +41 -0
  41. package/dist/charts/lib/scale.js +80 -0
  42. package/dist/charts/lib/use-active.d.ts +18 -0
  43. package/dist/charts/lib/use-active.js +71 -0
  44. package/dist/charts/lib/use-size.d.ts +9 -0
  45. package/dist/charts/lib/use-size.js +40 -0
  46. package/dist/charts/line-chart.d.ts +7 -0
  47. package/dist/charts/line-chart.js +8 -0
  48. package/dist/charts/radar-chart.d.ts +27 -0
  49. package/dist/charts/radar-chart.js +165 -0
  50. package/dist/charts/rose-chart.d.ts +35 -0
  51. package/dist/charts/rose-chart.js +242 -0
  52. package/dist/charts/sparkline.d.ts +35 -0
  53. package/dist/charts/sparkline.js +110 -0
  54. package/dist/charts/tooltip.d.ts +35 -0
  55. package/dist/charts/tooltip.js +22 -0
  56. package/dist/charts/treemap.d.ts +28 -0
  57. package/dist/charts/treemap.js +103 -0
  58. package/dist/charts/uptime-strip.d.ts +40 -0
  59. package/dist/charts/uptime-strip.js +178 -0
  60. package/dist/components/accordion.d.ts +31 -0
  61. package/dist/components/accordion.js +34 -0
  62. package/dist/components/activity-item.d.ts +12 -0
  63. package/dist/components/activity-item.js +6 -0
  64. package/dist/components/alert.d.ts +21 -0
  65. package/dist/components/alert.js +58 -0
  66. package/dist/components/assistant-answer.d.ts +52 -0
  67. package/dist/components/assistant-answer.js +40 -0
  68. package/dist/components/assistant-prompt.d.ts +63 -0
  69. package/dist/components/assistant-prompt.js +122 -0
  70. package/dist/components/avatar.d.ts +28 -0
  71. package/dist/components/avatar.js +75 -0
  72. package/dist/components/badge.d.ts +16 -0
  73. package/dist/components/badge.js +33 -0
  74. package/dist/components/bar-list.d.ts +65 -0
  75. package/dist/components/bar-list.js +119 -0
  76. package/dist/components/block-slider.d.ts +43 -0
  77. package/dist/components/block-slider.js +63 -0
  78. package/dist/components/branch-graph.d.ts +87 -0
  79. package/dist/components/branch-graph.js +231 -0
  80. package/dist/components/breadcrumb.d.ts +41 -0
  81. package/dist/components/breadcrumb.js +53 -0
  82. package/dist/components/button.d.ts +23 -0
  83. package/dist/components/button.js +108 -0
  84. package/dist/components/calendar.d.ts +5 -0
  85. package/dist/components/calendar.js +90 -0
  86. package/dist/components/card.d.ts +65 -0
  87. package/dist/components/card.js +104 -0
  88. package/dist/components/chat-attachment.d.ts +47 -0
  89. package/dist/components/chat-attachment.js +63 -0
  90. package/dist/components/chat-event.d.ts +7 -0
  91. package/dist/components/chat-event.js +7 -0
  92. package/dist/components/chat-message.d.ts +59 -0
  93. package/dist/components/chat-message.js +91 -0
  94. package/dist/components/chat-reference.d.ts +20 -0
  95. package/dist/components/chat-reference.js +41 -0
  96. package/dist/components/chat-shared.d.ts +21 -0
  97. package/dist/components/chat-shared.js +23 -0
  98. package/dist/components/chat-suggestions.d.ts +59 -0
  99. package/dist/components/chat-suggestions.js +88 -0
  100. package/dist/components/chat-thread.d.ts +39 -0
  101. package/dist/components/chat-thread.js +172 -0
  102. package/dist/components/chat-typing.d.ts +35 -0
  103. package/dist/components/chat-typing.js +66 -0
  104. package/dist/components/checkbox.d.ts +18 -0
  105. package/dist/components/checkbox.js +33 -0
  106. package/dist/components/checklist.d.ts +26 -0
  107. package/dist/components/checklist.js +18 -0
  108. package/dist/components/chip.d.ts +29 -0
  109. package/dist/components/chip.js +61 -0
  110. package/dist/components/code-input.d.ts +40 -0
  111. package/dist/components/code-input.js +285 -0
  112. package/dist/components/color-picker.d.ts +38 -0
  113. package/dist/components/color-picker.js +855 -0
  114. package/dist/components/combobox.d.ts +75 -0
  115. package/dist/components/combobox.js +112 -0
  116. package/dist/components/command-menu.d.ts +58 -0
  117. package/dist/components/command-menu.js +166 -0
  118. package/dist/components/copy-button.d.ts +16 -0
  119. package/dist/components/copy-button.js +31 -0
  120. package/dist/components/data-grid.d.ts +50 -0
  121. package/dist/components/data-grid.js +599 -0
  122. package/dist/components/date-picker.d.ts +109 -0
  123. package/dist/components/date-picker.js +268 -0
  124. package/dist/components/description-list.d.ts +12 -0
  125. package/dist/components/description-list.js +10 -0
  126. package/dist/components/dialog.d.ts +57 -0
  127. package/dist/components/dialog.js +140 -0
  128. package/dist/components/dock.d.ts +45 -0
  129. package/dist/components/dock.js +79 -0
  130. package/dist/components/drop-zone.d.ts +46 -0
  131. package/dist/components/drop-zone.js +101 -0
  132. package/dist/components/dropdown-menu.d.ts +50 -0
  133. package/dist/components/dropdown-menu.js +78 -0
  134. package/dist/components/editable-cell.d.ts +34 -0
  135. package/dist/components/editable-cell.js +89 -0
  136. package/dist/components/empty-state.d.ts +17 -0
  137. package/dist/components/empty-state.js +9 -0
  138. package/dist/components/field.d.ts +43 -0
  139. package/dist/components/field.js +75 -0
  140. package/dist/components/filter-condition.d.ts +188 -0
  141. package/dist/components/filter-condition.js +113 -0
  142. package/dist/components/filter-menu.d.ts +57 -0
  143. package/dist/components/filter-menu.js +46 -0
  144. package/dist/components/header-bar.d.ts +84 -0
  145. package/dist/components/header-bar.js +94 -0
  146. package/dist/components/hold-button.d.ts +15 -0
  147. package/dist/components/hold-button.js +88 -0
  148. package/dist/components/icon-tile.d.ts +12 -0
  149. package/dist/components/icon-tile.js +29 -0
  150. package/dist/components/info-tip.d.ts +13 -0
  151. package/dist/components/info-tip.js +26 -0
  152. package/dist/components/input.d.ts +17 -0
  153. package/dist/components/input.js +83 -0
  154. package/dist/components/kbd.d.ts +6 -0
  155. package/dist/components/kbd.js +7 -0
  156. package/dist/components/menu-item.d.ts +56 -0
  157. package/dist/components/menu-item.js +94 -0
  158. package/dist/components/message-composer.d.ts +75 -0
  159. package/dist/components/message-composer.js +199 -0
  160. package/dist/components/nav-rail.d.ts +31 -0
  161. package/dist/components/nav-rail.js +85 -0
  162. package/dist/components/notice.d.ts +45 -0
  163. package/dist/components/notice.js +66 -0
  164. package/dist/components/optimistic-undo.d.ts +28 -0
  165. package/dist/components/optimistic-undo.js +43 -0
  166. package/dist/components/option-row.d.ts +15 -0
  167. package/dist/components/option-row.js +14 -0
  168. package/dist/components/page-content.d.ts +11 -0
  169. package/dist/components/page-content.js +31 -0
  170. package/dist/components/pagination.d.ts +34 -0
  171. package/dist/components/pagination.js +41 -0
  172. package/dist/components/popover.d.ts +27 -0
  173. package/dist/components/popover.js +25 -0
  174. package/dist/components/progress.d.ts +33 -0
  175. package/dist/components/progress.js +75 -0
  176. package/dist/components/radio-group.d.ts +7 -0
  177. package/dist/components/radio-group.js +33 -0
  178. package/dist/components/readonly-field.d.ts +33 -0
  179. package/dist/components/readonly-field.js +80 -0
  180. package/dist/components/segmented-control.d.ts +21 -0
  181. package/dist/components/segmented-control.js +108 -0
  182. package/dist/components/select.d.ts +42 -0
  183. package/dist/components/select.js +86 -0
  184. package/dist/components/separator.d.ts +10 -0
  185. package/dist/components/separator.js +7 -0
  186. package/dist/components/setting-row.d.ts +53 -0
  187. package/dist/components/setting-row.js +93 -0
  188. package/dist/components/sidebar-switcher.d.ts +47 -0
  189. package/dist/components/sidebar-switcher.js +20 -0
  190. package/dist/components/sidebar.d.ts +220 -0
  191. package/dist/components/sidebar.js +327 -0
  192. package/dist/components/skeleton.d.ts +18 -0
  193. package/dist/components/skeleton.js +23 -0
  194. package/dist/components/slider.d.ts +15 -0
  195. package/dist/components/slider.js +28 -0
  196. package/dist/components/spinner.d.ts +14 -0
  197. package/dist/components/spinner.js +19 -0
  198. package/dist/components/status-dot.d.ts +24 -0
  199. package/dist/components/status-dot.js +35 -0
  200. package/dist/components/stepper.d.ts +60 -0
  201. package/dist/components/stepper.js +197 -0
  202. package/dist/components/switch.d.ts +9 -0
  203. package/dist/components/switch.js +26 -0
  204. package/dist/components/table-cell.d.ts +38 -0
  205. package/dist/components/table-cell.js +107 -0
  206. package/dist/components/table-context.d.ts +48 -0
  207. package/dist/components/table-context.js +69 -0
  208. package/dist/components/table-head.d.ts +42 -0
  209. package/dist/components/table-head.js +188 -0
  210. package/dist/components/table-tree.d.ts +96 -0
  211. package/dist/components/table-tree.js +137 -0
  212. package/dist/components/table.d.ts +78 -0
  213. package/dist/components/table.js +119 -0
  214. package/dist/components/tabs.d.ts +27 -0
  215. package/dist/components/tabs.js +79 -0
  216. package/dist/components/textarea.d.ts +3 -0
  217. package/dist/components/textarea.js +20 -0
  218. package/dist/components/theme.d.ts +41 -0
  219. package/dist/components/theme.js +60 -0
  220. package/dist/components/thinking.d.ts +68 -0
  221. package/dist/components/thinking.js +94 -0
  222. package/dist/components/toast.d.ts +39 -0
  223. package/dist/components/toast.js +110 -0
  224. package/dist/components/toolbar.d.ts +25 -0
  225. package/dist/components/toolbar.js +92 -0
  226. package/dist/components/tooltip.d.ts +23 -0
  227. package/dist/components/tooltip.js +27 -0
  228. package/dist/components/top-nav.d.ts +129 -0
  229. package/dist/components/top-nav.js +211 -0
  230. package/dist/components/truncate.d.ts +29 -0
  231. package/dist/components/truncate.js +138 -0
  232. package/dist/deprecated.d.ts +43 -0
  233. package/dist/deprecated.js +47 -0
  234. package/dist/index.d.ts +93 -0
  235. package/dist/index.js +93 -0
  236. package/dist/lib/cn.d.ts +5 -0
  237. package/dist/lib/cn.js +14 -0
  238. package/dist/lib/color.d.ts +136 -0
  239. package/dist/lib/color.js +307 -0
  240. package/dist/lib/contrast.d.ts +53 -0
  241. package/dist/lib/contrast.js +169 -0
  242. package/dist/lib/create-theme.d.ts +240 -0
  243. package/dist/lib/create-theme.js +461 -0
  244. package/dist/lib/harmony.d.ts +102 -0
  245. package/dist/lib/harmony.js +426 -0
  246. package/dist/lib/import-theme.d.ts +22 -0
  247. package/dist/lib/import-theme.js +221 -0
  248. package/dist/lib/overflow-scroll.d.ts +25 -0
  249. package/dist/lib/overflow-scroll.js +66 -0
  250. package/dist/lib/radio-toggle-render.d.ts +3 -0
  251. package/dist/lib/radio-toggle-render.js +9 -0
  252. package/dist/lib/reorder.d.ts +38 -0
  253. package/dist/lib/reorder.js +193 -0
  254. package/dist/lib/spacing.d.ts +101 -0
  255. package/dist/lib/spacing.js +118 -0
  256. package/dist/lib/tailwind-palette.d.ts +5 -0
  257. package/dist/lib/tailwind-palette.js +351 -0
  258. package/dist/lib/truncate.d.ts +18 -0
  259. package/dist/lib/truncate.js +69 -0
  260. package/dist/lib/use-single-value.d.ts +5 -0
  261. package/dist/lib/use-single-value.js +17 -0
  262. package/dist/styles.css +7988 -0
  263. package/mcp/server.mjs +87590 -0
  264. package/package.json +58 -5
  265. package/skills/continuum-fix-padding/SKILL.md +69 -0
  266. package/skills/continuum-fix-padding/scripts/compute.ts +155 -0
  267. package/skills/continuum-fix-padding/scripts/spacing.ts +189 -0
  268. package/skills/continuum-taste/SKILL.md +186 -0
  269. package/skills/continuum-taste/assets/tokens.css +647 -0
  270. package/skills/continuum-taste/references/charts.md +375 -0
  271. package/skills/continuum-taste/references/components.md +1369 -0
  272. package/skills/continuum-taste/references/foundations.md +539 -0
  273. package/skills/continuum-taste/references/media/canvas.md +75 -0
  274. package/skills/continuum-taste/references/media/css.md +61 -0
  275. package/skills/continuum-taste/references/media/react.md +161 -0
  276. package/skills/continuum-taste/references/method.md +87 -0
  277. package/skills/continuum-taste/references/pages.md +391 -0
  278. package/skills/continuum-taste/references/patterns.md +77 -0
  279. package/skills/continuum-taste/references/screens/agent.tsx +450 -0
  280. package/skills/continuum-taste/references/screens/analytics-overview.tsx +377 -0
  281. package/skills/continuum-taste/references/screens/checkout-funnel.tsx +193 -0
  282. package/skills/continuum-taste/references/screens/customer.tsx +475 -0
  283. package/skills/continuum-taste/references/screens/customers.tsx +608 -0
  284. package/skills/continuum-taste/references/screens/index.tsx +72 -0
  285. package/skills/continuum-taste/references/screens/invoice-editor.tsx +558 -0
  286. package/skills/continuum-taste/references/screens/new-customer.tsx +205 -0
  287. package/skills/continuum-taste/references/screens/overview.tsx +196 -0
  288. package/skills/continuum-taste/references/screens/report-generator.tsx +329 -0
  289. package/skills/continuum-taste/references/screens/risk-report.tsx +577 -0
  290. package/skills/continuum-taste/references/screens/sequence-editor.tsx +455 -0
  291. package/skills/continuum-taste/references/screens/settings.tsx +288 -0
  292. package/skills/continuum-taste/references/screens/shared.tsx +204 -0
  293. package/skills/continuum-taste/references/screens/sign-in.tsx +104 -0
  294. package/skills/continuum-taste/references/screens/workspace.tsx +596 -0
  295. package/skills/continuum-taste/references/theming.md +95 -0
  296. package/src/charts/area-chart.tsx +17 -0
  297. package/src/charts/bar-chart.tsx +15 -0
  298. package/src/charts/bubble-chart.tsx +913 -0
  299. package/src/charts/bullet-chart.tsx +419 -0
  300. package/src/charts/candlestick-chart.tsx +1242 -0
  301. package/src/charts/cartesian.tsx +1437 -0
  302. package/src/charts/chart-card.tsx +394 -0
  303. package/src/charts/donut-chart.tsx +299 -0
  304. package/src/charts/funnel-chart.tsx +373 -0
  305. package/src/charts/horizontal-bar-chart.tsx +991 -0
  306. package/src/charts/index.ts +76 -0
  307. package/src/charts/legend.tsx +59 -0
  308. package/src/charts/lib/candlestick-window.ts +42 -0
  309. package/src/charts/lib/color.ts +110 -0
  310. package/src/charts/lib/format.ts +42 -0
  311. package/src/charts/lib/frame.ts +6 -0
  312. package/src/charts/lib/layout.ts +78 -0
  313. package/src/charts/lib/path.ts +225 -0
  314. package/src/charts/lib/scale.ts +91 -0
  315. package/src/charts/lib/use-active.ts +87 -0
  316. package/src/charts/lib/use-size.ts +45 -0
  317. package/src/charts/line-chart.tsx +16 -0
  318. package/src/charts/radar-chart.tsx +485 -0
  319. package/src/charts/rose-chart.tsx +553 -0
  320. package/src/charts/sparkline.tsx +293 -0
  321. package/src/charts/tooltip.tsx +105 -0
  322. package/src/charts/treemap.tsx +271 -0
  323. package/src/charts/uptime-strip.tsx +469 -0
  324. package/src/components/accordion.tsx +136 -0
  325. package/src/components/activity-item.tsx +43 -0
  326. package/src/components/alert.tsx +135 -0
  327. package/src/components/assistant-answer.tsx +185 -0
  328. package/src/components/assistant-prompt.tsx +352 -0
  329. package/src/components/avatar.tsx +157 -0
  330. package/src/components/badge.tsx +55 -0
  331. package/src/components/bar-list.tsx +350 -0
  332. package/src/components/block-slider.tsx +189 -0
  333. package/src/components/branch-graph.tsx +580 -0
  334. package/src/components/breadcrumb.tsx +173 -0
  335. package/src/components/button.tsx +157 -0
  336. package/src/components/calendar.tsx +154 -0
  337. package/src/components/card.tsx +296 -0
  338. package/src/components/chat-attachment.tsx +255 -0
  339. package/src/components/chat-event.tsx +34 -0
  340. package/src/components/chat-message.tsx +332 -0
  341. package/src/components/chat-reference.tsx +100 -0
  342. package/src/components/chat-shared.ts +41 -0
  343. package/src/components/chat-suggestions.tsx +188 -0
  344. package/src/components/chat-thread.tsx +348 -0
  345. package/src/components/chat-typing.tsx +155 -0
  346. package/src/components/checkbox.tsx +83 -0
  347. package/src/components/checklist.tsx +146 -0
  348. package/src/components/chip.tsx +152 -0
  349. package/src/components/code-input.tsx +406 -0
  350. package/src/components/color-picker.tsx +1385 -0
  351. package/src/components/combobox.tsx +420 -0
  352. package/src/components/command-menu.tsx +405 -0
  353. package/src/components/copy-button.tsx +87 -0
  354. package/src/components/data-grid.tsx +965 -0
  355. package/src/components/date-picker.tsx +652 -0
  356. package/src/components/description-list.tsx +39 -0
  357. package/src/components/dialog.tsx +367 -0
  358. package/src/components/dock.tsx +192 -0
  359. package/src/components/drop-zone.tsx +283 -0
  360. package/src/components/dropdown-menu.tsx +323 -0
  361. package/src/components/editable-cell.tsx +219 -0
  362. package/src/components/empty-state.tsx +50 -0
  363. package/src/components/field.tsx +148 -0
  364. package/src/components/filter-condition.tsx +463 -0
  365. package/src/components/filter-menu.tsx +196 -0
  366. package/src/components/header-bar.tsx +220 -0
  367. package/src/components/hold-button.tsx +146 -0
  368. package/src/components/icon-tile.tsx +43 -0
  369. package/src/components/info-tip.tsx +87 -0
  370. package/src/components/input.tsx +136 -0
  371. package/src/components/kbd.tsx +17 -0
  372. package/src/components/menu-item.tsx +216 -0
  373. package/src/components/message-composer.tsx +458 -0
  374. package/src/components/nav-rail.tsx +235 -0
  375. package/src/components/notice.tsx +133 -0
  376. package/src/components/optimistic-undo.ts +68 -0
  377. package/src/components/option-row.tsx +54 -0
  378. package/src/components/page-content.tsx +44 -0
  379. package/src/components/pagination.tsx +182 -0
  380. package/src/components/popover.tsx +113 -0
  381. package/src/components/progress.tsx +222 -0
  382. package/src/components/radio-group.tsx +80 -0
  383. package/src/components/readonly-field.tsx +251 -0
  384. package/src/components/segmented-control.tsx +205 -0
  385. package/src/components/select.tsx +259 -0
  386. package/src/components/separator.tsx +30 -0
  387. package/src/components/setting-row.tsx +277 -0
  388. package/src/components/sidebar-switcher.tsx +134 -0
  389. package/src/components/sidebar.tsx +804 -0
  390. package/src/components/skeleton.tsx +57 -0
  391. package/src/components/slider.tsx +106 -0
  392. package/src/components/spinner.tsx +43 -0
  393. package/src/components/status-dot.tsx +100 -0
  394. package/src/components/stepper.tsx +459 -0
  395. package/src/components/switch.tsx +54 -0
  396. package/src/components/table-cell.tsx +239 -0
  397. package/src/components/table-context.ts +135 -0
  398. package/src/components/table-head.tsx +393 -0
  399. package/src/components/table-tree.tsx +357 -0
  400. package/src/components/table.tsx +295 -0
  401. package/src/components/tabs.tsx +191 -0
  402. package/src/components/textarea.tsx +44 -0
  403. package/src/components/theme.tsx +120 -0
  404. package/src/components/thinking.tsx +295 -0
  405. package/src/components/toast.tsx +308 -0
  406. package/src/components/toolbar.tsx +166 -0
  407. package/src/components/tooltip.tsx +84 -0
  408. package/src/components/top-nav.tsx +660 -0
  409. package/src/components/truncate.tsx +234 -0
  410. package/src/deprecated.ts +61 -0
  411. package/src/index.ts +184 -0
  412. package/src/lib/cn.ts +22 -0
  413. package/src/lib/color.ts +469 -0
  414. package/src/lib/contrast.ts +220 -0
  415. package/src/lib/create-theme.ts +659 -0
  416. package/src/lib/harmony.ts +609 -0
  417. package/src/lib/import-theme.ts +272 -0
  418. package/src/lib/overflow-scroll.tsx +108 -0
  419. package/src/lib/radio-toggle-render.tsx +15 -0
  420. package/src/lib/reorder.ts +222 -0
  421. package/src/lib/spacing.ts +189 -0
  422. package/src/lib/tailwind-palette.ts +355 -0
  423. package/src/lib/truncate.ts +79 -0
  424. package/src/lib/use-single-value.ts +23 -0
  425. package/src/styles/continuum.css +3 -0
  426. package/src/styles/theme.css +524 -0
  427. package/src/styles/tokens.css +647 -0
@@ -0,0 +1,469 @@
1
+ /*
2
+ * Colour maths for the theme builder: sRGB hex to and from oklch, the sRGB and Display P3 gamuts,
3
+ * and ramps built the way Tailwind's palette is built.
4
+ *
5
+ * Read off Tailwind's own values (tailwind-palette.ts), its palette follows four rules:
6
+ * - The ends are fixed and the middle follows the hue. Every hue starts near the same white and ends
7
+ * near the same black, but its middle sits where the hue can be most vivid: yellow high, indigo low.
8
+ * - Chroma is a share of what the gamut allows at each step, not one bell: about four fifths at the
9
+ * ends and nine tenths in the middle, so the most vivid step moves with the hue (400 for yellow, 600
10
+ * for blue). The palette is drawn in Display P3; most of its 500s lie outside sRGB.
11
+ * - The hue drifts at the ends, warm hues most: lighter towards yellow, darker towards red, so a dark
12
+ * yellow turns brown rather than olive.
13
+ * - Greys are a curve of their own, darker below 300, with a faint chroma that peaks in the middle.
14
+ * The engine interpolates those per-step curves between the two Tailwind families nearest in hue.
15
+ */
16
+
17
+ import { tailwindGreys, tailwindHues } from './tailwind-palette';
18
+
19
+ export type Oklch = { l: number; c: number; h: number };
20
+
21
+ /** The colours a ramp may use: Display P3, as Tailwind's palette, or sRGB, which every screen shows. */
22
+ export type Gamut = 'p3' | 'srgb';
23
+
24
+ const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
25
+
26
+ /** "#abc", "#aabbcc" or "aabbcc" to 0–1 channels; null when it is not a hex colour. */
27
+ export const parseHex = (hex: string): [number, number, number] | null => {
28
+ const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex.trim());
29
+ if (!match?.[1]) return null;
30
+ const digits = match[1].length === 3 ? [...match[1]].map(d => d + d).join('') : match[1];
31
+ const value = Number.parseInt(digits, 16);
32
+ return [((value >> 16) & 255) / 255, ((value >> 8) & 255) / 255, (value & 255) / 255];
33
+ };
34
+
35
+ export const toHex = ([r, g, b]: [number, number, number]) =>
36
+ `#${[r, g, b]
37
+ .map(channel =>
38
+ Math.round(clamp01(channel) * 255)
39
+ .toString(16)
40
+ .padStart(2, '0'),
41
+ )
42
+ .join('')}`;
43
+
44
+ const toLinear = (channel: number) =>
45
+ channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
46
+
47
+ const fromLinear = (channel: number) =>
48
+ channel <= 0.0031308 ? channel * 12.92 : 1.055 * channel ** (1 / 2.4) - 0.055;
49
+
50
+ /** Linear sRGB to oklch (Björn Ottosson's OKLab); channels outside 0–1 are colours outside sRGB. */
51
+ export const linearSrgbToOklch = ([lr, lg, lb]: readonly [number, number, number]): Oklch => {
52
+ const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb);
53
+ const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb);
54
+ const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb);
55
+ const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s;
56
+ const a = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s;
57
+ const bb = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s;
58
+ const c = Math.hypot(a, bb);
59
+ const h = ((Math.atan2(bb, a) * 180) / Math.PI + 360) % 360;
60
+ return { l: L, c, h };
61
+ };
62
+
63
+ /** sRGB channels to oklch. */
64
+ const rgbToOklch = ([r, g, b]: [number, number, number]): Oklch =>
65
+ linearSrgbToOklch([toLinear(r), toLinear(g), toLinear(b)]);
66
+
67
+ /** Linear sRGB channels to gamma-encoded ones, as a screen is sent them. */
68
+ export const encodeSrgb = (channel: number) => fromLinear(channel);
69
+
70
+ /** oklch to linear sRGB, unclamped: a channel outside 0–1 means the colour is outside sRGB. */
71
+ export const oklchToLinearSrgb = ({ l: L, c, h }: Oklch): [number, number, number] => {
72
+ const a = c * Math.cos((h * Math.PI) / 180);
73
+ const bb = c * Math.sin((h * Math.PI) / 180);
74
+ const l = (L + 0.3963377774 * a + 0.2158037573 * bb) ** 3;
75
+ const m = (L - 0.1055613458 * a - 0.0638541728 * bb) ** 3;
76
+ const s = (L - 0.0894841775 * a - 1.291485548 * bb) ** 3;
77
+ return [
78
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
79
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
80
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
81
+ ];
82
+ };
83
+
84
+ /** Linear sRGB to linear Display P3 (both D65). */
85
+ const linearSrgbToP3 = ([r, g, b]: [number, number, number]): [number, number, number] => [
86
+ 0.8224621209 * r + 0.1775378791 * g,
87
+ 0.0331941989 * r + 0.9668058011 * g,
88
+ 0.0170826307 * r + 0.0723974407 * g + 0.9105199286 * b,
89
+ ];
90
+
91
+ const inside = (channels: [number, number, number]) =>
92
+ channels.every(channel => channel >= -0.0001 && channel <= 1.0001);
93
+
94
+ /** Whether a colour lies inside a gamut. */
95
+ export const inGamut = (color: Oklch, gamut: Gamut) => {
96
+ const linear = oklchToLinearSrgb(color);
97
+ return inside(gamut === 'p3' ? linearSrgbToP3(linear) : linear);
98
+ };
99
+
100
+ /** The most chroma a lightness and hue can carry inside a gamut. */
101
+ export const maxChroma = (l: number, h: number, gamut: Gamut) => {
102
+ if (l <= 0 || l >= 1) return 0;
103
+ let low = 0;
104
+ let high = 0.45;
105
+ for (let i = 0; i < 24; i++) {
106
+ const mid = (low + high) / 2;
107
+ if (inGamut({ l, c: mid, h }, gamut)) low = mid;
108
+ else high = mid;
109
+ }
110
+ return low;
111
+ };
112
+
113
+ /** The colour as sRGB hex; outside sRGB its chroma is reduced at the same lightness and hue until it fits. */
114
+ export const oklchToHex = (color: Oklch) =>
115
+ toHex(
116
+ oklchToLinearSrgb({
117
+ ...color,
118
+ c: Math.min(color.c, maxChroma(color.l, color.h, 'srgb')),
119
+ }).map(fromLinear) as [number, number, number],
120
+ );
121
+
122
+ export const hexToOklch = (hex: string): Oklch | null => {
123
+ const rgb = parseHex(hex);
124
+ return rgb ? rgbToOklch(rgb) : null;
125
+ };
126
+
127
+ const round = (value: number, digits: number) => Number(value.toFixed(digits));
128
+
129
+ /** The colour as CSS, `oklch(62.3% 0.214 259.8)`, as Tailwind writes its palette. */
130
+ export const formatOklch = ({ l, c, h }: Oklch) =>
131
+ `oklch(${round(l * 100, 1)}% ${round(c, 3)} ${round(c < 0.0005 ? 0 : h, 1)})`;
132
+
133
+ /** A colour for a stylesheet: oklch inside P3, or hex when the ramp keeps to sRGB. */
134
+ export const toCss = (color: Oklch, gamut: Gamut) =>
135
+ gamut === 'srgb' ? oklchToHex(color) : formatOklch(color);
136
+
137
+ /** The eleven steps of a ramp, 50 to 950. */
138
+ export type RampSteps = [
139
+ string,
140
+ string,
141
+ string,
142
+ string,
143
+ string,
144
+ string,
145
+ string,
146
+ string,
147
+ string,
148
+ string,
149
+ string,
150
+ ];
151
+
152
+ /** The step names, by index. */
153
+ export const rampSteps = [
154
+ '50',
155
+ '100',
156
+ '200',
157
+ '300',
158
+ '400',
159
+ '500',
160
+ '600',
161
+ '700',
162
+ '800',
163
+ '900',
164
+ '950',
165
+ ] as const;
166
+
167
+ export type RampStep = (typeof rampSteps)[number];
168
+
169
+ /** One channel of a ramp: lightness, chroma or hue. */
170
+ export type RampChannel = 'l' | 'c' | 'h';
171
+
172
+ /**
173
+ * One step of a ramp set by hand, in absolute oklch: lightness 0–1, chroma, hue in degrees. A channel
174
+ * left out follows the curve; a chroma left out keeps the step's share of the gamut, so a step made
175
+ * lighter or turned stays as vivid as its neighbours.
176
+ */
177
+ export type RampStepOverride = { l?: number; c?: number; h?: number };
178
+
179
+ /**
180
+ * How a ramp is shaped, whole curves at a time; every value left out is Tailwind's. Each moves a
181
+ * curve as a whole, never one step, so the ramp stays even. `steps` is the exception: single steps
182
+ * set by hand, laid over the curves once they are built.
183
+ */
184
+ export type RampShape = {
185
+ /** Lightness of the 50, 0–1. */
186
+ lightest?: number;
187
+ /** Lightness of the 950, 0–1. */
188
+ darkest?: number;
189
+ /** Bends the steps between the ends and the seed: -1 darker midtones, 0 Tailwind's, 1 lighter. */
190
+ midtones?: number;
191
+ /** Chroma of every step but the seed, times this: 0 grey, 1 Tailwind's, up to the gamut. */
192
+ saturation?: number;
193
+ /** How much the ends fade against the middle: 0 not at all, 1 Tailwind's, 2 twice as much. */
194
+ taper?: number;
195
+ /** Tailwind's hue drift along the ramp, times this: 0 one hue throughout, 1 Tailwind's. */
196
+ drift?: number;
197
+ /** Degrees the hue turns at the 50, growing from the seed. */
198
+ lightHue?: number;
199
+ /** Degrees the hue turns at the 950, growing from the seed. */
200
+ darkHue?: number;
201
+ /**
202
+ * Steps set by hand, absolute, over the curves: the curves' controls move every other step and
203
+ * leave these where they were put. A primary's seed is the accent itself and ignores its entry.
204
+ */
205
+ steps?: Partial<Record<RampStep, RampStepOverride>>;
206
+ };
207
+
208
+ /** The shape controls that move a whole curve: every key but `steps`. */
209
+ export type RampCurveKey = Exclude<keyof RampShape, 'steps'>;
210
+
211
+ /** What every shape control is when left alone. */
212
+ export const neutralShape: Required<Omit<RampShape, 'lightest' | 'darkest' | 'steps'>> = {
213
+ midtones: 0,
214
+ saturation: 1,
215
+ taper: 1,
216
+ drift: 1,
217
+ lightHue: 0,
218
+ darkHue: 0,
219
+ };
220
+
221
+ /** The signed difference between two hues, -180 to 180. */
222
+ const hueDelta = (a: number, b: number) => ((a - b + 540) % 360) - 180;
223
+
224
+ const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
225
+
226
+ /* ---------- Tailwind's curves, by hue ---------- */
227
+
228
+ type Profile = {
229
+ /** The family's hue at 500, what it is placed by. */
230
+ hue: number;
231
+ /** Lightness per step. */
232
+ l: number[];
233
+ /** Chroma per step as a share of what the gamut allows there. */
234
+ share: Record<Gamut, number[]>;
235
+ /** The hue per step, relative to the 500. */
236
+ drift: number[];
237
+ };
238
+
239
+ const profiles: Profile[] = Object.values(tailwindHues)
240
+ .map(ramp => {
241
+ const hue = ramp[5]?.[2] ?? 0;
242
+ const share = (gamut: Gamut) =>
243
+ ramp.map(([l, c, h]) => Math.min(1, c / Math.max(1e-6, maxChroma(l, h, gamut))));
244
+ return {
245
+ hue,
246
+ l: ramp.map(([l]) => l),
247
+ share: { p3: share('p3'), srgb: share('srgb') },
248
+ drift: ramp.map(([, , h]) => hueDelta(h, hue)),
249
+ };
250
+ })
251
+ .sort((a, b) => a.hue - b.hue);
252
+
253
+ /**
254
+ * Tailwind's curves for a hue: the two families on either side of it on the colour wheel, mixed by
255
+ * how near each is. `without` leaves families out, for testing the engine against one it has not seen.
256
+ */
257
+ export const paletteProfile = (hue: number, without: readonly number[] = []) => {
258
+ const pool = profiles.filter(profile => !without.includes(profile.hue));
259
+ const h = ((hue % 360) + 360) % 360;
260
+ let after = pool.findIndex(profile => profile.hue >= h);
261
+ if (after < 0) after = 0;
262
+ const next = pool[after] as Profile;
263
+ const previous = pool[(after - 1 + pool.length) % pool.length] as Profile;
264
+ const span = (next.hue - previous.hue + 360) % 360 || 360;
265
+ const t = ((h - previous.hue + 360) % 360) / span;
266
+ const mix = (a: readonly number[], b: readonly number[]) =>
267
+ a.map((x, i) => lerp(x, b[i] ?? x, t));
268
+ return {
269
+ l: mix(previous.l, next.l),
270
+ share: {
271
+ p3: mix(previous.share.p3, next.share.p3),
272
+ srgb: mix(previous.share.srgb, next.share.srgb),
273
+ },
274
+ drift: mix(previous.drift, next.drift),
275
+ };
276
+ };
277
+
278
+ /**
279
+ * The tinted greys' chroma per step, relative to their 500, where the tint is given.
280
+ *
281
+ * Up to the 500 it rises as Tailwind's tinted greys do, taken together: they agree there, a 50
282
+ * carrying about a twentieth of the 500's chroma and a 400 three quarters. From the 500 down it
283
+ * holds. Tailwind's greys part ways in the dark half: slate and gray keep their tint into the 950,
284
+ * zinc, stone and the newer four let it fade to a third or less. Averaged, as this once was, the
285
+ * darkest steps kept two fifths of the tint, and those are the steps a tint has to show on: the
286
+ * surface and the canvas in dark mode, the text in light. A tint that fades there reads as no tint
287
+ * at all, so the dark half follows the two that hold. A grey that should fade is given its steps
288
+ * by hand.
289
+ */
290
+ const greyShape = (() => {
291
+ const tinted = Object.values(tailwindGreys).filter(ramp => ramp.some(([, c]) => c > 0.001));
292
+ const shapes = tinted.map(ramp => {
293
+ const middle = Math.max(1e-6, ramp[5]?.[1] ?? 0);
294
+ return ramp.map(([, c]) => c / middle);
295
+ });
296
+ return rampSteps.map((_, i) =>
297
+ i >= 5
298
+ ? 1
299
+ : Math.min(1, shapes.reduce((sum, shape) => sum + (shape[i] ?? 0), 0) / shapes.length),
300
+ );
301
+ })();
302
+
303
+ /* ---------- Ramps ---------- */
304
+
305
+ export type RampOptions = {
306
+ /** Which step the seed is: 0 for 50 … 10 for 950; 5, the 500, unless given. */
307
+ step?: number;
308
+ gamut?: Gamut;
309
+ shape?: RampShape;
310
+ };
311
+
312
+ const finite = (value: number | undefined, fallback: number) =>
313
+ typeof value === 'number' && Number.isFinite(value) ? value : fallback;
314
+
315
+ /** A step's place between an end (0) and the seed (1), bent: midtones above 0 lift it towards the light end. */
316
+ const bend = (t: number, midtones: number) => Math.min(1, Math.max(0, t)) ** (2 ** midtones);
317
+
318
+ /** How far the light or dark end's extra turn reaches a step: 0 at the seed, 1 at the end. */
319
+ const reach = (i: number, s: number) =>
320
+ i < s ? (s - i) / Math.max(1, s) : i > s ? (i - s) / Math.max(1, 10 - s) : 0;
321
+
322
+ /** A step's chroma within what its lightness and hue allow in the gamut. */
323
+ const fit = (color: Oklch, gamut: Gamut): Oklch => ({
324
+ ...color,
325
+ c: Math.max(0, Math.min(color.c, maxChroma(color.l, color.h, gamut))),
326
+ });
327
+
328
+ const isSet = (value: number | undefined): value is number =>
329
+ typeof value === 'number' && Number.isFinite(value);
330
+
331
+ /** True when a shape sets any step by hand. */
332
+ export const hasRampSteps = (shape?: RampShape) =>
333
+ Boolean(
334
+ shape?.steps &&
335
+ Object.values(shape.steps).some(
336
+ step => step && (isSet(step.l) || isSet(step.c) || isSet(step.h)),
337
+ ),
338
+ );
339
+
340
+ /**
341
+ * The steps set by hand laid over a built ramp, each channel clamped: lightness 0–1, hue round the
342
+ * wheel, chroma within the gamut. A chroma not set keeps the step's share of what the gamut allows,
343
+ * so moving a step's lightness or hue never pushes it out of the gamut or greys it. `skip` is a step
344
+ * the overrides never touch: a primary's seed.
345
+ */
346
+ export const applyRampSteps = (
347
+ ramp: readonly Oklch[],
348
+ steps: RampShape['steps'],
349
+ gamut: Gamut,
350
+ skip?: number,
351
+ ): Oklch[] =>
352
+ ramp.map((color, i) => {
353
+ const override = steps?.[rampSteps[i] as RampStep];
354
+ if (i === skip || !override) return color;
355
+ const l = isSet(override.l) ? Math.min(1, Math.max(0, override.l)) : color.l;
356
+ const h = isSet(override.h) ? ((override.h % 360) + 360) % 360 : color.h;
357
+ let c = color.c;
358
+ if (isSet(override.c)) c = Math.max(0, override.c);
359
+ else if (l !== color.l || h !== color.h) {
360
+ const room = maxChroma(color.l, color.h, gamut);
361
+ c = room > 1e-6 ? (color.c / room) * maxChroma(l, h, gamut) : color.c;
362
+ }
363
+ return fit({ l, c, h }, gamut);
364
+ });
365
+
366
+ /**
367
+ * A ramp of one hue around a seed, as Tailwind would draw it: the seed exactly at its step, the ends
368
+ * fixed, the middle placed by the hue, the chroma a share of the gamut, the hue drifting as Tailwind's
369
+ * does at the ends. Steps set by hand in the shape's `steps` replace the generated values; a chroma
370
+ * left alone follows a lightness or hue set by hand, so the step keeps its share of the gamut.
371
+ */
372
+ export const buildRamp = (seed: Oklch, { step = 5, gamut = 'p3', shape }: RampOptions = {}) =>
373
+ buildRampFrom(seed, paletteProfile(seed.h), { step, gamut, shape });
374
+
375
+ /** The ramp from a given profile; `buildRamp` takes it from the seed's hue. */
376
+ export const buildRampFrom = (
377
+ seed: Oklch,
378
+ profile: ReturnType<typeof paletteProfile>,
379
+ { step = 5, gamut = 'p3', shape = {} }: RampOptions = {},
380
+ ): Oklch[] => {
381
+ const s = Math.min(10, Math.max(0, Math.round(step)));
382
+ const share = profile.share[gamut];
383
+ const peakShare = Math.max(...share);
384
+ const taper = finite(shape.taper, neutralShape.taper);
385
+ // The ends fade from the middle's share by Tailwind's own amount, times the taper.
386
+ const shareAt = (i: number) => peakShare - (peakShare - (share[i] ?? peakShare)) * taper;
387
+ const top = finite(shape.lightest, profile.l[0] ?? 0.977);
388
+ const bottom = finite(shape.darkest, profile.l[10] ?? 0.278);
389
+ const midtones = finite(shape.midtones, neutralShape.midtones);
390
+ const saturation = finite(shape.saturation, neutralShape.saturation);
391
+ const drift = finite(shape.drift, neutralShape.drift);
392
+ const lightHue = finite(shape.lightHue, neutralShape.lightHue);
393
+ const darkHue = finite(shape.darkHue, neutralShape.darkHue);
394
+ const profileTop = profile.l[0] ?? 0.977;
395
+ const profileBottom = profile.l[10] ?? 0.278;
396
+ const at = profile.l[s] ?? seed.l;
397
+ // A seed beyond an end leaves no room for the steps on that side; it moves the end instead.
398
+ const first = Math.max(top, seed.l + 0.01);
399
+ const last = Math.min(bottom, seed.l - 0.01);
400
+ const seedColor = fit(seed, gamut);
401
+ // How much of its gamut the seed uses, against how much Tailwind's would at that step.
402
+ const strength =
403
+ seedColor.c / Math.max(1e-6, maxChroma(seed.l, seed.h, gamut)) / Math.max(1e-6, shareAt(s));
404
+
405
+ const curves = rampSteps.map((_, i) => {
406
+ if (i === s) return seedColor;
407
+ const base = profile.l[i] ?? at;
408
+ // Tailwind's own spacing on each side of the seed, stretched from the end to the seed and bent.
409
+ const l =
410
+ i < s
411
+ ? first -
412
+ (first - seed.l) *
413
+ bend((profileTop - base) / Math.max(1e-6, profileTop - at), midtones)
414
+ : seed.l -
415
+ (seed.l - last) *
416
+ (1 - bend(1 - (at - base) / Math.max(1e-6, at - profileBottom), -midtones));
417
+ const h =
418
+ (seed.h +
419
+ ((profile.drift[i] ?? 0) - (profile.drift[s] ?? 0)) * drift +
420
+ (i < s ? lightHue : darkHue) * reach(i, s) +
421
+ 360) %
422
+ 360;
423
+ const c = strength * saturation * shareAt(i) * maxChroma(l, h, gamut);
424
+ return fit({ l, c, h }, gamut);
425
+ });
426
+ return shape.steps ? applyRampSteps(curves, shape.steps, gamut, s) : curves;
427
+ };
428
+
429
+ /**
430
+ * A grey ramp: the given lightness at every step, stretched to the shape's ends and bent by its
431
+ * midtones; one hue, turned at the ends by the shape; and a faint chroma, `tint` at the 500, that
432
+ * rises to it from the 50 as Tailwind's tinted greys do and holds from it into the 950. The taper
433
+ * is how much the lighter steps fade: none leaves every step at the tint, twice fades them to
434
+ * nothing sooner.
435
+ */
436
+ export const buildGreyRamp = (
437
+ hue: number,
438
+ tint: number,
439
+ lightness: readonly number[],
440
+ { gamut = 'p3', shape = {} }: Omit<RampOptions, 'step'> = {},
441
+ ): Oklch[] => {
442
+ const baseTop = lightness[0] ?? 0.98;
443
+ const baseBottom = lightness[10] ?? 0.15;
444
+ const top = finite(shape.lightest, baseTop);
445
+ const bottom = finite(shape.darkest, baseBottom);
446
+ const midtones = finite(shape.midtones, neutralShape.midtones);
447
+ const taper = finite(shape.taper, neutralShape.taper);
448
+ const saturation = finite(shape.saturation, neutralShape.saturation);
449
+ const lightHue = finite(shape.lightHue, neutralShape.lightHue);
450
+ const darkHue = finite(shape.darkHue, neutralShape.darkHue);
451
+ const curves = rampSteps.map((_, i) => {
452
+ // Where the step sits between the lightest and the darkest, bent as the midtones ask.
453
+ const t = (baseTop - (lightness[i] ?? 0.5)) / Math.max(1e-6, baseTop - baseBottom);
454
+ const l = top - (top - bottom) * bend(t, midtones);
455
+ const h = (hue + (i < 5 ? lightHue : darkHue) * reach(i, 5) + 360) % 360;
456
+ // Never under none: a taper past one would otherwise turn the palest steps to the opposite hue.
457
+ const c = tint * saturation * Math.max(0, 1 - (1 - (greyShape[i] ?? 0)) * taper);
458
+ return fit({ l, c, h }, gamut);
459
+ });
460
+ return shape.steps ? applyRampSteps(curves, shape.steps, gamut) : curves;
461
+ };
462
+
463
+ /** A ramp as stylesheet values, 50 to 950. */
464
+ export const rampToCss = (ramp: readonly Oklch[], gamut: Gamut) =>
465
+ ramp.map(color => toCss(color, gamut)) as RampSteps;
466
+
467
+ /** The lightness of every step of a ramp written as hex, for shaping greys on. */
468
+ export const rampLightness = (reference: readonly string[]) =>
469
+ reference.map(hex => hexToOklch(hex)?.l ?? 0.5);
@@ -0,0 +1,220 @@
1
+ import { encodeSrgb, linearSrgbToOklch, type Oklch, oklchToLinearSrgb } from './color';
2
+
3
+ /*
4
+ * How a theme's colours read: WCAG 2's contrast ratio, APCA's lightness contrast, how a colour looks
5
+ * to someone with a colour-vision deficiency, and how far apart two colours look. Colours are oklch
6
+ * throughout, so a P3 colour's luminance is measured as it is shown on a P3 screen; APCA and the
7
+ * simulations work on sRGB channels, so they take a colour outside sRGB clipped to it, as an sRGB
8
+ * screen shows it.
9
+ */
10
+
11
+ const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
12
+
13
+ const linear = (color: Oklch) => oklchToLinearSrgb(color);
14
+
15
+ const clipped = (color: Oklch) => linear(color).map(clamp01) as [number, number, number];
16
+
17
+ /** WCAG 2's relative luminance, 0 (black) to 1 (white). */
18
+ export const luminance = (color: Oklch) => {
19
+ const [r, g, b] = linear(color);
20
+ return clamp01(0.2126 * r + 0.7152 * g + 0.0722 * b);
21
+ };
22
+
23
+ /** WCAG 2's contrast ratio, 1 to 21: 4.5 for text, 3 for large text and the parts of controls. */
24
+ export const wcagContrast = (a: Oklch, b: Oklch) => {
25
+ const [high, low] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [number, number];
26
+ return (high + 0.05) / (low + 0.05);
27
+ };
28
+
29
+ /*
30
+ * APCA, the lightness contrast proposed for WCAG 3: APCA-W3 0.0.98G-4g, after Myndex's apca-w3 and
31
+ * Color.js. It tells text from background (dark on light reads differently from light on dark) and
32
+ * gives Lc, about 0 to 106 for dark text on light and 0 to -108 for light on dark.
33
+ */
34
+ const apcaY = (color: Oklch) => {
35
+ const [r, g, b] = clipped(color).map(channel => encodeSrgb(channel) ** 2.4) as [
36
+ number,
37
+ number,
38
+ number,
39
+ ];
40
+ const y = 0.2126729 * r + 0.7151522 * g + 0.072175 * b;
41
+ // Near black, the screen's flare lifts what the eye sees. 1.414 is APCA's clamp exponent, not √2.
42
+ // biome-ignore lint/suspicious/noApproximativeNumericConstant: APCA's own constant, kept to its spec.
43
+ return y >= 0.022 ? y : y + (0.022 - y) ** 1.414;
44
+ };
45
+
46
+ /** APCA's Lc for text on a background; negative when the text is the lighter. Compare its size. */
47
+ export const apcaContrast = (text: Oklch, background: Oklch) => {
48
+ const t = apcaY(text);
49
+ const b = apcaY(background);
50
+ if (Math.abs(b - t) < 0.0005) return 0;
51
+ const c = b > t ? (b ** 0.56 - t ** 0.57) * 1.14 : (b ** 0.65 - t ** 0.62) * 1.14;
52
+ if (Math.abs(c) < 0.1) return 0;
53
+ return (c > 0 ? c - 0.027 : c + 0.027) * 100;
54
+ };
55
+
56
+ /* ---------- Colour vision ---------- */
57
+
58
+ /** How a colour is seen: with full colour vision, or with no red, green or blue cones. */
59
+ export type Vision = 'normal' | 'protan' | 'deutan' | 'tritan';
60
+
61
+ /** Machado, Oliveira and Fernandes (2009) at full severity, on linear sRGB. */
62
+ const machado: Record<Exclude<Vision, 'normal'>, readonly (readonly number[])[]> = {
63
+ protan: [
64
+ [0.152286, 1.052583, -0.204868],
65
+ [0.114503, 0.786281, 0.099216],
66
+ [-0.003882, -0.048116, 1.051998],
67
+ ],
68
+ deutan: [
69
+ [0.367322, 0.860646, -0.227968],
70
+ [0.280085, 0.672501, 0.047413],
71
+ [-0.01182, 0.04294, 0.968881],
72
+ ],
73
+ tritan: [
74
+ [1.255528, -0.076749, -0.178779],
75
+ [-0.078411, 0.930809, 0.147602],
76
+ [0.004733, 0.691367, 0.3039],
77
+ ],
78
+ };
79
+
80
+ /** A colour as someone with the given colour vision sees it. */
81
+ export const simulateVision = (color: Oklch, vision: Vision): Oklch => {
82
+ if (vision === 'normal') return color;
83
+ const [r, g, b] = clipped(color);
84
+ const m = machado[vision];
85
+ const row = (i: number) =>
86
+ clamp01((m[i]?.[0] ?? 0) * r + (m[i]?.[1] ?? 0) * g + (m[i]?.[2] ?? 0) * b);
87
+ return linearSrgbToOklch([row(0), row(1), row(2)]);
88
+ };
89
+
90
+ const lab = ({ l, c, h }: Oklch) => {
91
+ const angle = (h * Math.PI) / 180;
92
+ return [l, c * Math.cos(angle), c * Math.sin(angle)] as const;
93
+ };
94
+
95
+ /** How far apart two colours look: the OKLab distance times 100, as seen with the given vision. */
96
+ export const deltaE = (a: Oklch, b: Oklch, vision: Vision = 'normal') => {
97
+ const [l1, a1, b1] = lab(simulateVision(a, vision));
98
+ const [l2, a2, b2] = lab(simulateVision(b, vision));
99
+ return 100 * Math.hypot(l1 - l2, a1 - a2, b1 - b2);
100
+ };
101
+
102
+ /* ---------- Data colours ---------- */
103
+
104
+ /**
105
+ * What a set of series colours must clear to tell series apart, in every reader's eyes: a lightness
106
+ * band per mode, so none sinks into the surface or glares off it; a chroma floor, below which a
107
+ * hue reads as grey; a distance between neighbours under protan and deutan vision (8 wanted, 6
108
+ * only with labels or texture beside it) and with full colour vision (15); 3:1 against the surface.
109
+ */
110
+ export const seriesThresholds = {
111
+ band: { light: [0.43, 0.77], dark: [0.48, 0.67] },
112
+ chroma: 0.1,
113
+ cvd: { target: 8, floor: 6 },
114
+ normal: 15,
115
+ contrast: 3,
116
+ } as const;
117
+
118
+ export type SeriesCheckId = 'band' | 'chroma' | 'cvd' | 'normal' | 'contrast';
119
+
120
+ export type SeriesCheck = {
121
+ id: SeriesCheckId;
122
+ /** `warn` holds only with labels, a table or texture beside the colours. */
123
+ state: 'pass' | 'warn' | 'fail';
124
+ /** The measure: the worst value found. */
125
+ value: number;
126
+ /** The series it is about, by position: the pair for a distance, the ones that miss otherwise. */
127
+ series: number[];
128
+ /** For a distance, the vision it was worst under. */
129
+ vision?: Vision;
130
+ };
131
+
132
+ /**
133
+ * The measurable checks on a set of series colours against the chart's surface: neighbours only
134
+ * (stacks, bars, lines), or every pair (scatter, maps, small multiples).
135
+ */
136
+ export const checkSeries = (
137
+ colors: readonly Oklch[],
138
+ {
139
+ mode,
140
+ surface,
141
+ pairs = 'adjacent',
142
+ }: { mode: 'light' | 'dark'; surface: Oklch; pairs?: 'adjacent' | 'all' },
143
+ ): SeriesCheck[] => {
144
+ const [low, high] = seriesThresholds.band[mode];
145
+ const index = colors.map((_, i) => i);
146
+ const list =
147
+ pairs === 'all'
148
+ ? index.flatMap(i => index.filter(j => j > i).map(j => [i, j] as const))
149
+ : index.slice(1).map(i => [i - 1, i] as const);
150
+
151
+ const off = index.filter(i => {
152
+ const l = colors[i]?.l ?? 0;
153
+ return l < low || l > high;
154
+ });
155
+ const bandValue = Math.max(
156
+ 0,
157
+ ...colors.map(color => Math.max(low - color.l, color.l - high, 0)),
158
+ );
159
+
160
+ const grey = index.filter(i => (colors[i]?.c ?? 0) < seriesThresholds.chroma);
161
+
162
+ const worstPair = (visions: Vision[]) => {
163
+ let worst = {
164
+ value: Number.POSITIVE_INFINITY,
165
+ pair: [] as number[],
166
+ vision: 'normal' as Vision,
167
+ };
168
+ for (const vision of visions) {
169
+ for (const [i, j] of list) {
170
+ const a = colors[i];
171
+ const b = colors[j];
172
+ if (!a || !b) continue;
173
+ const value = deltaE(a, b, vision);
174
+ if (value < worst.value) worst = { value, pair: [i, j], vision };
175
+ }
176
+ }
177
+ return worst;
178
+ };
179
+ const cvd = worstPair(['protan', 'deutan']);
180
+ const normal = worstPair(['normal']);
181
+
182
+ const ratios = colors.map(color => wcagContrast(color, surface));
183
+ const faint = index.filter(i => (ratios[i] ?? 0) < seriesThresholds.contrast);
184
+
185
+ const hasPairs = list.length > 0;
186
+ return [
187
+ { id: 'band', state: off.length ? 'fail' : 'pass', value: bandValue, series: off },
188
+ {
189
+ id: 'chroma',
190
+ state: grey.length ? 'fail' : 'pass',
191
+ value: Math.min(...colors.map(color => color.c)),
192
+ series: grey,
193
+ },
194
+ {
195
+ id: 'cvd',
196
+ state:
197
+ !hasPairs || cvd.value >= seriesThresholds.cvd.target
198
+ ? 'pass'
199
+ : cvd.value >= seriesThresholds.cvd.floor
200
+ ? 'warn'
201
+ : 'fail',
202
+ value: hasPairs ? cvd.value : 0,
203
+ series: cvd.pair,
204
+ vision: cvd.vision,
205
+ },
206
+ {
207
+ id: 'normal',
208
+ state: !hasPairs || normal.value >= seriesThresholds.normal ? 'pass' : 'fail',
209
+ value: hasPairs ? normal.value : 0,
210
+ series: normal.pair,
211
+ vision: 'normal',
212
+ },
213
+ {
214
+ id: 'contrast',
215
+ state: faint.length ? 'warn' : 'pass',
216
+ value: Math.min(...ratios),
217
+ series: faint,
218
+ },
219
+ ];
220
+ };