@sarjallab09/figma-intelligence 1.0.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 +26 -0
- package/README.md +327 -0
- package/bin/cli.js +859 -0
- package/design-bridge/.env.example +5 -0
- package/design-bridge/bridge.js +196 -0
- package/design-bridge/lib/assets.js +367 -0
- package/design-bridge/lib/prompt.js +85 -0
- package/design-bridge/lib/server.js +66 -0
- package/design-bridge/lib/stitch.js +37 -0
- package/design-bridge/lib/tokens.js +82 -0
- package/design-bridge/package-lock.json +579 -0
- package/design-bridge/package.json +19 -0
- package/figma-bridge-plugin/README.md +97 -0
- package/figma-bridge-plugin/anthropic-chat-runner.js +192 -0
- package/figma-bridge-plugin/bridge-relay.js +2363 -0
- package/figma-bridge-plugin/chat-runner.js +459 -0
- package/figma-bridge-plugin/code.js +1528 -0
- package/figma-bridge-plugin/codex-runner.js +505 -0
- package/figma-bridge-plugin/component-schemas.js +110 -0
- package/figma-bridge-plugin/content-context.js +869 -0
- package/figma-bridge-plugin/create-button.js +216 -0
- package/figma-bridge-plugin/gemini-cli-runner.js +291 -0
- package/figma-bridge-plugin/gemini-runner.js +187 -0
- package/figma-bridge-plugin/html-to-figma.js +927 -0
- package/figma-bridge-plugin/knowledge-hub/.gitkeep +0 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/anatomy-spec.md +159 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/api-spec.md +162 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/color-spec.md +148 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/full-spec-template.md +314 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/property-spec.md +175 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/screen-reader-spec.md +180 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/structure-spec.md +165 -0
- package/figma-bridge-plugin/manifest.json +21 -0
- package/figma-bridge-plugin/package-lock.json +1936 -0
- package/figma-bridge-plugin/package.json +20 -0
- package/figma-bridge-plugin/perplexity-runner.js +188 -0
- package/figma-bridge-plugin/references/SKILL.md +178 -0
- package/figma-bridge-plugin/references/anatomy-spec.md +159 -0
- package/figma-bridge-plugin/references/api-spec.md +162 -0
- package/figma-bridge-plugin/references/color-spec.md +148 -0
- package/figma-bridge-plugin/references/full-spec-template.md +314 -0
- package/figma-bridge-plugin/references/property-spec.md +175 -0
- package/figma-bridge-plugin/references/screen-reader-spec.md +180 -0
- package/figma-bridge-plugin/references/structure-spec.md +165 -0
- package/figma-bridge-plugin/shared-prompt-config.js +604 -0
- package/figma-bridge-plugin/spec-helpers/build-table.js +269 -0
- package/figma-bridge-plugin/spec-helpers/classify-elements.js +189 -0
- package/figma-bridge-plugin/spec-helpers/index.js +35 -0
- package/figma-bridge-plugin/spec-helpers/parse-figma-link.js +49 -0
- package/figma-bridge-plugin/spec-helpers/position-markers.js +158 -0
- package/figma-bridge-plugin/stitch-auth.js +322 -0
- package/figma-bridge-plugin/stitch-runner.js +1427 -0
- package/figma-bridge-plugin/token-resolver.js +107 -0
- package/figma-bridge-plugin/ui.html +4467 -0
- package/figma-intelligence-layer/.env.example +39 -0
- package/figma-intelligence-layer/docs/local-image-generation.md +60 -0
- package/figma-intelligence-layer/examples/comfyui-workflow-template.example.json +101 -0
- package/figma-intelligence-layer/jest.config.js +14 -0
- package/figma-intelligence-layer/mcp-config.json +19 -0
- package/figma-intelligence-layer/package-lock.json +5892 -0
- package/figma-intelligence-layer/package.json +48 -0
- package/figma-intelligence-layer/scripts/setup-comfyui-local.sh +67 -0
- package/figma-intelligence-layer/scripts/start-comfyui.sh +33 -0
- package/figma-intelligence-layer/src/index.ts +2233 -0
- package/figma-intelligence-layer/src/shared/auto-layout-validator.ts +404 -0
- package/figma-intelligence-layer/src/shared/cache.ts +187 -0
- package/figma-intelligence-layer/src/shared/color-operations.ts +533 -0
- package/figma-intelligence-layer/src/shared/color-utils.ts +138 -0
- package/figma-intelligence-layer/src/shared/component-script-builder.ts +413 -0
- package/figma-intelligence-layer/src/shared/component-templates.ts +2767 -0
- package/figma-intelligence-layer/src/shared/concept-taxonomy.ts +694 -0
- package/figma-intelligence-layer/src/shared/decision-log.ts +128 -0
- package/figma-intelligence-layer/src/shared/design-system-context.ts +568 -0
- package/figma-intelligence-layer/src/shared/design-system-intelligence.ts +131 -0
- package/figma-intelligence-layer/src/shared/design-system-matcher.ts +184 -0
- package/figma-intelligence-layer/src/shared/design-system-normalizers.ts +196 -0
- package/figma-intelligence-layer/src/shared/design-system-tokens.ts +295 -0
- package/figma-intelligence-layer/src/shared/dtcg-validator.ts +530 -0
- package/figma-intelligence-layer/src/shared/enrichment-pipeline.ts +671 -0
- package/figma-intelligence-layer/src/shared/figma-bridge.ts +1408 -0
- package/figma-intelligence-layer/src/shared/font-config.ts +126 -0
- package/figma-intelligence-layer/src/shared/icon-catalog.ts +360 -0
- package/figma-intelligence-layer/src/shared/icon-fetch.ts +80 -0
- package/figma-intelligence-layer/src/shared/prototype-script-builder.ts +162 -0
- package/figma-intelligence-layer/src/shared/response-compression.ts +440 -0
- package/figma-intelligence-layer/src/shared/semantic-token-catalog.ts +324 -0
- package/figma-intelligence-layer/src/shared/token-binder.ts +505 -0
- package/figma-intelligence-layer/src/shared/token-math.ts +427 -0
- package/figma-intelligence-layer/src/shared/token-naming.ts +468 -0
- package/figma-intelligence-layer/src/shared/token-utils.ts +420 -0
- package/figma-intelligence-layer/src/shared/types.ts +346 -0
- package/figma-intelligence-layer/src/shared/typography-presets.ts +94 -0
- package/figma-intelligence-layer/src/shared/unsplash.ts +165 -0
- package/figma-intelligence-layer/src/shared/vision-client.ts +607 -0
- package/figma-intelligence-layer/src/shared/vision-provider-anthropic.ts +334 -0
- package/figma-intelligence-layer/src/shared/vision-provider-openai.ts +446 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotate-handler.ts +782 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotate-renderer.ts +496 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotation-kit.ts +230 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/colorblind-sim.ts +66 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/index.ts +810 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-analyzer.ts +1191 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-figma-page.ts +1346 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-handler.ts +148 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/vpat-figma-page.ts +499 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/vpat-report.ts +910 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/wcag-checker.ts +989 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/wcag-criteria.ts +1160 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/design-from-ref/index.ts +424 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/component-recognizer.ts +38 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/ds-matcher.ts +111 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/font-matcher.ts +114 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/icon-resolver.ts +103 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/index.ts +1060 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/layout-segmenter.ts +18 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/token-inferencer.ts +39 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/vision-pipeline.ts +58 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/sketch-to-design/index.ts +298 -0
- package/figma-intelligence-layer/src/tools/phase1-vision/visual-audit/index.ts +197 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/component-audit/index.ts +494 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/intent-translator/index.ts +356 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/layout-intelligence/container-patterns.ts +123 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/layout-intelligence/index.ts +663 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/built-in-rules.yaml +56 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/index.ts +614 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/rule-engine.ts +113 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/theme-generator/color-theory.ts +178 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/theme-generator/index.ts +470 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/variant-expander/index.ts +429 -0
- package/figma-intelligence-layer/src/tools/phase2-accuracy/variant-expander/token-override-maps.ts +226 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/ai-image-insert/index.ts +535 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/component-archaeologist/index.ts +660 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/component-archaeologist/pattern-fingerprints.ts +209 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/composition-builder/index.ts +540 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/figma-animated-build.ts +391 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/page-architect/index.ts +2019 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/page-architect/screen-templates.ts +131 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/prototype-map/index.ts +381 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/prototype-wire/index.ts +565 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/swarm-build/index.ts +764 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/system-drift/index.ts +535 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/unsplash-search/index.ts +84 -0
- package/figma-intelligence-layer/src/tools/phase3-generation/url-to-frame/index.ts +401 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/css-animations.ts +68 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/framer-motion.ts +78 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/swift-animations.ts +93 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/index.ts +596 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/ci-check/index.ts +462 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/export-tokens/index.ts +1470 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/generate-component-code/index.ts +829 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/handoff-spec/index.ts +702 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/icon-library-sync/index.ts +483 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/sync-from-code/index.ts +501 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/sync-from-code/storybook-parser.ts +106 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/watch-docs/index.ts +676 -0
- package/figma-intelligence-layer/src/tools/phase4-sync/webhook-listener/index.ts +560 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/apg-doc/index.ts +1043 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/component-detection.ts +620 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/anatomy.ts +331 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/color-tokens.ts +77 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/properties.ts +54 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/snapshot.ts +287 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/spacing.ts +71 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/states.ts +43 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/typography.ts +71 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/index.ts +221 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/_default.ts +166 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/accordion.ts +232 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/alert.ts +234 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/avatar-group.ts +270 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/avatar.ts +249 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/badge.ts +231 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/banner.ts +293 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/breadcrumb.ts +240 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/button.ts +243 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/calendar.ts +307 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/card.ts +143 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/checkbox.ts +227 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/chip.ts +233 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/combobox.ts +282 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/datepicker.ts +276 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/divider.ts +223 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/drawer.ts +255 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/dropdown-menu.ts +289 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/empty-state.ts +261 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/file-uploader.ts +290 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/form.ts +265 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/grid.ts +238 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/icon.ts +255 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/index.ts +128 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/inline-edit.ts +286 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/inline-message.ts +255 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/input.ts +330 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/link.ts +247 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/list.ts +250 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/menu.ts +247 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/modal.ts +144 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/navbar.ts +264 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/navigation.ts +251 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/number-input.ts +261 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/pagination.ts +248 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/popover.ts +270 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/progress.ts +251 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/radio.ts +142 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/range-slider.ts +282 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/rating.ts +250 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/search.ts +258 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/segmented-control.ts +265 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/select.ts +319 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/skeleton.ts +256 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/slider.ts +232 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/spinner.ts +239 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/status-dot.ts +252 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/stepper.ts +270 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/table.ts +244 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tabs.ts +143 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tag.ts +243 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/textarea.ts +259 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/time-picker.ts +293 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toast.ts +144 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toggle.ts +289 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toolbar.ts +267 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tooltip.ts +232 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/treeview.ts +257 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/typography.ts +319 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/legacy-compat.ts +121 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/anatomy-diagram.ts +430 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/figma-page.ts +312 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/json.ts +129 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/markdown.ts +78 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/visual-doc.ts +2333 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/accessibility.ts +100 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/anatomy.ts +32 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/color-tokens.ts +59 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/content-guidance.ts +18 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/design-tokens.ts +53 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/interaction-rules.ts +19 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/overview.ts +91 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/properties-api.ts +71 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/qa-criteria.ts +19 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/related-components.ts +110 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/responsive.ts +19 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/size-specs.ts +67 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/spacing-structure.ts +58 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/state-specs.ts +79 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/states.ts +50 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/type-hierarchy.ts +33 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/typography.ts +55 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/usage-guidelines.ts +73 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/variants.ts +81 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/types.ts +409 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/index.ts +198 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/renderer.ts +701 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/types.ts +88 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/decision-log/index.ts +135 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/design-decision-log/index.ts +491 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/ds-primitives/index.ts +416 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/ds-scaffolder/index.ts +722 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/ds-variables/index.ts +449 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/health-report/index.ts +393 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/index.ts +406 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/figma-page.ts +292 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/json.ts +24 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/markdown.ts +172 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/naming-guide.ts +409 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/token-analytics/index.ts +594 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/token-docs/index.ts +710 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/token-migrate/index.ts +458 -0
- package/figma-intelligence-layer/src/tools/phase5-governance/token-naming/index.ts +134 -0
- package/figma-intelligence-layer/tests/apg-doc.test.ts +101 -0
- package/figma-intelligence-layer/tests/design-system-context.test.ts +152 -0
- package/figma-intelligence-layer/tests/design-system-matcher.test.ts +144 -0
- package/figma-intelligence-layer/tests/figma-bridge.test.ts +83 -0
- package/figma-intelligence-layer/tests/generate-image-and-insert.test.ts +56 -0
- package/figma-intelligence-layer/tests/screen-cloner-regression.test.ts +69 -0
- package/figma-intelligence-layer/tests/smoke.test.ts +174 -0
- package/figma-intelligence-layer/tests/spec-generator.test.ts +127 -0
- package/figma-intelligence-layer/tests/token-migrate.test.ts +21 -0
- package/figma-intelligence-layer/tests/token-naming.test.ts +30 -0
- package/figma-intelligence-layer/tsconfig.json +19 -0
- package/package.json +35 -0
- package/scripts/clean-existing-chunks.js +179 -0
- package/scripts/connect-ai-tool.js +490 -0
- package/scripts/convert-hub-pdfs.js +425 -0
- package/scripts/figma-mcp-status.js +349 -0
- package/scripts/register-codex-mcp.js +96 -0
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color operations for design token manipulation.
|
|
3
|
+
* Supports: lighten, darken, mix, alpha, hue shift, saturate, desaturate
|
|
4
|
+
* All operations work on hex color strings (#RRGGBB or #RRGGBBAA)
|
|
5
|
+
*
|
|
6
|
+
* Provides operations that Tokens Studio Pro offers but the plugin currently lacks.
|
|
7
|
+
* Pure TypeScript — no external dependencies.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Types
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
export interface RGB {
|
|
15
|
+
r: number; // 0-1
|
|
16
|
+
g: number; // 0-1
|
|
17
|
+
b: number; // 0-1
|
|
18
|
+
a: number; // 0-1
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface HSL {
|
|
22
|
+
h: number; // 0-360
|
|
23
|
+
s: number; // 0-1
|
|
24
|
+
l: number; // 0-1
|
|
25
|
+
a: number; // 0-1
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Oklch {
|
|
29
|
+
L: number; // 0-1 (perceptual lightness)
|
|
30
|
+
C: number; // 0+ (chroma)
|
|
31
|
+
h: number; // 0-360 (hue angle, degrees)
|
|
32
|
+
a: number; // 0-1 (alpha)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Internal helpers
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
function clamp(v: number, min: number, max: number): number {
|
|
40
|
+
return Math.min(Math.max(v, min), max);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function round(v: number, decimals: number): number {
|
|
44
|
+
const f = 10 ** decimals;
|
|
45
|
+
return Math.round(v * f) / f;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function hexByte(v: number): string {
|
|
49
|
+
const h = Math.round(clamp(v, 0, 1) * 255)
|
|
50
|
+
.toString(16)
|
|
51
|
+
.padStart(2, "0");
|
|
52
|
+
return h;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseHexByte(s: string): number {
|
|
56
|
+
return parseInt(s, 16) / 255;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
// Hex <-> RGB
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
export function hexToRgb(hex: string): RGB {
|
|
64
|
+
let h = hex.replace(/^#/, "");
|
|
65
|
+
|
|
66
|
+
// Expand shorthand (#RGB or #RGBA)
|
|
67
|
+
if (h.length === 3 || h.length === 4) {
|
|
68
|
+
h = h
|
|
69
|
+
.split("")
|
|
70
|
+
.map((c) => c + c)
|
|
71
|
+
.join("");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (h.length !== 6 && h.length !== 8) {
|
|
75
|
+
throw new Error(`Invalid hex color: ${hex}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
r: parseHexByte(h.slice(0, 2)),
|
|
80
|
+
g: parseHexByte(h.slice(2, 4)),
|
|
81
|
+
b: parseHexByte(h.slice(4, 6)),
|
|
82
|
+
a: h.length === 8 ? parseHexByte(h.slice(6, 8)) : 1,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function rgbToHex(rgb: RGB): string {
|
|
87
|
+
const r = hexByte(rgb.r);
|
|
88
|
+
const g = hexByte(rgb.g);
|
|
89
|
+
const b = hexByte(rgb.b);
|
|
90
|
+
if (rgb.a < 1) {
|
|
91
|
+
return `#${r}${g}${b}${hexByte(rgb.a)}`;
|
|
92
|
+
}
|
|
93
|
+
return `#${r}${g}${b}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// RGB <-> HSL
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
export function rgbToHsl(rgb: RGB): HSL {
|
|
101
|
+
const { r, g, b, a } = rgb;
|
|
102
|
+
const max = Math.max(r, g, b);
|
|
103
|
+
const min = Math.min(r, g, b);
|
|
104
|
+
const l = (max + min) / 2;
|
|
105
|
+
let h = 0;
|
|
106
|
+
let s = 0;
|
|
107
|
+
|
|
108
|
+
if (max !== min) {
|
|
109
|
+
const d = max - min;
|
|
110
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
111
|
+
switch (max) {
|
|
112
|
+
case r:
|
|
113
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
114
|
+
break;
|
|
115
|
+
case g:
|
|
116
|
+
h = ((b - r) / d + 2) / 6;
|
|
117
|
+
break;
|
|
118
|
+
case b:
|
|
119
|
+
h = ((r - g) / d + 4) / 6;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { h: h * 360, s, l, a };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function hslToRgb(hsl: HSL): RGB {
|
|
128
|
+
const { h, s, l, a } = hsl;
|
|
129
|
+
let r: number, g: number, b: number;
|
|
130
|
+
|
|
131
|
+
if (s === 0) {
|
|
132
|
+
r = g = b = l;
|
|
133
|
+
} else {
|
|
134
|
+
const hue2rgb = (p: number, q: number, t: number): number => {
|
|
135
|
+
let tt = t;
|
|
136
|
+
if (tt < 0) tt += 1;
|
|
137
|
+
if (tt > 1) tt -= 1;
|
|
138
|
+
if (tt < 1 / 6) return p + (q - p) * 6 * tt;
|
|
139
|
+
if (tt < 1 / 2) return q;
|
|
140
|
+
if (tt < 2 / 3) return p + (q - p) * (2 / 3 - tt) * 6;
|
|
141
|
+
return p;
|
|
142
|
+
};
|
|
143
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
144
|
+
const p = 2 * l - q;
|
|
145
|
+
const hNorm = h / 360;
|
|
146
|
+
r = hue2rgb(p, q, hNorm + 1 / 3);
|
|
147
|
+
g = hue2rgb(p, q, hNorm);
|
|
148
|
+
b = hue2rgb(p, q, hNorm - 1 / 3);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return { r, g, b, a };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// RGB <-> Oklch (sRGB -> linear sRGB -> XYZ D65 -> Oklab -> Oklch)
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
/** sRGB gamma to linear */
|
|
159
|
+
function srgbToLinear(c: number): number {
|
|
160
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** linear to sRGB gamma */
|
|
164
|
+
function linearToSrgb(c: number): number {
|
|
165
|
+
return c <= 0.0031308 ? c * 12.92 : 1.055 * c ** (1 / 2.4) - 0.055;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function rgbToLinear(rgb: RGB): [number, number, number] {
|
|
169
|
+
return [srgbToLinear(rgb.r), srgbToLinear(rgb.g), srgbToLinear(rgb.b)];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function linearToXyz(lr: number, lg: number, lb: number): [number, number, number] {
|
|
173
|
+
// sRGB D65 matrix
|
|
174
|
+
const x = 0.4123907992659595 * lr + 0.357584339383878 * lg + 0.1804807884018343 * lb;
|
|
175
|
+
const y = 0.21263900587151027 * lr + 0.715168678767756 * lg + 0.07219231536073371 * lb;
|
|
176
|
+
const z = 0.01933081871559182 * lr + 0.11919477979462598 * lg + 0.9505321522496607 * lb;
|
|
177
|
+
return [x, y, z];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function xyzToLinear(x: number, y: number, z: number): [number, number, number] {
|
|
181
|
+
// Inverse sRGB D65 matrix
|
|
182
|
+
const lr = 3.2409699419045226 * x + -1.5373831775700939 * y + -0.4986107602930034 * z;
|
|
183
|
+
const lg = -0.9692436362808796 * x + 1.8759675015077202 * y + 0.04155505740717559 * z;
|
|
184
|
+
const lb = 0.05563007969699366 * x + -0.20397696064091520 * y + 1.0569715142428786 * z;
|
|
185
|
+
return [lr, lg, lb];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function xyzToOklab(x: number, y: number, z: number): [number, number, number] {
|
|
189
|
+
// XYZ to LMS (using Oklab M1)
|
|
190
|
+
const l = 0.8189330101 * x + 0.3618667424 * y - 0.1288597137 * z;
|
|
191
|
+
const m = 0.0329845436 * x + 0.9293118715 * y + 0.0361456387 * z;
|
|
192
|
+
const s = 0.0482003018 * x + 0.2643662691 * y + 0.6338517070 * z;
|
|
193
|
+
|
|
194
|
+
// Cube root
|
|
195
|
+
const lp = Math.cbrt(l);
|
|
196
|
+
const mp = Math.cbrt(m);
|
|
197
|
+
const sp = Math.cbrt(s);
|
|
198
|
+
|
|
199
|
+
// LMS' to Lab (using Oklab M2)
|
|
200
|
+
const L = 0.2104542553 * lp + 0.7936177850 * mp - 0.0040720468 * sp;
|
|
201
|
+
const A = 1.9779984951 * lp - 2.4285922050 * mp + 0.4505937099 * sp;
|
|
202
|
+
const B = 0.0259040371 * lp + 0.7827717662 * mp - 0.8086757660 * sp;
|
|
203
|
+
|
|
204
|
+
return [L, A, B];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function oklabToXyz(L: number, A: number, B: number): [number, number, number] {
|
|
208
|
+
// Inverse M2
|
|
209
|
+
const lp = L + 0.3963377774 * A + 0.2158037573 * B;
|
|
210
|
+
const mp = L - 0.1055613458 * A - 0.0638541728 * B;
|
|
211
|
+
const sp = L - 0.0894841775 * A - 1.2914855480 * B;
|
|
212
|
+
|
|
213
|
+
// Cube
|
|
214
|
+
const l = lp * lp * lp;
|
|
215
|
+
const m = mp * mp * mp;
|
|
216
|
+
const s = sp * sp * sp;
|
|
217
|
+
|
|
218
|
+
// Inverse M1
|
|
219
|
+
const x = 1.2270138511035211 * l - 0.5577999806518222 * m + 0.2812561489664678 * s;
|
|
220
|
+
const y = -0.0405801784232806 * l + 1.1122568696168302 * m - 0.0716766786656012 * s;
|
|
221
|
+
const z = -0.0763812845057069 * l - 0.4214819784180127 * m + 1.5861632204407947 * s;
|
|
222
|
+
|
|
223
|
+
return [x, y, z];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function rgbToOklch(rgb: RGB): Oklch {
|
|
227
|
+
const [lr, lg, lb] = rgbToLinear(rgb);
|
|
228
|
+
const [x, y, z] = linearToXyz(lr, lg, lb);
|
|
229
|
+
const [L, A, B] = xyzToOklab(x, y, z);
|
|
230
|
+
|
|
231
|
+
const C = Math.sqrt(A * A + B * B);
|
|
232
|
+
let h = (Math.atan2(B, A) * 180) / Math.PI;
|
|
233
|
+
if (h < 0) h += 360;
|
|
234
|
+
|
|
235
|
+
return { L, C, h, a: rgb.a };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function oklchToRgb(oklch: Oklch): RGB {
|
|
239
|
+
const { L, C, h, a } = oklch;
|
|
240
|
+
const hRad = (h * Math.PI) / 180;
|
|
241
|
+
const A = C * Math.cos(hRad);
|
|
242
|
+
const B = C * Math.sin(hRad);
|
|
243
|
+
|
|
244
|
+
const [x, y, z] = oklabToXyz(L, A, B);
|
|
245
|
+
const [lr, lg, lb] = xyzToLinear(x, y, z);
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
r: clamp(linearToSrgb(lr), 0, 1),
|
|
249
|
+
g: clamp(linearToSrgb(lg), 0, 1),
|
|
250
|
+
b: clamp(linearToSrgb(lb), 0, 1),
|
|
251
|
+
a,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
// CSS Color 4 formatters
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
export function formatCssColor(
|
|
260
|
+
hex: string,
|
|
261
|
+
space: "srgb" | "display-p3" | "oklch" = "srgb",
|
|
262
|
+
): string {
|
|
263
|
+
const rgb = hexToRgb(hex);
|
|
264
|
+
|
|
265
|
+
switch (space) {
|
|
266
|
+
case "srgb": {
|
|
267
|
+
if (rgb.a < 1) {
|
|
268
|
+
return `rgb(${Math.round(rgb.r * 255)} ${Math.round(rgb.g * 255)} ${Math.round(rgb.b * 255)} / ${round(rgb.a, 3)})`;
|
|
269
|
+
}
|
|
270
|
+
return `rgb(${Math.round(rgb.r * 255)} ${Math.round(rgb.g * 255)} ${Math.round(rgb.b * 255)})`;
|
|
271
|
+
}
|
|
272
|
+
case "display-p3": {
|
|
273
|
+
// Display P3 uses the same RGB values as sRGB for in-gamut colors
|
|
274
|
+
// but expressed via the color() function
|
|
275
|
+
if (rgb.a < 1) {
|
|
276
|
+
return `color(display-p3 ${round(rgb.r, 4)} ${round(rgb.g, 4)} ${round(rgb.b, 4)} / ${round(rgb.a, 3)})`;
|
|
277
|
+
}
|
|
278
|
+
return `color(display-p3 ${round(rgb.r, 4)} ${round(rgb.g, 4)} ${round(rgb.b, 4)})`;
|
|
279
|
+
}
|
|
280
|
+
case "oklch": {
|
|
281
|
+
const o = rgbToOklch(rgb);
|
|
282
|
+
if (o.a < 1) {
|
|
283
|
+
return `oklch(${round(o.L, 4)} ${round(o.C, 4)} ${round(o.h, 2)} / ${round(o.a, 3)})`;
|
|
284
|
+
}
|
|
285
|
+
return `oklch(${round(o.L, 4)} ${round(o.C, 4)} ${round(o.h, 2)})`;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ---------------------------------------------------------------------------
|
|
291
|
+
// Color operations (all return hex strings)
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
|
|
294
|
+
export function lighten(hex: string, amount: number): string {
|
|
295
|
+
const hsl = rgbToHsl(hexToRgb(hex));
|
|
296
|
+
hsl.l = clamp(hsl.l + (1 - hsl.l) * clamp(amount, 0, 1), 0, 1);
|
|
297
|
+
return rgbToHex(hslToRgb(hsl));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function darken(hex: string, amount: number): string {
|
|
301
|
+
const hsl = rgbToHsl(hexToRgb(hex));
|
|
302
|
+
hsl.l = clamp(hsl.l * (1 - clamp(amount, 0, 1)), 0, 1);
|
|
303
|
+
return rgbToHex(hslToRgb(hsl));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function mix(hex1: string, hex2: string, weight: number = 0.5): string {
|
|
307
|
+
const c1 = hexToRgb(hex1);
|
|
308
|
+
const c2 = hexToRgb(hex2);
|
|
309
|
+
const w = clamp(weight, 0, 1);
|
|
310
|
+
return rgbToHex({
|
|
311
|
+
r: c1.r * (1 - w) + c2.r * w,
|
|
312
|
+
g: c1.g * (1 - w) + c2.g * w,
|
|
313
|
+
b: c1.b * (1 - w) + c2.b * w,
|
|
314
|
+
a: c1.a * (1 - w) + c2.a * w,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export function setAlpha(hex: string, alpha: number): string {
|
|
319
|
+
const rgb = hexToRgb(hex);
|
|
320
|
+
rgb.a = clamp(alpha, 0, 1);
|
|
321
|
+
return rgbToHex(rgb);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export function adjustHue(hex: string, degrees: number): string {
|
|
325
|
+
const hsl = rgbToHsl(hexToRgb(hex));
|
|
326
|
+
hsl.h = ((hsl.h + degrees) % 360 + 360) % 360;
|
|
327
|
+
return rgbToHex(hslToRgb(hsl));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function saturate(hex: string, amount: number): string {
|
|
331
|
+
const hsl = rgbToHsl(hexToRgb(hex));
|
|
332
|
+
hsl.s = clamp(hsl.s + (1 - hsl.s) * clamp(amount, 0, 1), 0, 1);
|
|
333
|
+
return rgbToHex(hslToRgb(hsl));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function desaturate(hex: string, amount: number): string {
|
|
337
|
+
const hsl = rgbToHsl(hexToRgb(hex));
|
|
338
|
+
hsl.s = clamp(hsl.s * (1 - clamp(amount, 0, 1)), 0, 1);
|
|
339
|
+
return rgbToHex(hslToRgb(hsl));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function complement(hex: string): string {
|
|
343
|
+
return adjustHue(hex, 180);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export function invert(hex: string): string {
|
|
347
|
+
const rgb = hexToRgb(hex);
|
|
348
|
+
return rgbToHex({
|
|
349
|
+
r: 1 - rgb.r,
|
|
350
|
+
g: 1 - rgb.g,
|
|
351
|
+
b: 1 - rgb.b,
|
|
352
|
+
a: rgb.a,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ---------------------------------------------------------------------------
|
|
357
|
+
// Contrast utilities (WCAG 2.x relative luminance)
|
|
358
|
+
// ---------------------------------------------------------------------------
|
|
359
|
+
|
|
360
|
+
export function luminance(hex: string): number {
|
|
361
|
+
const rgb = hexToRgb(hex);
|
|
362
|
+
const [lr, lg, lb] = rgbToLinear(rgb);
|
|
363
|
+
// Rec. 709 coefficients
|
|
364
|
+
return 0.2126 * lr + 0.7152 * lg + 0.0722 * lb;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function contrastRatio(hex1: string, hex2: string): number {
|
|
368
|
+
const l1 = luminance(hex1);
|
|
369
|
+
const l2 = luminance(hex2);
|
|
370
|
+
const lighter = Math.max(l1, l2);
|
|
371
|
+
const darker = Math.min(l1, l2);
|
|
372
|
+
return (lighter + 0.05) / (darker + 0.05);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function meetsWcagAA(
|
|
376
|
+
hex1: string,
|
|
377
|
+
hex2: string,
|
|
378
|
+
isLargeText: boolean = false,
|
|
379
|
+
): boolean {
|
|
380
|
+
const ratio = contrastRatio(hex1, hex2);
|
|
381
|
+
return isLargeText ? ratio >= 3 : ratio >= 4.5;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export function meetsWcagAAA(
|
|
385
|
+
hex1: string,
|
|
386
|
+
hex2: string,
|
|
387
|
+
isLargeText: boolean = false,
|
|
388
|
+
): boolean {
|
|
389
|
+
const ratio = contrastRatio(hex1, hex2);
|
|
390
|
+
return isLargeText ? ratio >= 4.5 : ratio >= 7;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Attempt to adjust the foreground color to meet the target contrast ratio
|
|
395
|
+
* against the given background. Lightens or darkens the fg in HSL space.
|
|
396
|
+
* Returns the adjusted hex color, or the best achievable if the target is
|
|
397
|
+
* impossible.
|
|
398
|
+
*/
|
|
399
|
+
export function suggestAccessibleColor(
|
|
400
|
+
bg: string,
|
|
401
|
+
fg: string,
|
|
402
|
+
targetRatio: number = 4.5,
|
|
403
|
+
): string {
|
|
404
|
+
// If already passing, return as-is
|
|
405
|
+
if (contrastRatio(bg, fg) >= targetRatio) {
|
|
406
|
+
return fg;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const bgLum = luminance(bg);
|
|
410
|
+
const hsl = rgbToHsl(hexToRgb(fg));
|
|
411
|
+
|
|
412
|
+
// Try both directions: lighter and darker
|
|
413
|
+
const tryDirection = (dir: "lighten" | "darken"): { hex: string; ratio: number } => {
|
|
414
|
+
let lo = 0;
|
|
415
|
+
let hi = 1;
|
|
416
|
+
let bestHex = fg;
|
|
417
|
+
let bestRatio = contrastRatio(bg, fg);
|
|
418
|
+
|
|
419
|
+
for (let i = 0; i < 32; i++) {
|
|
420
|
+
const mid = (lo + hi) / 2;
|
|
421
|
+
const testHsl = { ...hsl };
|
|
422
|
+
if (dir === "lighten") {
|
|
423
|
+
testHsl.l = clamp(hsl.l + (1 - hsl.l) * mid, 0, 1);
|
|
424
|
+
} else {
|
|
425
|
+
testHsl.l = clamp(hsl.l * (1 - mid), 0, 1);
|
|
426
|
+
}
|
|
427
|
+
const candidate = rgbToHex(hslToRgb(testHsl));
|
|
428
|
+
const ratio = contrastRatio(bg, candidate);
|
|
429
|
+
|
|
430
|
+
if (ratio >= targetRatio) {
|
|
431
|
+
bestHex = candidate;
|
|
432
|
+
bestRatio = ratio;
|
|
433
|
+
hi = mid; // try less adjustment
|
|
434
|
+
} else {
|
|
435
|
+
lo = mid; // need more adjustment
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return { hex: bestHex, ratio: bestRatio };
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
// Decide primary direction: if bg is dark, lighten fg; if bg is light, darken fg
|
|
442
|
+
const primaryDir = bgLum > 0.5 ? "darken" : "lighten";
|
|
443
|
+
const secondaryDir = primaryDir === "darken" ? "lighten" : "darken";
|
|
444
|
+
|
|
445
|
+
const primary = tryDirection(primaryDir);
|
|
446
|
+
if (primary.ratio >= targetRatio) {
|
|
447
|
+
return primary.hex;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const secondary = tryDirection(secondaryDir);
|
|
451
|
+
if (secondary.ratio >= targetRatio) {
|
|
452
|
+
return secondary.hex;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Return whichever got closest
|
|
456
|
+
return primary.ratio >= secondary.ratio ? primary.hex : secondary.hex;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// ---------------------------------------------------------------------------
|
|
460
|
+
// Palette generation
|
|
461
|
+
// ---------------------------------------------------------------------------
|
|
462
|
+
|
|
463
|
+
export function generateTints(hex: string, steps: number = 10): string[] {
|
|
464
|
+
const result: string[] = [];
|
|
465
|
+
for (let i = 1; i <= steps; i++) {
|
|
466
|
+
result.push(mix(hex, "#ffffff", i / (steps + 1)));
|
|
467
|
+
}
|
|
468
|
+
return result;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function generateShades(hex: string, steps: number = 10): string[] {
|
|
472
|
+
const result: string[] = [];
|
|
473
|
+
for (let i = 1; i <= steps; i++) {
|
|
474
|
+
result.push(mix(hex, "#000000", i / (steps + 1)));
|
|
475
|
+
}
|
|
476
|
+
return result;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Generate a Tailwind-style tint/shade scale where 500 is the base color.
|
|
481
|
+
* Returns keys: 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950
|
|
482
|
+
*/
|
|
483
|
+
export function generateTintShadeScale(
|
|
484
|
+
hex: string,
|
|
485
|
+
steps: number = 11,
|
|
486
|
+
): Record<string, string> {
|
|
487
|
+
// Fixed scale labels matching Tailwind convention
|
|
488
|
+
const labels = ["50", "100", "200", "300", "400", "500", "600", "700", "800", "900", "950"];
|
|
489
|
+
|
|
490
|
+
// Lightness targets (approximate Tailwind lightness distribution)
|
|
491
|
+
// 50 is lightest, 950 is darkest. 500 = base color.
|
|
492
|
+
const lightnessTargets = [0.95, 0.90, 0.80, 0.70, 0.60, 0.50, 0.40, 0.30, 0.22, 0.14, 0.09];
|
|
493
|
+
|
|
494
|
+
const hsl = rgbToHsl(hexToRgb(hex));
|
|
495
|
+
const baseLightness = hsl.l;
|
|
496
|
+
|
|
497
|
+
const scale: Record<string, string> = {};
|
|
498
|
+
|
|
499
|
+
for (let i = 0; i < labels.length; i++) {
|
|
500
|
+
const target = lightnessTargets[i];
|
|
501
|
+
|
|
502
|
+
if (labels[i] === "500") {
|
|
503
|
+
// 500 is always the base color
|
|
504
|
+
scale[labels[i]] = hex;
|
|
505
|
+
} else {
|
|
506
|
+
// Map target lightness relative to the base color's natural position
|
|
507
|
+
// For tints (above 500): interpolate between base and white
|
|
508
|
+
// For shades (below 500): interpolate between base and black
|
|
509
|
+
const adjusted = { ...hsl };
|
|
510
|
+
|
|
511
|
+
if (target > 0.5) {
|
|
512
|
+
// Tint territory: blend toward white
|
|
513
|
+
const t = (target - 0.5) / 0.5; // 0..1 as target goes from 0.5 to 1.0
|
|
514
|
+
adjusted.l = baseLightness + (1 - baseLightness) * t;
|
|
515
|
+
// Desaturate tints slightly for more natural palette
|
|
516
|
+
adjusted.s = hsl.s * (1 - t * 0.3);
|
|
517
|
+
} else {
|
|
518
|
+
// Shade territory: blend toward black
|
|
519
|
+
const t = (0.5 - target) / 0.5; // 0..1 as target goes from 0.5 to 0.0
|
|
520
|
+
adjusted.l = baseLightness * (1 - t);
|
|
521
|
+
// Slightly increase saturation in mid-shades, reduce in deep shades
|
|
522
|
+
adjusted.s = hsl.s * (1 - t * 0.15);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
adjusted.l = clamp(adjusted.l, 0, 1);
|
|
526
|
+
adjusted.s = clamp(adjusted.s, 0, 1);
|
|
527
|
+
|
|
528
|
+
scale[labels[i]] = rgbToHex(hslToRgb(adjusted));
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return scale;
|
|
533
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
2
|
+
// Color Utilities
|
|
3
|
+
// Shared color conversion, manipulation, and analysis helpers used across
|
|
4
|
+
// the token utilities, theme generator, a11y audit, and vision pipeline.
|
|
5
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
export interface HSL {
|
|
8
|
+
h: number; // 0–360
|
|
9
|
+
s: number; // 0–1
|
|
10
|
+
l: number; // 0–1
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface RGB {
|
|
14
|
+
r: number; // 0–255
|
|
15
|
+
g: number;
|
|
16
|
+
b: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ─── Hex ↔ RGB ────────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
export function hexToRgb(hex: string): RGB | null {
|
|
22
|
+
const clean = hex.replace(/^#/, "");
|
|
23
|
+
if (clean.length !== 6 && clean.length !== 3) return null;
|
|
24
|
+
const full = clean.length === 3
|
|
25
|
+
? clean.split("").map((c) => c + c).join("")
|
|
26
|
+
: clean;
|
|
27
|
+
const num = parseInt(full, 16);
|
|
28
|
+
if (isNaN(num)) return null;
|
|
29
|
+
return { r: (num >> 16) & 255, g: (num >> 8) & 255, b: num & 255 };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function rgbToHex(r: number, g: number, b: number): string {
|
|
33
|
+
return (
|
|
34
|
+
"#" +
|
|
35
|
+
[r, g, b]
|
|
36
|
+
.map((c) => Math.round(Math.max(0, Math.min(255, c))).toString(16).padStart(2, "0"))
|
|
37
|
+
.join("")
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Convert Figma's 0-1 RGBA components to a hex string. */
|
|
42
|
+
export function figmaRgbaToHex(r: number, g: number, b: number): string {
|
|
43
|
+
return rgbToHex(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ─── RGB ↔ HSL ────────────────────────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
export function rgbToHsl(r: number, g: number, b: number): HSL {
|
|
49
|
+
const rn = r / 255;
|
|
50
|
+
const gn = g / 255;
|
|
51
|
+
const bn = b / 255;
|
|
52
|
+
const max = Math.max(rn, gn, bn);
|
|
53
|
+
const min = Math.min(rn, gn, bn);
|
|
54
|
+
const l = (max + min) / 2;
|
|
55
|
+
|
|
56
|
+
if (max === min) return { h: 0, s: 0, l };
|
|
57
|
+
|
|
58
|
+
const d = max - min;
|
|
59
|
+
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
60
|
+
let h: number;
|
|
61
|
+
if (max === rn) h = ((gn - bn) / d + (gn < bn ? 6 : 0)) / 6;
|
|
62
|
+
else if (max === gn) h = ((bn - rn) / d + 2) / 6;
|
|
63
|
+
else h = ((rn - gn) / d + 4) / 6;
|
|
64
|
+
|
|
65
|
+
return { h: h * 360, s, l };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function hue2rgb(p: number, q: number, t: number): number {
|
|
69
|
+
let tn = t;
|
|
70
|
+
if (tn < 0) tn += 1;
|
|
71
|
+
if (tn > 1) tn -= 1;
|
|
72
|
+
if (tn < 1 / 6) return p + (q - p) * 6 * tn;
|
|
73
|
+
if (tn < 1 / 2) return q;
|
|
74
|
+
if (tn < 2 / 3) return p + (q - p) * (2 / 3 - tn) * 6;
|
|
75
|
+
return p;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function hslToRgb(h: number, s: number, l: number): RGB {
|
|
79
|
+
if (s === 0) {
|
|
80
|
+
const v = Math.round(l * 255);
|
|
81
|
+
return { r: v, g: v, b: v };
|
|
82
|
+
}
|
|
83
|
+
const hn = h / 360;
|
|
84
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
85
|
+
const p = 2 * l - q;
|
|
86
|
+
return {
|
|
87
|
+
r: Math.round(hue2rgb(p, q, hn + 1 / 3) * 255),
|
|
88
|
+
g: Math.round(hue2rgb(p, q, hn) * 255),
|
|
89
|
+
b: Math.round(hue2rgb(p, q, hn - 1 / 3) * 255),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ─── Luminance & Contrast ─────────────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
/** WCAG relative luminance (0–1). */
|
|
96
|
+
export function relativeLuminance(r: number, g: number, b: number): number {
|
|
97
|
+
const [rs, gs, bs] = [r, g, b].map((c) => {
|
|
98
|
+
const srgb = c / 255;
|
|
99
|
+
return srgb <= 0.03928 ? srgb / 12.92 : Math.pow((srgb + 0.055) / 1.055, 2.4);
|
|
100
|
+
});
|
|
101
|
+
return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** WCAG contrast ratio between two hex colors. */
|
|
105
|
+
export function contrastRatio(hex1: string, hex2: string): number {
|
|
106
|
+
const c1 = hexToRgb(hex1);
|
|
107
|
+
const c2 = hexToRgb(hex2);
|
|
108
|
+
if (!c1 || !c2) return 1;
|
|
109
|
+
const l1 = relativeLuminance(c1.r, c1.g, c1.b);
|
|
110
|
+
const l2 = relativeLuminance(c2.r, c2.g, c2.b);
|
|
111
|
+
const lighter = Math.max(l1, l2);
|
|
112
|
+
const darker = Math.min(l1, l2);
|
|
113
|
+
return (lighter + 0.05) / (darker + 0.05);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ─── Color Distance ───────────────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/** Euclidean distance in RGB space (0–441.67). */
|
|
119
|
+
export function colorDistance(hex1: string, hex2: string): number {
|
|
120
|
+
const c1 = hexToRgb(hex1);
|
|
121
|
+
const c2 = hexToRgb(hex2);
|
|
122
|
+
if (!c1 || !c2) return Infinity;
|
|
123
|
+
return Math.sqrt(
|
|
124
|
+
(c1.r - c2.r) ** 2 + (c1.g - c2.g) ** 2 + (c1.b - c2.b) ** 2
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Hue + lightness distance for semantic color matching. */
|
|
129
|
+
export function semanticColorDistance(hex1: string, hex2: string): number {
|
|
130
|
+
const c1 = hexToRgb(hex1);
|
|
131
|
+
const c2 = hexToRgb(hex2);
|
|
132
|
+
if (!c1 || !c2) return Infinity;
|
|
133
|
+
const hsl1 = rgbToHsl(c1.r, c1.g, c1.b);
|
|
134
|
+
const hsl2 = rgbToHsl(c2.r, c2.g, c2.b);
|
|
135
|
+
const hueDist = Math.min(Math.abs(hsl1.h - hsl2.h), 360 - Math.abs(hsl1.h - hsl2.h)) / 180;
|
|
136
|
+
const lightDist = Math.abs(hsl1.l - hsl2.l);
|
|
137
|
+
return hueDist * 0.6 + lightDist * 0.4;
|
|
138
|
+
}
|