@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.
- package/LICENSE +30 -0
- package/README.md +150 -2
- package/cli/contui.mjs +250 -0
- package/dist/charts/area-chart.d.ts +8 -0
- package/dist/charts/area-chart.js +9 -0
- package/dist/charts/bar-chart.d.ts +9 -0
- package/dist/charts/bar-chart.js +10 -0
- package/dist/charts/bubble-chart.d.ts +60 -0
- package/dist/charts/bubble-chart.js +392 -0
- package/dist/charts/bullet-chart.d.ts +53 -0
- package/dist/charts/bullet-chart.js +123 -0
- package/dist/charts/candlestick-chart.d.ts +77 -0
- package/dist/charts/candlestick-chart.js +558 -0
- package/dist/charts/cartesian.d.ts +121 -0
- package/dist/charts/cartesian.js +625 -0
- package/dist/charts/chart-card.d.ts +80 -0
- package/dist/charts/chart-card.js +114 -0
- package/dist/charts/donut-chart.d.ts +40 -0
- package/dist/charts/donut-chart.js +88 -0
- package/dist/charts/funnel-chart.d.ts +16 -0
- package/dist/charts/funnel-chart.js +164 -0
- package/dist/charts/horizontal-bar-chart.d.ts +67 -0
- package/dist/charts/horizontal-bar-chart.js +481 -0
- package/dist/charts/index.d.ts +21 -0
- package/dist/charts/index.js +32 -0
- package/dist/charts/legend.d.ts +23 -0
- package/dist/charts/legend.js +22 -0
- package/dist/charts/lib/candlestick-window.d.ts +15 -0
- package/dist/charts/lib/candlestick-window.js +23 -0
- package/dist/charts/lib/color.d.ts +39 -0
- package/dist/charts/lib/color.js +83 -0
- package/dist/charts/lib/format.d.ts +11 -0
- package/dist/charts/lib/format.js +33 -0
- package/dist/charts/lib/frame.d.ts +6 -0
- package/dist/charts/lib/frame.js +6 -0
- package/dist/charts/lib/layout.d.ts +11 -0
- package/dist/charts/lib/layout.js +71 -0
- package/dist/charts/lib/path.d.ts +35 -0
- package/dist/charts/lib/path.js +203 -0
- package/dist/charts/lib/scale.d.ts +41 -0
- package/dist/charts/lib/scale.js +80 -0
- package/dist/charts/lib/use-active.d.ts +18 -0
- package/dist/charts/lib/use-active.js +71 -0
- package/dist/charts/lib/use-size.d.ts +9 -0
- package/dist/charts/lib/use-size.js +40 -0
- package/dist/charts/line-chart.d.ts +7 -0
- package/dist/charts/line-chart.js +8 -0
- package/dist/charts/radar-chart.d.ts +27 -0
- package/dist/charts/radar-chart.js +165 -0
- package/dist/charts/rose-chart.d.ts +35 -0
- package/dist/charts/rose-chart.js +242 -0
- package/dist/charts/sparkline.d.ts +35 -0
- package/dist/charts/sparkline.js +110 -0
- package/dist/charts/tooltip.d.ts +35 -0
- package/dist/charts/tooltip.js +22 -0
- package/dist/charts/treemap.d.ts +28 -0
- package/dist/charts/treemap.js +103 -0
- package/dist/charts/uptime-strip.d.ts +40 -0
- package/dist/charts/uptime-strip.js +178 -0
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.js +34 -0
- package/dist/components/activity-item.d.ts +12 -0
- package/dist/components/activity-item.js +6 -0
- package/dist/components/alert.d.ts +21 -0
- package/dist/components/alert.js +58 -0
- package/dist/components/assistant-answer.d.ts +52 -0
- package/dist/components/assistant-answer.js +40 -0
- package/dist/components/assistant-prompt.d.ts +63 -0
- package/dist/components/assistant-prompt.js +122 -0
- package/dist/components/avatar.d.ts +28 -0
- package/dist/components/avatar.js +75 -0
- package/dist/components/badge.d.ts +16 -0
- package/dist/components/badge.js +33 -0
- package/dist/components/bar-list.d.ts +65 -0
- package/dist/components/bar-list.js +119 -0
- package/dist/components/block-slider.d.ts +43 -0
- package/dist/components/block-slider.js +63 -0
- package/dist/components/branch-graph.d.ts +87 -0
- package/dist/components/branch-graph.js +231 -0
- package/dist/components/breadcrumb.d.ts +41 -0
- package/dist/components/breadcrumb.js +53 -0
- package/dist/components/button.d.ts +23 -0
- package/dist/components/button.js +108 -0
- package/dist/components/calendar.d.ts +5 -0
- package/dist/components/calendar.js +90 -0
- package/dist/components/card.d.ts +65 -0
- package/dist/components/card.js +104 -0
- package/dist/components/chat-attachment.d.ts +47 -0
- package/dist/components/chat-attachment.js +63 -0
- package/dist/components/chat-event.d.ts +7 -0
- package/dist/components/chat-event.js +7 -0
- package/dist/components/chat-message.d.ts +59 -0
- package/dist/components/chat-message.js +91 -0
- package/dist/components/chat-reference.d.ts +20 -0
- package/dist/components/chat-reference.js +41 -0
- package/dist/components/chat-shared.d.ts +21 -0
- package/dist/components/chat-shared.js +23 -0
- package/dist/components/chat-suggestions.d.ts +59 -0
- package/dist/components/chat-suggestions.js +88 -0
- package/dist/components/chat-thread.d.ts +39 -0
- package/dist/components/chat-thread.js +172 -0
- package/dist/components/chat-typing.d.ts +35 -0
- package/dist/components/chat-typing.js +66 -0
- package/dist/components/checkbox.d.ts +18 -0
- package/dist/components/checkbox.js +33 -0
- package/dist/components/checklist.d.ts +26 -0
- package/dist/components/checklist.js +18 -0
- package/dist/components/chip.d.ts +29 -0
- package/dist/components/chip.js +61 -0
- package/dist/components/code-input.d.ts +40 -0
- package/dist/components/code-input.js +285 -0
- package/dist/components/color-picker.d.ts +38 -0
- package/dist/components/color-picker.js +855 -0
- package/dist/components/combobox.d.ts +75 -0
- package/dist/components/combobox.js +112 -0
- package/dist/components/command-menu.d.ts +58 -0
- package/dist/components/command-menu.js +166 -0
- package/dist/components/copy-button.d.ts +16 -0
- package/dist/components/copy-button.js +31 -0
- package/dist/components/data-grid.d.ts +50 -0
- package/dist/components/data-grid.js +599 -0
- package/dist/components/date-picker.d.ts +109 -0
- package/dist/components/date-picker.js +268 -0
- package/dist/components/description-list.d.ts +12 -0
- package/dist/components/description-list.js +10 -0
- package/dist/components/dialog.d.ts +57 -0
- package/dist/components/dialog.js +140 -0
- package/dist/components/dock.d.ts +45 -0
- package/dist/components/dock.js +79 -0
- package/dist/components/drop-zone.d.ts +46 -0
- package/dist/components/drop-zone.js +101 -0
- package/dist/components/dropdown-menu.d.ts +50 -0
- package/dist/components/dropdown-menu.js +78 -0
- package/dist/components/editable-cell.d.ts +34 -0
- package/dist/components/editable-cell.js +89 -0
- package/dist/components/empty-state.d.ts +17 -0
- package/dist/components/empty-state.js +9 -0
- package/dist/components/field.d.ts +43 -0
- package/dist/components/field.js +75 -0
- package/dist/components/filter-condition.d.ts +188 -0
- package/dist/components/filter-condition.js +113 -0
- package/dist/components/filter-menu.d.ts +57 -0
- package/dist/components/filter-menu.js +46 -0
- package/dist/components/header-bar.d.ts +84 -0
- package/dist/components/header-bar.js +94 -0
- package/dist/components/hold-button.d.ts +15 -0
- package/dist/components/hold-button.js +88 -0
- package/dist/components/icon-tile.d.ts +12 -0
- package/dist/components/icon-tile.js +29 -0
- package/dist/components/info-tip.d.ts +13 -0
- package/dist/components/info-tip.js +26 -0
- package/dist/components/input.d.ts +17 -0
- package/dist/components/input.js +83 -0
- package/dist/components/kbd.d.ts +6 -0
- package/dist/components/kbd.js +7 -0
- package/dist/components/menu-item.d.ts +56 -0
- package/dist/components/menu-item.js +94 -0
- package/dist/components/message-composer.d.ts +75 -0
- package/dist/components/message-composer.js +199 -0
- package/dist/components/nav-rail.d.ts +31 -0
- package/dist/components/nav-rail.js +85 -0
- package/dist/components/notice.d.ts +45 -0
- package/dist/components/notice.js +66 -0
- package/dist/components/optimistic-undo.d.ts +28 -0
- package/dist/components/optimistic-undo.js +43 -0
- package/dist/components/option-row.d.ts +15 -0
- package/dist/components/option-row.js +14 -0
- package/dist/components/page-content.d.ts +11 -0
- package/dist/components/page-content.js +31 -0
- package/dist/components/pagination.d.ts +34 -0
- package/dist/components/pagination.js +41 -0
- package/dist/components/popover.d.ts +27 -0
- package/dist/components/popover.js +25 -0
- package/dist/components/progress.d.ts +33 -0
- package/dist/components/progress.js +75 -0
- package/dist/components/radio-group.d.ts +7 -0
- package/dist/components/radio-group.js +33 -0
- package/dist/components/readonly-field.d.ts +33 -0
- package/dist/components/readonly-field.js +80 -0
- package/dist/components/segmented-control.d.ts +21 -0
- package/dist/components/segmented-control.js +108 -0
- package/dist/components/select.d.ts +42 -0
- package/dist/components/select.js +86 -0
- package/dist/components/separator.d.ts +10 -0
- package/dist/components/separator.js +7 -0
- package/dist/components/setting-row.d.ts +53 -0
- package/dist/components/setting-row.js +93 -0
- package/dist/components/sidebar-switcher.d.ts +47 -0
- package/dist/components/sidebar-switcher.js +20 -0
- package/dist/components/sidebar.d.ts +220 -0
- package/dist/components/sidebar.js +327 -0
- package/dist/components/skeleton.d.ts +18 -0
- package/dist/components/skeleton.js +23 -0
- package/dist/components/slider.d.ts +15 -0
- package/dist/components/slider.js +28 -0
- package/dist/components/spinner.d.ts +14 -0
- package/dist/components/spinner.js +19 -0
- package/dist/components/status-dot.d.ts +24 -0
- package/dist/components/status-dot.js +35 -0
- package/dist/components/stepper.d.ts +60 -0
- package/dist/components/stepper.js +197 -0
- package/dist/components/switch.d.ts +9 -0
- package/dist/components/switch.js +26 -0
- package/dist/components/table-cell.d.ts +38 -0
- package/dist/components/table-cell.js +107 -0
- package/dist/components/table-context.d.ts +48 -0
- package/dist/components/table-context.js +69 -0
- package/dist/components/table-head.d.ts +42 -0
- package/dist/components/table-head.js +188 -0
- package/dist/components/table-tree.d.ts +96 -0
- package/dist/components/table-tree.js +137 -0
- package/dist/components/table.d.ts +78 -0
- package/dist/components/table.js +119 -0
- package/dist/components/tabs.d.ts +27 -0
- package/dist/components/tabs.js +79 -0
- package/dist/components/textarea.d.ts +3 -0
- package/dist/components/textarea.js +20 -0
- package/dist/components/theme.d.ts +41 -0
- package/dist/components/theme.js +60 -0
- package/dist/components/thinking.d.ts +68 -0
- package/dist/components/thinking.js +94 -0
- package/dist/components/toast.d.ts +39 -0
- package/dist/components/toast.js +110 -0
- package/dist/components/toolbar.d.ts +25 -0
- package/dist/components/toolbar.js +92 -0
- package/dist/components/tooltip.d.ts +23 -0
- package/dist/components/tooltip.js +27 -0
- package/dist/components/top-nav.d.ts +129 -0
- package/dist/components/top-nav.js +211 -0
- package/dist/components/truncate.d.ts +29 -0
- package/dist/components/truncate.js +138 -0
- package/dist/deprecated.d.ts +43 -0
- package/dist/deprecated.js +47 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +93 -0
- package/dist/lib/cn.d.ts +5 -0
- package/dist/lib/cn.js +14 -0
- package/dist/lib/color.d.ts +136 -0
- package/dist/lib/color.js +307 -0
- package/dist/lib/contrast.d.ts +53 -0
- package/dist/lib/contrast.js +169 -0
- package/dist/lib/create-theme.d.ts +240 -0
- package/dist/lib/create-theme.js +461 -0
- package/dist/lib/harmony.d.ts +102 -0
- package/dist/lib/harmony.js +426 -0
- package/dist/lib/import-theme.d.ts +22 -0
- package/dist/lib/import-theme.js +221 -0
- package/dist/lib/overflow-scroll.d.ts +25 -0
- package/dist/lib/overflow-scroll.js +66 -0
- package/dist/lib/radio-toggle-render.d.ts +3 -0
- package/dist/lib/radio-toggle-render.js +9 -0
- package/dist/lib/reorder.d.ts +38 -0
- package/dist/lib/reorder.js +193 -0
- package/dist/lib/spacing.d.ts +101 -0
- package/dist/lib/spacing.js +118 -0
- package/dist/lib/tailwind-palette.d.ts +5 -0
- package/dist/lib/tailwind-palette.js +351 -0
- package/dist/lib/truncate.d.ts +18 -0
- package/dist/lib/truncate.js +69 -0
- package/dist/lib/use-single-value.d.ts +5 -0
- package/dist/lib/use-single-value.js +17 -0
- package/dist/styles.css +7988 -0
- package/mcp/server.mjs +87590 -0
- package/package.json +58 -5
- package/skills/continuum-fix-padding/SKILL.md +69 -0
- package/skills/continuum-fix-padding/scripts/compute.ts +155 -0
- package/skills/continuum-fix-padding/scripts/spacing.ts +189 -0
- package/skills/continuum-taste/SKILL.md +186 -0
- package/skills/continuum-taste/assets/tokens.css +647 -0
- package/skills/continuum-taste/references/charts.md +375 -0
- package/skills/continuum-taste/references/components.md +1369 -0
- package/skills/continuum-taste/references/foundations.md +539 -0
- package/skills/continuum-taste/references/media/canvas.md +75 -0
- package/skills/continuum-taste/references/media/css.md +61 -0
- package/skills/continuum-taste/references/media/react.md +161 -0
- package/skills/continuum-taste/references/method.md +87 -0
- package/skills/continuum-taste/references/pages.md +391 -0
- package/skills/continuum-taste/references/patterns.md +77 -0
- package/skills/continuum-taste/references/screens/agent.tsx +450 -0
- package/skills/continuum-taste/references/screens/analytics-overview.tsx +377 -0
- package/skills/continuum-taste/references/screens/checkout-funnel.tsx +193 -0
- package/skills/continuum-taste/references/screens/customer.tsx +475 -0
- package/skills/continuum-taste/references/screens/customers.tsx +608 -0
- package/skills/continuum-taste/references/screens/index.tsx +72 -0
- package/skills/continuum-taste/references/screens/invoice-editor.tsx +558 -0
- package/skills/continuum-taste/references/screens/new-customer.tsx +205 -0
- package/skills/continuum-taste/references/screens/overview.tsx +196 -0
- package/skills/continuum-taste/references/screens/report-generator.tsx +329 -0
- package/skills/continuum-taste/references/screens/risk-report.tsx +577 -0
- package/skills/continuum-taste/references/screens/sequence-editor.tsx +455 -0
- package/skills/continuum-taste/references/screens/settings.tsx +288 -0
- package/skills/continuum-taste/references/screens/shared.tsx +204 -0
- package/skills/continuum-taste/references/screens/sign-in.tsx +104 -0
- package/skills/continuum-taste/references/screens/workspace.tsx +596 -0
- package/skills/continuum-taste/references/theming.md +95 -0
- package/src/charts/area-chart.tsx +17 -0
- package/src/charts/bar-chart.tsx +15 -0
- package/src/charts/bubble-chart.tsx +913 -0
- package/src/charts/bullet-chart.tsx +419 -0
- package/src/charts/candlestick-chart.tsx +1242 -0
- package/src/charts/cartesian.tsx +1437 -0
- package/src/charts/chart-card.tsx +394 -0
- package/src/charts/donut-chart.tsx +299 -0
- package/src/charts/funnel-chart.tsx +373 -0
- package/src/charts/horizontal-bar-chart.tsx +991 -0
- package/src/charts/index.ts +76 -0
- package/src/charts/legend.tsx +59 -0
- package/src/charts/lib/candlestick-window.ts +42 -0
- package/src/charts/lib/color.ts +110 -0
- package/src/charts/lib/format.ts +42 -0
- package/src/charts/lib/frame.ts +6 -0
- package/src/charts/lib/layout.ts +78 -0
- package/src/charts/lib/path.ts +225 -0
- package/src/charts/lib/scale.ts +91 -0
- package/src/charts/lib/use-active.ts +87 -0
- package/src/charts/lib/use-size.ts +45 -0
- package/src/charts/line-chart.tsx +16 -0
- package/src/charts/radar-chart.tsx +485 -0
- package/src/charts/rose-chart.tsx +553 -0
- package/src/charts/sparkline.tsx +293 -0
- package/src/charts/tooltip.tsx +105 -0
- package/src/charts/treemap.tsx +271 -0
- package/src/charts/uptime-strip.tsx +469 -0
- package/src/components/accordion.tsx +136 -0
- package/src/components/activity-item.tsx +43 -0
- package/src/components/alert.tsx +135 -0
- package/src/components/assistant-answer.tsx +185 -0
- package/src/components/assistant-prompt.tsx +352 -0
- package/src/components/avatar.tsx +157 -0
- package/src/components/badge.tsx +55 -0
- package/src/components/bar-list.tsx +350 -0
- package/src/components/block-slider.tsx +189 -0
- package/src/components/branch-graph.tsx +580 -0
- package/src/components/breadcrumb.tsx +173 -0
- package/src/components/button.tsx +157 -0
- package/src/components/calendar.tsx +154 -0
- package/src/components/card.tsx +296 -0
- package/src/components/chat-attachment.tsx +255 -0
- package/src/components/chat-event.tsx +34 -0
- package/src/components/chat-message.tsx +332 -0
- package/src/components/chat-reference.tsx +100 -0
- package/src/components/chat-shared.ts +41 -0
- package/src/components/chat-suggestions.tsx +188 -0
- package/src/components/chat-thread.tsx +348 -0
- package/src/components/chat-typing.tsx +155 -0
- package/src/components/checkbox.tsx +83 -0
- package/src/components/checklist.tsx +146 -0
- package/src/components/chip.tsx +152 -0
- package/src/components/code-input.tsx +406 -0
- package/src/components/color-picker.tsx +1385 -0
- package/src/components/combobox.tsx +420 -0
- package/src/components/command-menu.tsx +405 -0
- package/src/components/copy-button.tsx +87 -0
- package/src/components/data-grid.tsx +965 -0
- package/src/components/date-picker.tsx +652 -0
- package/src/components/description-list.tsx +39 -0
- package/src/components/dialog.tsx +367 -0
- package/src/components/dock.tsx +192 -0
- package/src/components/drop-zone.tsx +283 -0
- package/src/components/dropdown-menu.tsx +323 -0
- package/src/components/editable-cell.tsx +219 -0
- package/src/components/empty-state.tsx +50 -0
- package/src/components/field.tsx +148 -0
- package/src/components/filter-condition.tsx +463 -0
- package/src/components/filter-menu.tsx +196 -0
- package/src/components/header-bar.tsx +220 -0
- package/src/components/hold-button.tsx +146 -0
- package/src/components/icon-tile.tsx +43 -0
- package/src/components/info-tip.tsx +87 -0
- package/src/components/input.tsx +136 -0
- package/src/components/kbd.tsx +17 -0
- package/src/components/menu-item.tsx +216 -0
- package/src/components/message-composer.tsx +458 -0
- package/src/components/nav-rail.tsx +235 -0
- package/src/components/notice.tsx +133 -0
- package/src/components/optimistic-undo.ts +68 -0
- package/src/components/option-row.tsx +54 -0
- package/src/components/page-content.tsx +44 -0
- package/src/components/pagination.tsx +182 -0
- package/src/components/popover.tsx +113 -0
- package/src/components/progress.tsx +222 -0
- package/src/components/radio-group.tsx +80 -0
- package/src/components/readonly-field.tsx +251 -0
- package/src/components/segmented-control.tsx +205 -0
- package/src/components/select.tsx +259 -0
- package/src/components/separator.tsx +30 -0
- package/src/components/setting-row.tsx +277 -0
- package/src/components/sidebar-switcher.tsx +134 -0
- package/src/components/sidebar.tsx +804 -0
- package/src/components/skeleton.tsx +57 -0
- package/src/components/slider.tsx +106 -0
- package/src/components/spinner.tsx +43 -0
- package/src/components/status-dot.tsx +100 -0
- package/src/components/stepper.tsx +459 -0
- package/src/components/switch.tsx +54 -0
- package/src/components/table-cell.tsx +239 -0
- package/src/components/table-context.ts +135 -0
- package/src/components/table-head.tsx +393 -0
- package/src/components/table-tree.tsx +357 -0
- package/src/components/table.tsx +295 -0
- package/src/components/tabs.tsx +191 -0
- package/src/components/textarea.tsx +44 -0
- package/src/components/theme.tsx +120 -0
- package/src/components/thinking.tsx +295 -0
- package/src/components/toast.tsx +308 -0
- package/src/components/toolbar.tsx +166 -0
- package/src/components/tooltip.tsx +84 -0
- package/src/components/top-nav.tsx +660 -0
- package/src/components/truncate.tsx +234 -0
- package/src/deprecated.ts +61 -0
- package/src/index.ts +184 -0
- package/src/lib/cn.ts +22 -0
- package/src/lib/color.ts +469 -0
- package/src/lib/contrast.ts +220 -0
- package/src/lib/create-theme.ts +659 -0
- package/src/lib/harmony.ts +609 -0
- package/src/lib/import-theme.ts +272 -0
- package/src/lib/overflow-scroll.tsx +108 -0
- package/src/lib/radio-toggle-render.tsx +15 -0
- package/src/lib/reorder.ts +222 -0
- package/src/lib/spacing.ts +189 -0
- package/src/lib/tailwind-palette.ts +355 -0
- package/src/lib/truncate.ts +79 -0
- package/src/lib/use-single-value.ts +23 -0
- package/src/styles/continuum.css +3 -0
- package/src/styles/theme.css +524 -0
- package/src/styles/tokens.css +647 -0
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
import { buildRampFrom, type Gamut, maxChroma, type Oklch, paletteProfile } from './color';
|
|
2
|
+
import { seriesThresholds, simulateVision, wcagContrast } from './contrast';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* A theme's data colours, found from its primary. A harmony names the hues, as turns of the
|
|
6
|
+
* primary's hue on the oklch wheel, where equal turns look equally far apart (on the HSL wheel a
|
|
7
|
+
* turn of blue covers far less than one of green). Each hue is drawn as Tailwind would draw it,
|
|
8
|
+
* as vivid as the primary is, and every series then takes the step of its ramp that tells it
|
|
9
|
+
* best from its neighbours: in the lightness band for the mode, clear of grey, apart under
|
|
10
|
+
* protan and deutan vision and under full colour vision, and against the chart's surface. The
|
|
11
|
+
* order of the hues is searched the same way, so neighbours in a stack differ most. Auto
|
|
12
|
+
* searches the hues as well.
|
|
13
|
+
*
|
|
14
|
+
* A harmony can be worked within: the three with a parameter (analogous, split complementary,
|
|
15
|
+
* tetradic) take a spread; custom takes the hues as given, in their order; and any series' step
|
|
16
|
+
* can be held by hand, in place of the one the search would pick.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type ChartHarmony =
|
|
20
|
+
| 'auto'
|
|
21
|
+
| 'analogous'
|
|
22
|
+
| 'complementary'
|
|
23
|
+
| 'split'
|
|
24
|
+
| 'triadic'
|
|
25
|
+
| 'tetradic'
|
|
26
|
+
| 'square'
|
|
27
|
+
| 'monochrome'
|
|
28
|
+
| 'custom';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A harmony's one parameter, in degrees: each hue that follows it sits at `from + times × spread`;
|
|
32
|
+
* a hue without one (null) keeps the harmony's own turn.
|
|
33
|
+
*/
|
|
34
|
+
export type ChartSpread = {
|
|
35
|
+
/** What the parameter is: a spread either side, or the width of a rectangle. */
|
|
36
|
+
name: 'spread' | 'width';
|
|
37
|
+
initial: number;
|
|
38
|
+
min: number;
|
|
39
|
+
max: number;
|
|
40
|
+
members: readonly ({ from: number; times: number } | null)[];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type ChartHarmonyInfo = {
|
|
44
|
+
id: ChartHarmony;
|
|
45
|
+
label: string;
|
|
46
|
+
/** The hues of series 2 to 4, as turns of the primary's hue; a repeated hue takes another step. */
|
|
47
|
+
turns: readonly number[] | null;
|
|
48
|
+
description: string;
|
|
49
|
+
/** The parameter the harmony is drawn at, for those that have one. */
|
|
50
|
+
spread?: ChartSpread;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const chartHarmonies: readonly ChartHarmonyInfo[] = [
|
|
54
|
+
{
|
|
55
|
+
id: 'auto',
|
|
56
|
+
label: 'Auto',
|
|
57
|
+
turns: null,
|
|
58
|
+
description:
|
|
59
|
+
'The three hues that stay furthest from the primary and from each other, for every reader.',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: 'analogous',
|
|
63
|
+
label: 'Analogous',
|
|
64
|
+
turns: [-30, 30, 60],
|
|
65
|
+
description:
|
|
66
|
+
'Neighbours on the wheel, 30° and 60° on: one mood, told apart mostly by lightness.',
|
|
67
|
+
spread: {
|
|
68
|
+
name: 'spread',
|
|
69
|
+
initial: 30,
|
|
70
|
+
min: 10,
|
|
71
|
+
max: 90,
|
|
72
|
+
members: [
|
|
73
|
+
{ from: 0, times: -1 },
|
|
74
|
+
{ from: 0, times: 1 },
|
|
75
|
+
{ from: 0, times: 2 },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: 'complementary',
|
|
81
|
+
label: 'Complementary',
|
|
82
|
+
turns: [180, 0, 180],
|
|
83
|
+
description: 'The opposite hue, then the primary and its opposite again at other steps.',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: 'split',
|
|
87
|
+
label: 'Split complementary',
|
|
88
|
+
turns: [150, 210, 0],
|
|
89
|
+
description: 'The two hues either side of the opposite, then the primary at another step.',
|
|
90
|
+
spread: {
|
|
91
|
+
name: 'spread',
|
|
92
|
+
initial: 30,
|
|
93
|
+
min: 10,
|
|
94
|
+
max: 80,
|
|
95
|
+
members: [{ from: 180, times: -1 }, { from: 180, times: 1 }, null],
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: 'triadic',
|
|
100
|
+
label: 'Triadic',
|
|
101
|
+
turns: [120, 240, 0],
|
|
102
|
+
description: 'Three hues a third of the wheel apart, then the primary at another step.',
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: 'tetradic',
|
|
106
|
+
label: 'Tetradic',
|
|
107
|
+
turns: [60, 180, 240],
|
|
108
|
+
description: 'Two opposite pairs 60° apart: a rectangle on the wheel.',
|
|
109
|
+
spread: {
|
|
110
|
+
name: 'width',
|
|
111
|
+
initial: 60,
|
|
112
|
+
min: 15,
|
|
113
|
+
max: 165,
|
|
114
|
+
members: [{ from: 0, times: 1 }, null, { from: 180, times: 1 }],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
id: 'square',
|
|
119
|
+
label: 'Square',
|
|
120
|
+
turns: [90, 180, 270],
|
|
121
|
+
description: 'Four hues a quarter of the wheel apart.',
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 'monochrome',
|
|
125
|
+
label: 'Monochrome',
|
|
126
|
+
turns: [0, 0, 0],
|
|
127
|
+
description:
|
|
128
|
+
'The primary in four steps. It reads as an order more than as four series, so label them.',
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: 'custom',
|
|
132
|
+
label: 'Custom',
|
|
133
|
+
turns: null,
|
|
134
|
+
description: 'Hues placed by hand, each a turn of the primary, in the order given.',
|
|
135
|
+
},
|
|
136
|
+
];
|
|
137
|
+
|
|
138
|
+
const infoOf = (harmony: ChartHarmony) => chartHarmonies.find(item => item.id === harmony);
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The turns of series 2 to 4 a harmony names, at a spread for the three that take one (clamped to
|
|
142
|
+
* its range); null for auto and custom, whose hues are searched or given.
|
|
143
|
+
*/
|
|
144
|
+
export const harmonyTurns = (harmony: ChartHarmony, spread?: number): number[] | null => {
|
|
145
|
+
const info = infoOf(harmony);
|
|
146
|
+
if (!info?.turns) return null;
|
|
147
|
+
const range = info.spread;
|
|
148
|
+
if (!range || spread === undefined || !Number.isFinite(spread)) return [...info.turns];
|
|
149
|
+
const value = Math.min(range.max, Math.max(range.min, spread));
|
|
150
|
+
return info.turns.map((turn, i) => {
|
|
151
|
+
const member = range.members[i];
|
|
152
|
+
return member ? member.from + member.times * value : turn;
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export type ChartSeries = {
|
|
157
|
+
/** Series 1 to 4 for light mode; the first is the primary's 500 unless it was put elsewhere. */
|
|
158
|
+
light: Oklch[];
|
|
159
|
+
/** The same series for dark mode, a step lighter or darker each where the surface asks. */
|
|
160
|
+
dark: Oklch[];
|
|
161
|
+
/** The step series 1 takes in each mode, 0 for 50 … 10 for 950. */
|
|
162
|
+
primaryStep: { light: number; dark: number };
|
|
163
|
+
/** Series 1's hue as a turn of the primary's: 0 while it is the primary itself. */
|
|
164
|
+
leadTurn: number;
|
|
165
|
+
/** The hue of each series after the first, as a turn of series 1's. */
|
|
166
|
+
turns: number[];
|
|
167
|
+
/**
|
|
168
|
+
* Which of the harmony's hues each series after the first is, by its place in the harmony's
|
|
169
|
+
* turns (custom: in the turns given), so a step held by hand stays with its hue as the order
|
|
170
|
+
* is searched.
|
|
171
|
+
*/
|
|
172
|
+
members: number[];
|
|
173
|
+
/** The step each series takes in each mode, 0 for 50 … 10 for 950. */
|
|
174
|
+
steps: { light: number[]; dark: number[] };
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
type Mode = 'light' | 'dark';
|
|
178
|
+
|
|
179
|
+
/** The steps each mode's series may take, and the one a hue reads best at. */
|
|
180
|
+
const stepsFor: Record<Mode, { steps: number[]; home: number }> = {
|
|
181
|
+
light: { steps: [3, 4, 5, 6, 7, 8], home: 5 },
|
|
182
|
+
dark: { steps: [2, 3, 4, 5, 6, 7], home: 4 },
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/** The steps series 2 to 4 may take in each mode, lightest first: 0 for 50 … 10 for 950. */
|
|
186
|
+
export const chartSeriesSteps: Record<Mode, readonly number[]> = {
|
|
187
|
+
light: stepsFor.light.steps,
|
|
188
|
+
dark: stepsFor.dark.steps,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* A series' step carried from one mode to the other: the same place in the other mode's band, so a
|
|
193
|
+
* step held at the light band's 600 is the dark band's 500, one lighter, as the band itself is.
|
|
194
|
+
*/
|
|
195
|
+
export const chartStepIn = (step: number, from: Mode, to: Mode) => {
|
|
196
|
+
const source = stepsFor[from].steps;
|
|
197
|
+
const target = stepsFor[to].steps;
|
|
198
|
+
// The bands are runs of steps, so a place is a distance from the lightest; outside it is clamped.
|
|
199
|
+
const index = Math.min(source.length - 1, Math.max(0, Math.round(step) - (source[0] ?? 0)));
|
|
200
|
+
return target[index] ?? Math.round(step);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/** Series 1 put by hand: its hue as a turn of the primary's, and its light-mode step (3 to 8). */
|
|
204
|
+
export type ChartLead = { turn?: number; step?: number };
|
|
205
|
+
|
|
206
|
+
/** The geometry a harmony is worked within: its spread, custom hues, and steps held by hand. */
|
|
207
|
+
export type ChartGeometry = {
|
|
208
|
+
/**
|
|
209
|
+
* Series 1, where it is not the primary's own 500: the harmony's hues are turns of its hue, so
|
|
210
|
+
* the set follows it round.
|
|
211
|
+
*/
|
|
212
|
+
lead?: ChartLead;
|
|
213
|
+
/** The harmony's one parameter (analogous, split complementary, tetradic), in degrees. */
|
|
214
|
+
spread?: number;
|
|
215
|
+
/** Custom only: series 2 to 4 as turns of the primary's hue, in order. */
|
|
216
|
+
turns?: readonly number[];
|
|
217
|
+
/**
|
|
218
|
+
* The light-mode step (3 to 8) of each of the harmony's hues, in the order of its turns; null
|
|
219
|
+
* leaves one to the search. Dark mode takes the same place in its band. Auto has no hues to hold.
|
|
220
|
+
*/
|
|
221
|
+
steps?: readonly (number | null | undefined)[];
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
type Lab = readonly [number, number, number];
|
|
225
|
+
|
|
226
|
+
const toLab = ({ l, c, h }: Oklch): Lab => {
|
|
227
|
+
const angle = (h * Math.PI) / 180;
|
|
228
|
+
return [l, c * Math.cos(angle), c * Math.sin(angle)];
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const distance = (a: Lab, b: Lab) => 100 * Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
|
|
232
|
+
|
|
233
|
+
/** A colour readied for the search: how it looks to each reader, and how well it stands alone. */
|
|
234
|
+
type Option = {
|
|
235
|
+
color: Oklch;
|
|
236
|
+
step: number;
|
|
237
|
+
/** The turn of the hue it was drawn at, so two options of one hue are known as such. */
|
|
238
|
+
turn: number;
|
|
239
|
+
normal: Lab;
|
|
240
|
+
protan: Lab;
|
|
241
|
+
deutan: Lab;
|
|
242
|
+
/** In the mode's lightness band and clear of grey: the two checks a series must pass. */
|
|
243
|
+
fits: boolean;
|
|
244
|
+
/**
|
|
245
|
+
* 0 to 1: how well it holds against the surface, near the hue's best step; one that misses the
|
|
246
|
+
* band or the floor (taken only where nothing else is left) loses by how far it misses.
|
|
247
|
+
*/
|
|
248
|
+
alone: number;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const ready = (color: Oklch, step: number, turn: number, mode: Mode, surface: Oklch): Option => {
|
|
252
|
+
const [low, high] = seriesThresholds.band[mode];
|
|
253
|
+
// Under 3:1 a mark is still legal beside labels, so contrast is weighed, not required.
|
|
254
|
+
const contrast =
|
|
255
|
+
0.5 + 0.5 * Math.min(1, wcagContrast(color, surface) / seriesThresholds.contrast);
|
|
256
|
+
const home = 1 - 0.02 * Math.abs(step - stepsFor[mode].home);
|
|
257
|
+
const outside = Math.max(low - color.l, color.l - high, 0);
|
|
258
|
+
const near =
|
|
259
|
+
Math.max(0.1, 1 - outside * 6) *
|
|
260
|
+
Math.max(0.3, Math.min(1, color.c / seriesThresholds.chroma));
|
|
261
|
+
return {
|
|
262
|
+
color,
|
|
263
|
+
step,
|
|
264
|
+
turn,
|
|
265
|
+
normal: toLab(color),
|
|
266
|
+
protan: toLab(simulateVision(color, 'protan')),
|
|
267
|
+
deutan: toLab(simulateVision(color, 'deutan')),
|
|
268
|
+
fits: color.l >= low && color.l <= high && color.c >= seriesThresholds.chroma,
|
|
269
|
+
alone: contrast * home * near,
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/** How well two series are told apart: 1 where both thresholds are met, capped so one easy pair cannot hide a hard one. */
|
|
274
|
+
const apart = (a: Option, b: Option) =>
|
|
275
|
+
Math.min(
|
|
276
|
+
distance(a.normal, b.normal) / seriesThresholds.normal,
|
|
277
|
+
Math.min(distance(a.protan, b.protan), distance(a.deutan, b.deutan)) /
|
|
278
|
+
seriesThresholds.cvd.target,
|
|
279
|
+
1.5,
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
/** A set's score: its weakest neighbours or its weakest colour, with a share of its weakest pair overall. */
|
|
283
|
+
const score = (set: readonly Option[]) => {
|
|
284
|
+
let neighbours = Number.POSITIVE_INFINITY;
|
|
285
|
+
let all = Number.POSITIVE_INFINITY;
|
|
286
|
+
let alone = Number.POSITIVE_INFINITY;
|
|
287
|
+
for (let i = 0; i < set.length; i++) {
|
|
288
|
+
const a = set[i] as Option;
|
|
289
|
+
alone = Math.min(alone, a.alone);
|
|
290
|
+
for (let j = i + 1; j < set.length; j++) {
|
|
291
|
+
const value = apart(a, set[j] as Option);
|
|
292
|
+
all = Math.min(all, value);
|
|
293
|
+
if (j === i + 1) neighbours = Math.min(neighbours, value);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return Math.min(neighbours, alone) + 0.25 * all;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/** The same step of the same hue: one colour, which two series can never share. */
|
|
300
|
+
const same = (a: Option, b: Option) => a.step === b.step && (a.turn - b.turn) % 360 === 0;
|
|
301
|
+
|
|
302
|
+
/** The options that pass the band and the chroma floor, or all of them where none does. */
|
|
303
|
+
const fitting = (options: readonly Option[]) => {
|
|
304
|
+
const passing = options.filter(option => option.fits);
|
|
305
|
+
return passing.length ? passing : [...options];
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Every combination of one option per slot, no colour twice, scored, the best kept: first from
|
|
310
|
+
* the options that pass the band and the floor, then, if no set of distinct colours is left, from
|
|
311
|
+
* all; and where steps held by hand leave no distinct set at all, with a colour repeated.
|
|
312
|
+
*/
|
|
313
|
+
const bestOf = (slots: readonly Option[][]): { set: Option[]; score: number } => {
|
|
314
|
+
const strict = search(slots.map(fitting));
|
|
315
|
+
if (strict.set.length) return strict;
|
|
316
|
+
const loose = search(slots);
|
|
317
|
+
return loose.set.length ? loose : search(slots, true);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const search = (slots: readonly Option[][], repeat = false) => {
|
|
321
|
+
let best: Option[] = [];
|
|
322
|
+
let bestScore = Number.NEGATIVE_INFINITY;
|
|
323
|
+
const walk = (at: number, picked: Option[]) => {
|
|
324
|
+
if (at === slots.length) {
|
|
325
|
+
const value = score(picked);
|
|
326
|
+
if (value > bestScore) {
|
|
327
|
+
bestScore = value;
|
|
328
|
+
best = [...picked];
|
|
329
|
+
}
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
for (const option of slots[at] ?? []) {
|
|
333
|
+
if (!repeat && picked.some(other => same(other, option))) continue;
|
|
334
|
+
picked.push(option);
|
|
335
|
+
walk(at + 1, picked);
|
|
336
|
+
picked.pop();
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
walk(0, []);
|
|
340
|
+
return { set: best, score: bestScore };
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const permutations = <T>(items: readonly T[]): T[][] =>
|
|
344
|
+
items.length <= 1
|
|
345
|
+
? [[...items]]
|
|
346
|
+
: items.flatMap((item, i) =>
|
|
347
|
+
permutations([...items.slice(0, i), ...items.slice(i + 1)]).map(rest => [
|
|
348
|
+
item,
|
|
349
|
+
...rest,
|
|
350
|
+
]),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
const cache = new Map<string, ChartSeries>();
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* The ramps a primary's data colours are drawn from, by turn of its hue: the primary's own at 0,
|
|
357
|
+
* and every other hue as Tailwind would draw it, as vivid as the primary is. Each is built once.
|
|
358
|
+
*/
|
|
359
|
+
export const chartRamps = (primary: readonly Oklch[], gamut: Gamut) => {
|
|
360
|
+
const seed = primary[5] ?? { l: 0.55, c: 0.2, h: 265 };
|
|
361
|
+
const profile = paletteProfile(seed.h);
|
|
362
|
+
// How vivid the primary is against Tailwind's own at its step; the other hues follow it, but
|
|
363
|
+
// never so far that they read as grey.
|
|
364
|
+
const strength = Math.min(
|
|
365
|
+
1.1,
|
|
366
|
+
Math.max(
|
|
367
|
+
0.55,
|
|
368
|
+
seed.c /
|
|
369
|
+
Math.max(1e-6, maxChroma(seed.l, seed.h, gamut)) /
|
|
370
|
+
Math.max(1e-6, profile.share[gamut][5] ?? 0.9),
|
|
371
|
+
),
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
const ramps = new Map<number, readonly Oklch[]>();
|
|
375
|
+
return (turn: number): readonly Oklch[] => {
|
|
376
|
+
const t = ((turn % 360) + 360) % 360;
|
|
377
|
+
const known = ramps.get(t);
|
|
378
|
+
if (known) return known;
|
|
379
|
+
let ramp: readonly Oklch[];
|
|
380
|
+
if (t === 0) ramp = primary;
|
|
381
|
+
else {
|
|
382
|
+
const h = (seed.h + t) % 360;
|
|
383
|
+
const shape = paletteProfile(h);
|
|
384
|
+
const l = shape.l[5] ?? 0.62;
|
|
385
|
+
const c = strength * (shape.share[gamut][5] ?? 0.9) * maxChroma(l, h, gamut);
|
|
386
|
+
ramp = buildRampFrom({ l, c, h }, shape, { step: 5, gamut });
|
|
387
|
+
}
|
|
388
|
+
ramps.set(t, ramp);
|
|
389
|
+
return ramp;
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
/** Custom turns as given, when there are three and all are numbers. */
|
|
394
|
+
const givenTurns = (turns: readonly number[] | undefined) =>
|
|
395
|
+
turns?.length === 3 && turns.every(Number.isFinite) ? [...turns] : null;
|
|
396
|
+
|
|
397
|
+
/** Steps held by hand, one per hue, each within the light band or null. */
|
|
398
|
+
const heldSteps = (steps: ChartGeometry['steps']) => {
|
|
399
|
+
const [low = 3, high = 8] = [stepsFor.light.steps[0], stepsFor.light.steps.at(-1)];
|
|
400
|
+
const held = [0, 1, 2].map(i => {
|
|
401
|
+
const step = steps?.[i];
|
|
402
|
+
return typeof step === 'number' && Number.isFinite(step)
|
|
403
|
+
? Math.min(high, Math.max(low, Math.round(step)))
|
|
404
|
+
: null;
|
|
405
|
+
});
|
|
406
|
+
return held.some(step => step !== null) ? held : null;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Series 1 to 4 for a theme: the primary ramp (50 to 950, as light mode writes it), a harmony, the
|
|
411
|
+
* gamut and the chart surface in each mode, and the geometry the harmony is worked within, if any.
|
|
412
|
+
*/
|
|
413
|
+
export const buildChartSeries = (
|
|
414
|
+
primary: readonly Oklch[],
|
|
415
|
+
harmony: ChartHarmony,
|
|
416
|
+
{
|
|
417
|
+
gamut,
|
|
418
|
+
surface,
|
|
419
|
+
spread,
|
|
420
|
+
turns: customTurns,
|
|
421
|
+
steps: customSteps,
|
|
422
|
+
lead: customLead,
|
|
423
|
+
}: { gamut: Gamut; surface: Record<Mode, Oklch> } & ChartGeometry,
|
|
424
|
+
): ChartSeries => {
|
|
425
|
+
// Series 1: the primary's own hue and its 500 unless put elsewhere, within the band.
|
|
426
|
+
const leadTurn =
|
|
427
|
+
typeof customLead?.turn === 'number' && Number.isFinite(customLead.turn)
|
|
428
|
+
? ((Math.round(customLead.turn) % 360) + 360) % 360
|
|
429
|
+
: 0;
|
|
430
|
+
const [bandLow = 3, bandHigh = 8] = [stepsFor.light.steps[0], stepsFor.light.steps.at(-1)];
|
|
431
|
+
const leadStep =
|
|
432
|
+
typeof customLead?.step === 'number' && Number.isFinite(customLead.step)
|
|
433
|
+
? Math.min(bandHigh, Math.max(bandLow, Math.round(customLead.step)))
|
|
434
|
+
: 5;
|
|
435
|
+
// Custom without its three hues is auto; auto has no hues to hold a step for.
|
|
436
|
+
const given = harmony === 'custom' ? givenTurns(customTurns) : null;
|
|
437
|
+
const kind: ChartHarmony = harmony === 'custom' && !given ? 'auto' : harmony;
|
|
438
|
+
const hues = kind === 'custom' ? given : harmonyTurns(kind, spread);
|
|
439
|
+
const held = kind === 'auto' ? null : heldSteps(customSteps);
|
|
440
|
+
const key = JSON.stringify([
|
|
441
|
+
primary,
|
|
442
|
+
kind,
|
|
443
|
+
gamut,
|
|
444
|
+
surface,
|
|
445
|
+
hues && (spread !== undefined || held || kind === 'custom') ? [hues, held] : null,
|
|
446
|
+
leadTurn,
|
|
447
|
+
leadStep,
|
|
448
|
+
]);
|
|
449
|
+
const cached = cache.get(key);
|
|
450
|
+
if (cached) {
|
|
451
|
+
// The most recent go last, so a drag through many geometries never pushes out the sets in use.
|
|
452
|
+
cache.delete(key);
|
|
453
|
+
cache.set(key, cached);
|
|
454
|
+
return cached;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const rampAt = chartRamps(primary, gamut);
|
|
458
|
+
|
|
459
|
+
// A turn is of series 1's hue; the ramps are kept by turn of the primary's.
|
|
460
|
+
const optionsAt = (turn: number, mode: Mode, steps = stepsFor[mode].steps) => {
|
|
461
|
+
const ramp = rampAt(leadTurn + turn);
|
|
462
|
+
return steps.flatMap(step => {
|
|
463
|
+
const color = ramp[step];
|
|
464
|
+
return color ? [ready(color, step, turn, mode, surface[mode])] : [];
|
|
465
|
+
});
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
// A hue's options in a mode: every step of the band, or the one held for it, carried over.
|
|
469
|
+
const slot = (turn: number, mode: Mode, step: number | null | undefined) =>
|
|
470
|
+
optionsAt(
|
|
471
|
+
turn,
|
|
472
|
+
mode,
|
|
473
|
+
typeof step === 'number' ? [chartStepIn(step, 'light', mode)] : undefined,
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
// Series 1 is the colour as given in light mode: the primary's 500, or the step it was put
|
|
477
|
+
// on. It is never moved there by the search; where it misses the band, the checks say so and
|
|
478
|
+
// it can be put on another step. Dark mode has no colour of its own to keep, so its step is
|
|
479
|
+
// found among the four round the 500's place, as it always was, unless the step was put by hand.
|
|
480
|
+
const leadHeld = typeof customLead?.step === 'number' && Number.isFinite(customLead.step);
|
|
481
|
+
const first = (mode: Mode) =>
|
|
482
|
+
optionsAt(
|
|
483
|
+
0,
|
|
484
|
+
mode,
|
|
485
|
+
mode === 'light'
|
|
486
|
+
? [leadStep]
|
|
487
|
+
: leadHeld
|
|
488
|
+
? [chartStepIn(leadStep, 'light', 'dark')]
|
|
489
|
+
: [3, 4, 5, 6],
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
// A set of hues in one order (by their places in the list), at the best steps for each mode;
|
|
493
|
+
// it is as good as its weaker mode.
|
|
494
|
+
const judge = (list: readonly number[], order: readonly number[]) => {
|
|
495
|
+
const turnOf = (member: number) => list[member] ?? 0;
|
|
496
|
+
const slots = (mode: Mode) => [
|
|
497
|
+
first(mode),
|
|
498
|
+
...order.map(member => slot(turnOf(member), mode, held?.[member])),
|
|
499
|
+
];
|
|
500
|
+
const light = bestOf(slots('light'));
|
|
501
|
+
const dark = bestOf(slots('dark'));
|
|
502
|
+
return {
|
|
503
|
+
turns: order.map(turnOf),
|
|
504
|
+
members: [...order],
|
|
505
|
+
light: light.set,
|
|
506
|
+
dark: dark.set,
|
|
507
|
+
score: Math.min(light.score, dark.score),
|
|
508
|
+
};
|
|
509
|
+
};
|
|
510
|
+
const bestOrder = (list: readonly number[]) =>
|
|
511
|
+
permutations(list.map((_, i) => i))
|
|
512
|
+
.map(order => judge(list, order))
|
|
513
|
+
.reduce((best, next) => (next.score > best.score ? next : best));
|
|
514
|
+
|
|
515
|
+
let chosen: ReturnType<typeof judge>;
|
|
516
|
+
if (kind === 'custom' && hues) {
|
|
517
|
+
// Placed by hand: each hue is the series it was placed as.
|
|
518
|
+
chosen = judge(hues, [0, 1, 2]);
|
|
519
|
+
} else if (hues) {
|
|
520
|
+
chosen = bestOrder(hues);
|
|
521
|
+
} else {
|
|
522
|
+
// Auto: hues every 10°, each at least 30° from the primary and from each other, at the
|
|
523
|
+
// steps that stand well alone. The pairs are scored once, every ordered triple in light
|
|
524
|
+
// mode, and the best few hue sets are judged in both modes in every order.
|
|
525
|
+
const lead = fitting(first('light')).sort((x, y) => y.alone - x.alone)[0];
|
|
526
|
+
const pool = Array.from({ length: 31 }, (_, i) => 30 + i * 10).flatMap(turn =>
|
|
527
|
+
optionsAt(turn, 'light').filter(option => option.fits && option.alone > 0.6),
|
|
528
|
+
);
|
|
529
|
+
const gap = (x: number, y: number) => {
|
|
530
|
+
const d = Math.abs(x - y) % 360;
|
|
531
|
+
return Math.min(d, 360 - d);
|
|
532
|
+
};
|
|
533
|
+
// The best sets so far, by score, each a set of hues (the steps are picked again per mode).
|
|
534
|
+
const top: { hues: number[]; score: number }[] = [];
|
|
535
|
+
const keep = 12;
|
|
536
|
+
const floor = () =>
|
|
537
|
+
top.length < keep ? Number.NEGATIVE_INFINITY : (top.at(-1)?.score ?? 0);
|
|
538
|
+
const offer = (hues: number[], value: number) => {
|
|
539
|
+
const key = [...hues].sort((x, y) => x - y).join();
|
|
540
|
+
const known = top.findIndex(
|
|
541
|
+
entry => [...entry.hues].sort((x, y) => x - y).join() === key,
|
|
542
|
+
);
|
|
543
|
+
if (known >= 0) {
|
|
544
|
+
if ((top[known]?.score ?? 0) >= value) return;
|
|
545
|
+
top.splice(known, 1);
|
|
546
|
+
}
|
|
547
|
+
top.push({ hues, score: value });
|
|
548
|
+
top.sort((x, y) => y.score - x.score);
|
|
549
|
+
if (top.length > keep) top.pop();
|
|
550
|
+
};
|
|
551
|
+
if (lead) {
|
|
552
|
+
const n = pool.length;
|
|
553
|
+
const fromLead = pool.map(option => apart(lead, option));
|
|
554
|
+
const pair = pool.map((x, i) =>
|
|
555
|
+
pool.map((y, j) => (i === j || gap(x.turn, y.turn) < 30 ? -1 : apart(x, y))),
|
|
556
|
+
);
|
|
557
|
+
for (let i = 0; i < n; i++) {
|
|
558
|
+
const li = fromLead[i] ?? 0;
|
|
559
|
+
const alone = Math.min(lead.alone, pool[i]?.alone ?? 0);
|
|
560
|
+
for (let j = 0; j < n; j++) {
|
|
561
|
+
const ij = pair[i]?.[j] ?? -1;
|
|
562
|
+
if (ij < 0) continue;
|
|
563
|
+
// Neither the third hue nor the overall share can lift this pair past the kept sets.
|
|
564
|
+
if (Math.min(li, ij, alone) + 0.375 <= floor()) continue;
|
|
565
|
+
const lj = fromLead[j] ?? 0;
|
|
566
|
+
for (let k = 0; k < n; k++) {
|
|
567
|
+
const jk = pair[j]?.[k] ?? -1;
|
|
568
|
+
const ik = pair[i]?.[k] ?? -1;
|
|
569
|
+
if (jk < 0 || ik < 0) continue;
|
|
570
|
+
const lk = fromLead[k] ?? 0;
|
|
571
|
+
const weakest = Math.min(alone, pool[j]?.alone ?? 0, pool[k]?.alone ?? 0);
|
|
572
|
+
const value =
|
|
573
|
+
Math.min(li, ij, jk, weakest) + 0.25 * Math.min(li, lj, lk, ij, ik, jk);
|
|
574
|
+
if (value > floor()) {
|
|
575
|
+
offer(
|
|
576
|
+
[pool[i]?.turn ?? 0, pool[j]?.turn ?? 0, pool[k]?.turn ?? 0],
|
|
577
|
+
value,
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
chosen = (top.length ? top : [{ hues: [120, 240, 180], score: 0 }])
|
|
585
|
+
.map(entry => bestOrder(entry.hues))
|
|
586
|
+
.reduce((best, next) => (next.score > best.score ? next : best));
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const { turns, members, light, dark } = chosen;
|
|
590
|
+
|
|
591
|
+
const result: ChartSeries = {
|
|
592
|
+
light: light.map(option => option.color),
|
|
593
|
+
dark: dark.map(option => option.color),
|
|
594
|
+
primaryStep: { light: light[0]?.step ?? 5, dark: dark[0]?.step ?? 4 },
|
|
595
|
+
leadTurn,
|
|
596
|
+
turns,
|
|
597
|
+
members,
|
|
598
|
+
steps: {
|
|
599
|
+
light: light.map(option => option.step),
|
|
600
|
+
dark: dark.map(option => option.step),
|
|
601
|
+
},
|
|
602
|
+
};
|
|
603
|
+
if (cache.size >= 64) {
|
|
604
|
+
const oldest = cache.keys().next().value;
|
|
605
|
+
if (oldest !== undefined) cache.delete(oldest);
|
|
606
|
+
}
|
|
607
|
+
cache.set(key, result);
|
|
608
|
+
return result;
|
|
609
|
+
};
|