@wordpress/theme 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/LICENSE.md +788 -0
  2. package/README.md +67 -0
  3. package/bin/build-tokens.js +83 -0
  4. package/bin/generate-primitive-tokens/index.ts +115 -0
  5. package/bin/terrazzo-plugin-ds-tokens-docs/index.ts +103 -0
  6. package/bin/terrazzo-plugin-figma-ds-token-manager/index.ts +210 -0
  7. package/bin/terrazzo-plugin-figma-ds-token-manager/lib.ts +1 -0
  8. package/bin/terrazzo-plugin-known-wpds-css-variables/index.ts +72 -0
  9. package/build/color-ramps/index.js +132 -0
  10. package/build/color-ramps/index.js.map +7 -0
  11. package/build/color-ramps/lib/cache-utils.js +57 -0
  12. package/build/color-ramps/lib/cache-utils.js.map +7 -0
  13. package/build/color-ramps/lib/constants.js +105 -0
  14. package/build/color-ramps/lib/constants.js.map +7 -0
  15. package/build/color-ramps/lib/find-color-with-constraints.js +141 -0
  16. package/build/color-ramps/lib/find-color-with-constraints.js.map +7 -0
  17. package/build/color-ramps/lib/index.js +264 -0
  18. package/build/color-ramps/lib/index.js.map +7 -0
  19. package/build/color-ramps/lib/ramp-configs.js +315 -0
  20. package/build/color-ramps/lib/ramp-configs.js.map +7 -0
  21. package/build/color-ramps/lib/taper-chroma.js +159 -0
  22. package/build/color-ramps/lib/taper-chroma.js.map +7 -0
  23. package/build/color-ramps/lib/types.js +17 -0
  24. package/build/color-ramps/lib/types.js.map +7 -0
  25. package/build/color-ramps/lib/utils.js +106 -0
  26. package/build/color-ramps/lib/utils.js.map +7 -0
  27. package/build/context.js +34 -0
  28. package/build/context.js.map +7 -0
  29. package/build/index.js +29 -0
  30. package/build/index.js.map +7 -0
  31. package/build/lock-unlock.js +35 -0
  32. package/build/lock-unlock.js.map +7 -0
  33. package/build/prebuilt/js/design-tokens.js +135 -0
  34. package/build/prebuilt/js/design-tokens.js.map +7 -0
  35. package/build/prebuilt/json/figma.json +1317 -0
  36. package/build/prebuilt/ts/design-tokens.js +354 -0
  37. package/build/prebuilt/ts/design-tokens.js.map +7 -0
  38. package/build/private-apis.js +36 -0
  39. package/build/private-apis.js.map +7 -0
  40. package/build/style.module.css.js +2 -0
  41. package/build/theme-provider.js +92 -0
  42. package/build/theme-provider.js.map +7 -0
  43. package/build/types/css-modules.d.js +2 -0
  44. package/build/types/css-modules.d.js.map +7 -0
  45. package/build/types.js +17 -0
  46. package/build/types.js.map +7 -0
  47. package/build/use-theme-provider-styles.js +230 -0
  48. package/build/use-theme-provider-styles.js.map +7 -0
  49. package/build-module/color-ramps/index.js +95 -0
  50. package/build-module/color-ramps/index.js.map +7 -0
  51. package/build-module/color-ramps/lib/cache-utils.js +31 -0
  52. package/build-module/color-ramps/lib/cache-utils.js.map +7 -0
  53. package/build-module/color-ramps/lib/constants.js +63 -0
  54. package/build-module/color-ramps/lib/constants.js.map +7 -0
  55. package/build-module/color-ramps/lib/find-color-with-constraints.js +112 -0
  56. package/build-module/color-ramps/lib/find-color-with-constraints.js.map +7 -0
  57. package/build-module/color-ramps/lib/index.js +235 -0
  58. package/build-module/color-ramps/lib/index.js.map +7 -0
  59. package/build-module/color-ramps/lib/ramp-configs.js +290 -0
  60. package/build-module/color-ramps/lib/ramp-configs.js.map +7 -0
  61. package/build-module/color-ramps/lib/taper-chroma.js +125 -0
  62. package/build-module/color-ramps/lib/taper-chroma.js.map +7 -0
  63. package/build-module/color-ramps/lib/types.js +1 -0
  64. package/build-module/color-ramps/lib/types.js.map +7 -0
  65. package/build-module/color-ramps/lib/utils.js +84 -0
  66. package/build-module/color-ramps/lib/utils.js.map +7 -0
  67. package/build-module/context.js +10 -0
  68. package/build-module/context.js.map +7 -0
  69. package/build-module/index.js +5 -0
  70. package/build-module/index.js.map +7 -0
  71. package/build-module/lock-unlock.js +10 -0
  72. package/build-module/lock-unlock.js.map +7 -0
  73. package/build-module/prebuilt/js/design-tokens.js +115 -0
  74. package/build-module/prebuilt/js/design-tokens.js.map +7 -0
  75. package/build-module/prebuilt/json/figma.json +1317 -0
  76. package/build-module/prebuilt/ts/design-tokens.js +334 -0
  77. package/build-module/prebuilt/ts/design-tokens.js.map +7 -0
  78. package/build-module/private-apis.js +12 -0
  79. package/build-module/private-apis.js.map +7 -0
  80. package/build-module/style.module.css.js +1 -0
  81. package/build-module/theme-provider.js +58 -0
  82. package/build-module/theme-provider.js.map +7 -0
  83. package/build-module/types/css-modules.d.js +1 -0
  84. package/build-module/types/css-modules.d.js.map +7 -0
  85. package/build-module/types.js +1 -0
  86. package/build-module/types.js.map +7 -0
  87. package/build-module/use-theme-provider-styles.js +200 -0
  88. package/build-module/use-theme-provider-styles.js.map +7 -0
  89. package/build-style/style.css +3 -0
  90. package/build-types/color-ramps/index.d.ts +44 -0
  91. package/build-types/color-ramps/index.d.ts.map +1 -0
  92. package/build-types/color-ramps/lib/cache-utils.d.ts +22 -0
  93. package/build-types/color-ramps/lib/cache-utils.d.ts.map +1 -0
  94. package/build-types/color-ramps/lib/constants.d.ts +38 -0
  95. package/build-types/color-ramps/lib/constants.d.ts.map +1 -0
  96. package/build-types/color-ramps/lib/find-color-with-constraints.d.ts +37 -0
  97. package/build-types/color-ramps/lib/find-color-with-constraints.d.ts.map +1 -0
  98. package/build-types/color-ramps/lib/index.d.ts +11 -0
  99. package/build-types/color-ramps/lib/index.d.ts.map +1 -0
  100. package/build-types/color-ramps/lib/ramp-configs.d.ts +7 -0
  101. package/build-types/color-ramps/lib/ramp-configs.d.ts.map +1 -0
  102. package/build-types/color-ramps/lib/taper-chroma.d.ts +32 -0
  103. package/build-types/color-ramps/lib/taper-chroma.d.ts.map +1 -0
  104. package/build-types/color-ramps/lib/types.d.ts +78 -0
  105. package/build-types/color-ramps/lib/types.d.ts.map +1 -0
  106. package/build-types/color-ramps/lib/utils.d.ts +38 -0
  107. package/build-types/color-ramps/lib/utils.d.ts.map +1 -0
  108. package/build-types/color-ramps/stories/index.story.d.ts +14 -0
  109. package/build-types/color-ramps/stories/index.story.d.ts.map +1 -0
  110. package/build-types/color-ramps/stories/ramp-table.d.ts +19 -0
  111. package/build-types/color-ramps/stories/ramp-table.d.ts.map +1 -0
  112. package/build-types/context.d.ts +10 -0
  113. package/build-types/context.d.ts.map +1 -0
  114. package/build-types/index.d.ts +2 -0
  115. package/build-types/index.d.ts.map +1 -0
  116. package/build-types/lock-unlock.d.ts +2 -0
  117. package/build-types/lock-unlock.d.ts.map +1 -0
  118. package/build-types/prebuilt/js/design-tokens.d.ts +3 -0
  119. package/build-types/prebuilt/js/design-tokens.d.ts.map +1 -0
  120. package/build-types/prebuilt/ts/design-tokens.d.ts +7 -0
  121. package/build-types/prebuilt/ts/design-tokens.d.ts.map +1 -0
  122. package/build-types/private-apis.d.ts +2 -0
  123. package/build-types/private-apis.d.ts.map +1 -0
  124. package/build-types/stories/index.story.d.ts +15 -0
  125. package/build-types/stories/index.story.d.ts.map +1 -0
  126. package/build-types/theme-provider.d.ts +3 -0
  127. package/build-types/theme-provider.d.ts.map +1 -0
  128. package/build-types/types.d.ts +42 -0
  129. package/build-types/types.d.ts.map +1 -0
  130. package/build-types/use-theme-provider-styles.d.ts +17 -0
  131. package/build-types/use-theme-provider-styles.d.ts.map +1 -0
  132. package/docs/ds-tokens.md +283 -0
  133. package/package.json +58 -0
  134. package/src/color-ramps/index.ts +155 -0
  135. package/src/color-ramps/lib/cache-utils.ts +56 -0
  136. package/src/color-ramps/lib/constants.ts +85 -0
  137. package/src/color-ramps/lib/find-color-with-constraints.ts +190 -0
  138. package/src/color-ramps/lib/index.ts +369 -0
  139. package/src/color-ramps/lib/ramp-configs.ts +309 -0
  140. package/src/color-ramps/lib/taper-chroma.ts +226 -0
  141. package/src/color-ramps/lib/types.ts +90 -0
  142. package/src/color-ramps/lib/utils.ts +161 -0
  143. package/src/color-ramps/stories/index.story.tsx +264 -0
  144. package/src/color-ramps/stories/ramp-table.tsx +212 -0
  145. package/src/color-ramps/test/__snapshots__/index.test.ts.snap +1280 -0
  146. package/src/color-ramps/test/index.test.ts +94 -0
  147. package/src/context.ts +19 -0
  148. package/src/index.ts +2 -0
  149. package/src/lock-unlock.ts +10 -0
  150. package/src/prebuilt/css/design-tokens.css +401 -0
  151. package/src/prebuilt/js/design-tokens.js +116 -0
  152. package/src/prebuilt/json/figma.json +1317 -0
  153. package/src/prebuilt/ts/design-tokens.ts +335 -0
  154. package/src/private-apis.ts +12 -0
  155. package/src/stories/index.story.tsx +426 -0
  156. package/src/style.module.css +3 -0
  157. package/src/theme-provider.tsx +87 -0
  158. package/src/types/css-modules.d.ts +4 -0
  159. package/src/types.ts +44 -0
  160. package/src/use-theme-provider-styles.ts +247 -0
  161. package/terrazzo.config.ts +102 -0
  162. package/tokens/border.json +34 -0
  163. package/tokens/color.json +877 -0
  164. package/tokens/elevation.json +201 -0
  165. package/tokens/spacing.json +45 -0
  166. package/tokens/typography.json +93 -0
  167. package/tsconfig.json +9 -0
  168. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var color_ramps_exports = {};
30
+ __export(color_ramps_exports, {
31
+ DEFAULT_SEED_COLORS: () => import_constants2.DEFAULT_SEED_COLORS,
32
+ buildAccentRamp: () => buildAccentRamp,
33
+ buildBgRamp: () => buildBgRamp,
34
+ checkAccessibleCombinations: () => checkAccessibleCombinations
35
+ });
36
+ module.exports = __toCommonJS(color_ramps_exports);
37
+ var import_colorjs = __toESM(require("colorjs.io"));
38
+ var import_lib = require("./lib/index");
39
+ var import_utils = require("./lib/utils");
40
+ var import_ramp_configs = require("./lib/ramp-configs");
41
+ var import_cache_utils = require("./lib/cache-utils");
42
+ var import_constants = require("./lib/constants");
43
+ var import_constants2 = require("./lib/constants");
44
+ function buildBgRamp({
45
+ seed,
46
+ debug
47
+ }) {
48
+ if (typeof seed !== "string" || seed.trim() === "") {
49
+ throw new Error("Seed color must be a non-empty string");
50
+ }
51
+ return (0, import_lib.buildRamp)(seed, import_ramp_configs.BG_RAMP_CONFIG, { debug });
52
+ }
53
+ const STEP_TO_PIN = "surface2";
54
+ function getBgRampInfo(ramp) {
55
+ return {
56
+ mainDirection: ramp.direction,
57
+ pinLightness: {
58
+ stepName: STEP_TO_PIN,
59
+ value: (0, import_utils.clampAccentScaleReferenceLightness)(
60
+ new import_colorjs.default(ramp.ramp[STEP_TO_PIN].color).oklch.l,
61
+ ramp.direction
62
+ )
63
+ }
64
+ };
65
+ }
66
+ function buildAccentRamp({
67
+ seed,
68
+ bgRamp,
69
+ debug
70
+ }) {
71
+ if (typeof seed !== "string" || seed.trim() === "") {
72
+ throw new Error("Seed color must be a non-empty string");
73
+ }
74
+ const bgRampInfo = bgRamp ? getBgRampInfo(bgRamp) : void 0;
75
+ return (0, import_lib.buildRamp)(seed, import_ramp_configs.ACCENT_RAMP_CONFIG, {
76
+ ...bgRampInfo,
77
+ debug
78
+ });
79
+ }
80
+ function checkAccessibleCombinations({
81
+ bgRamp,
82
+ accentRamps = []
83
+ }) {
84
+ const unmetTargets = [];
85
+ [bgRamp, ...accentRamps].forEach((ramp) => {
86
+ import_constants.CONTRAST_COMBINATIONS.forEach(({ bgs, fgs, target }) => {
87
+ for (const bg of bgs) {
88
+ for (const fg of fgs) {
89
+ const bgColor = new import_colorjs.default(ramp.ramp[bg].color);
90
+ const fgColor = new import_colorjs.default(ramp.ramp[fg].color);
91
+ if ((0, import_cache_utils.getCachedContrast)(bgColor, fgColor) < target) {
92
+ unmetTargets.push({
93
+ bgName: bg,
94
+ bgColor: bgColor.toString(),
95
+ fgName: fg,
96
+ fgColor: fgColor.toString(),
97
+ unmetContrast: target
98
+ });
99
+ }
100
+ }
101
+ }
102
+ });
103
+ });
104
+ accentRamps.forEach((ramp) => {
105
+ import_constants.CONTRAST_COMBINATIONS.forEach(({ bgs, fgs, target }) => {
106
+ for (const bg of bgs) {
107
+ for (const fg of fgs) {
108
+ const bgColor = new import_colorjs.default(bgRamp.ramp[bg].color);
109
+ const fgColor = new import_colorjs.default(ramp.ramp[fg].color);
110
+ if ((0, import_cache_utils.getCachedContrast)(bgColor, fgColor) < target) {
111
+ unmetTargets.push({
112
+ bgName: bg,
113
+ bgColor: bgColor.toString(),
114
+ fgName: fg,
115
+ fgColor: fgColor.toString(),
116
+ unmetContrast: target
117
+ });
118
+ }
119
+ }
120
+ }
121
+ });
122
+ });
123
+ return unmetTargets;
124
+ }
125
+ // Annotate the CommonJS export names for ESM import in node:
126
+ 0 && (module.exports = {
127
+ DEFAULT_SEED_COLORS,
128
+ buildAccentRamp,
129
+ buildBgRamp,
130
+ checkAccessibleCombinations
131
+ });
132
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/color-ramps/index.ts"],
4
+ "sourcesContent": ["/**\n * External dependencies\n */\nimport Color from 'colorjs.io';\n\n/**\n * Internal dependencies\n */\nimport { buildRamp } from './lib/index';\nimport { clampAccentScaleReferenceLightness } from './lib/utils';\nimport { BG_RAMP_CONFIG, ACCENT_RAMP_CONFIG } from './lib/ramp-configs';\nimport type {\n\tRampResult as InternalRampResult,\n\tRampDirection,\n\tRamp,\n} from './lib/types';\nimport { getCachedContrast } from './lib/cache-utils';\nimport { CONTRAST_COMBINATIONS } from './lib/constants';\nexport { DEFAULT_SEED_COLORS } from './lib/constants';\n\n/**\n * Creates a background ramp.\n * @param params\n * @param params.seed\n * @param params.debug\n */\nexport function buildBgRamp( {\n\tseed,\n\tdebug,\n}: {\n\tseed: string;\n\tdebug?: boolean;\n} ): InternalRampResult {\n\tif ( typeof seed !== 'string' || seed.trim() === '' ) {\n\t\tthrow new Error( 'Seed color must be a non-empty string' );\n\t}\n\n\treturn buildRamp( seed, BG_RAMP_CONFIG, { debug } );\n}\n\nconst STEP_TO_PIN = 'surface2';\nfunction getBgRampInfo( ramp: InternalRampResult ): {\n\tmainDirection: RampDirection;\n\tpinLightness: {\n\t\tstepName: keyof Ramp;\n\t\tvalue: number;\n\t};\n} {\n\treturn {\n\t\tmainDirection: ramp.direction,\n\t\tpinLightness: {\n\t\t\tstepName: STEP_TO_PIN,\n\t\t\tvalue: clampAccentScaleReferenceLightness(\n\t\t\t\tnew Color( ramp.ramp[ STEP_TO_PIN ].color ).oklch.l,\n\t\t\t\tramp.direction\n\t\t\t),\n\t\t},\n\t};\n}\n\n/**\n * Creates an accent ramp (ie used by primary, success, info, warning and error\n * ramps).\n * @param params\n * @param params.seed\n * @param params.bgRamp\n * @param params.debug\n */\nexport function buildAccentRamp( {\n\tseed,\n\tbgRamp,\n\tdebug,\n}: {\n\tseed: string;\n\tbgRamp?: InternalRampResult;\n\tdebug?: boolean;\n} ): InternalRampResult {\n\tif ( typeof seed !== 'string' || seed.trim() === '' ) {\n\t\tthrow new Error( 'Seed color must be a non-empty string' );\n\t}\n\n\tconst bgRampInfo = bgRamp ? getBgRampInfo( bgRamp ) : undefined;\n\treturn buildRamp( seed, ACCENT_RAMP_CONFIG, {\n\t\t...bgRampInfo,\n\t\tdebug,\n\t} );\n}\n\n/**\n * Checks that all bg/fg combinations generated by the ramps meet contrast\n * targets.\n * @param params\n * @param params.bgRamp\n * @param params.accentRamps\n */\nexport function checkAccessibleCombinations( {\n\tbgRamp,\n\taccentRamps = [],\n}: {\n\tbgRamp: InternalRampResult;\n\taccentRamps?: InternalRampResult[];\n} ) {\n\tconst unmetTargets: {\n\t\tbgName: keyof Ramp;\n\t\tbgColor: string;\n\t\tfgName: keyof Ramp;\n\t\tfgColor: string;\n\t\tunmetContrast: number;\n\t}[] = [];\n\n\t// Assess combinations within each ramp\n\t[ bgRamp, ...accentRamps ].forEach( ( ramp ) => {\n\t\tCONTRAST_COMBINATIONS.forEach( ( { bgs, fgs, target } ) => {\n\t\t\tfor ( const bg of bgs ) {\n\t\t\t\tfor ( const fg of fgs ) {\n\t\t\t\t\tconst bgColor = new Color( ramp.ramp[ bg ].color );\n\t\t\t\t\tconst fgColor = new Color( ramp.ramp[ fg ].color );\n\t\t\t\t\tif ( getCachedContrast( bgColor, fgColor ) < target ) {\n\t\t\t\t\t\tunmetTargets.push( {\n\t\t\t\t\t\t\tbgName: bg,\n\t\t\t\t\t\t\tbgColor: bgColor.toString(),\n\t\t\t\t\t\t\tfgName: fg,\n\t\t\t\t\t\t\tfgColor: fgColor.toString(),\n\t\t\t\t\t\t\tunmetContrast: target,\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n\t} );\n\t// Assess each accent ramp's fg color against bg ramp\n\taccentRamps.forEach( ( ramp ) => {\n\t\tCONTRAST_COMBINATIONS.forEach( ( { bgs, fgs, target } ) => {\n\t\t\tfor ( const bg of bgs ) {\n\t\t\t\tfor ( const fg of fgs ) {\n\t\t\t\t\tconst bgColor = new Color( bgRamp.ramp[ bg ].color );\n\t\t\t\t\tconst fgColor = new Color( ramp.ramp[ fg ].color );\n\t\t\t\t\tif ( getCachedContrast( bgColor, fgColor ) < target ) {\n\t\t\t\t\t\tunmetTargets.push( {\n\t\t\t\t\t\t\tbgName: bg,\n\t\t\t\t\t\t\tbgColor: bgColor.toString(),\n\t\t\t\t\t\t\tfgName: fg,\n\t\t\t\t\t\t\tfgColor: fgColor.toString(),\n\t\t\t\t\t\t\tunmetContrast: target,\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n\t} );\n\n\treturn unmetTargets;\n}\n\nexport type RampResult = InternalRampResult;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAkB;AAKlB,iBAA0B;AAC1B,mBAAmD;AACnD,0BAAmD;AAMnD,yBAAkC;AAClC,uBAAsC;AACtC,IAAAA,oBAAoC;AAQ7B,SAAS,YAAa;AAAA,EAC5B;AAAA,EACA;AACD,GAGwB;AACvB,MAAK,OAAO,SAAS,YAAY,KAAK,KAAK,MAAM,IAAK;AACrD,UAAM,IAAI,MAAO,uCAAwC;AAAA,EAC1D;AAEA,aAAO,sBAAW,MAAM,oCAAgB,EAAE,MAAM,CAAE;AACnD;AAEA,MAAM,cAAc;AACpB,SAAS,cAAe,MAMtB;AACD,SAAO;AAAA,IACN,eAAe,KAAK;AAAA,IACpB,cAAc;AAAA,MACb,UAAU;AAAA,MACV,WAAO;AAAA,QACN,IAAI,eAAAC,QAAO,KAAK,KAAM,WAAY,EAAE,KAAM,EAAE,MAAM;AAAA,QAClD,KAAK;AAAA,MACN;AAAA,IACD;AAAA,EACD;AACD;AAUO,SAAS,gBAAiB;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AACD,GAIwB;AACvB,MAAK,OAAO,SAAS,YAAY,KAAK,KAAK,MAAM,IAAK;AACrD,UAAM,IAAI,MAAO,uCAAwC;AAAA,EAC1D;AAEA,QAAM,aAAa,SAAS,cAAe,MAAO,IAAI;AACtD,aAAO,sBAAW,MAAM,wCAAoB;AAAA,IAC3C,GAAG;AAAA,IACH;AAAA,EACD,CAAE;AACH;AASO,SAAS,4BAA6B;AAAA,EAC5C;AAAA,EACA,cAAc,CAAC;AAChB,GAGI;AACH,QAAM,eAMA,CAAC;AAGP,GAAE,QAAQ,GAAG,WAAY,EAAE,QAAS,CAAE,SAAU;AAC/C,2CAAsB,QAAS,CAAE,EAAE,KAAK,KAAK,OAAO,MAAO;AAC1D,iBAAY,MAAM,KAAM;AACvB,mBAAY,MAAM,KAAM;AACvB,gBAAM,UAAU,IAAI,eAAAA,QAAO,KAAK,KAAM,EAAG,EAAE,KAAM;AACjD,gBAAM,UAAU,IAAI,eAAAA,QAAO,KAAK,KAAM,EAAG,EAAE,KAAM;AACjD,kBAAK,sCAAmB,SAAS,OAAQ,IAAI,QAAS;AACrD,yBAAa,KAAM;AAAA,cAClB,QAAQ;AAAA,cACR,SAAS,QAAQ,SAAS;AAAA,cAC1B,QAAQ;AAAA,cACR,SAAS,QAAQ,SAAS;AAAA,cAC1B,eAAe;AAAA,YAChB,CAAE;AAAA,UACH;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAE;AAAA,EACH,CAAE;AAEF,cAAY,QAAS,CAAE,SAAU;AAChC,2CAAsB,QAAS,CAAE,EAAE,KAAK,KAAK,OAAO,MAAO;AAC1D,iBAAY,MAAM,KAAM;AACvB,mBAAY,MAAM,KAAM;AACvB,gBAAM,UAAU,IAAI,eAAAA,QAAO,OAAO,KAAM,EAAG,EAAE,KAAM;AACnD,gBAAM,UAAU,IAAI,eAAAA,QAAO,KAAK,KAAM,EAAG,EAAE,KAAM;AACjD,kBAAK,sCAAmB,SAAS,OAAQ,IAAI,QAAS;AACrD,yBAAa,KAAM;AAAA,cAClB,QAAQ;AAAA,cACR,SAAS,QAAQ,SAAS;AAAA,cAC1B,QAAQ;AAAA,cACR,SAAS,QAAQ,SAAS;AAAA,cAC1B,eAAe;AAAA,YAChB,CAAE;AAAA,UACH;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAE;AAAA,EACH,CAAE;AAEF,SAAO;AACR;",
6
+ "names": ["import_constants", "Color"]
7
+ }
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var cache_utils_exports = {};
20
+ __export(cache_utils_exports, {
21
+ clearCaches: () => clearCaches,
22
+ getCachedContrast: () => getCachedContrast,
23
+ getColorString: () => getColorString
24
+ });
25
+ module.exports = __toCommonJS(cache_utils_exports);
26
+ const contrastCache = /* @__PURE__ */ new Map();
27
+ const colorStringCache = /* @__PURE__ */ new Map();
28
+ function getColorString(color) {
29
+ let str = colorStringCache.get(color);
30
+ if (str === void 0) {
31
+ str = color.to("srgb").toString({ format: "hex", inGamut: true });
32
+ colorStringCache.set(color, str);
33
+ }
34
+ return str;
35
+ }
36
+ function getCachedContrast(colorA, colorB) {
37
+ const keyA = getColorString(colorA);
38
+ const keyB = getColorString(colorB);
39
+ const cacheKey = keyA < keyB ? `${keyA}|${keyB}` : `${keyB}|${keyA}`;
40
+ let contrast = contrastCache.get(cacheKey);
41
+ if (contrast === void 0) {
42
+ contrast = colorA.contrastWCAG21(colorB);
43
+ contrastCache.set(cacheKey, contrast);
44
+ }
45
+ return contrast;
46
+ }
47
+ function clearCaches() {
48
+ contrastCache.clear();
49
+ colorStringCache.clear();
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ clearCaches,
54
+ getCachedContrast,
55
+ getColorString
56
+ });
57
+ //# sourceMappingURL=cache-utils.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/color-ramps/lib/cache-utils.ts"],
4
+ "sourcesContent": ["/**\n * External dependencies\n */\nimport type Color from 'colorjs.io';\n\n/**\n * Cache for WCAG contrast calculations\n */\nconst contrastCache = new Map< string, number >();\n\n/**\n * Cache for color string representations\n */\nconst colorStringCache = new Map< Color, string >();\n\n/**\n * Get cached string representation of a color\n * @param color - Color object to stringify\n * @return Cached string representation\n */\nexport function getColorString( color: Color ): string {\n\tlet str = colorStringCache.get( color );\n\tif ( str === undefined ) {\n\t\tstr = color.to( 'srgb' ).toString( { format: 'hex', inGamut: true } );\n\t\tcolorStringCache.set( color, str );\n\t}\n\treturn str;\n}\n\n/**\n * Get cached contrast calculation between two colors\n * @param colorA - First color\n * @param colorB - Second color\n * @return WCAG 2.1 contrast ratio\n */\nexport function getCachedContrast( colorA: Color, colorB: Color ): number {\n\tconst keyA = getColorString( colorA );\n\tconst keyB = getColorString( colorB );\n\tconst cacheKey =\n\t\tkeyA < keyB ? `${ keyA }|${ keyB }` : `${ keyB }|${ keyA }`;\n\n\tlet contrast = contrastCache.get( cacheKey );\n\tif ( contrast === undefined ) {\n\t\tcontrast = colorA.contrastWCAG21( colorB );\n\t\tcontrastCache.set( cacheKey, contrast );\n\t}\n\treturn contrast;\n}\n\n/**\n * Clear all caches - useful for memory management or testing\n */\nexport function clearCaches(): void {\n\tcontrastCache.clear();\n\tcolorStringCache.clear();\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,MAAM,gBAAgB,oBAAI,IAAsB;AAKhD,MAAM,mBAAmB,oBAAI,IAAqB;AAO3C,SAAS,eAAgB,OAAuB;AACtD,MAAI,MAAM,iBAAiB,IAAK,KAAM;AACtC,MAAK,QAAQ,QAAY;AACxB,UAAM,MAAM,GAAI,MAAO,EAAE,SAAU,EAAE,QAAQ,OAAO,SAAS,KAAK,CAAE;AACpE,qBAAiB,IAAK,OAAO,GAAI;AAAA,EAClC;AACA,SAAO;AACR;AAQO,SAAS,kBAAmB,QAAe,QAAwB;AACzE,QAAM,OAAO,eAAgB,MAAO;AACpC,QAAM,OAAO,eAAgB,MAAO;AACpC,QAAM,WACL,OAAO,OAAO,GAAI,IAAK,IAAK,IAAK,KAAK,GAAI,IAAK,IAAK,IAAK;AAE1D,MAAI,WAAW,cAAc,IAAK,QAAS;AAC3C,MAAK,aAAa,QAAY;AAC7B,eAAW,OAAO,eAAgB,MAAO;AACzC,kBAAc,IAAK,UAAU,QAAS;AAAA,EACvC;AACA,SAAO;AACR;AAKO,SAAS,cAAoB;AACnC,gBAAc,MAAM;AACpB,mBAAiB,MAAM;AACxB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var constants_exports = {};
30
+ __export(constants_exports, {
31
+ ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS: () => ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS,
32
+ BLACK: () => BLACK,
33
+ CONTRAST_COMBINATIONS: () => CONTRAST_COMBINATIONS,
34
+ DEFAULT_SEED_COLORS: () => DEFAULT_SEED_COLORS,
35
+ LIGHTNESS_EPSILON: () => LIGHTNESS_EPSILON,
36
+ MAX_BISECTION_ITERATIONS: () => MAX_BISECTION_ITERATIONS,
37
+ UNIVERSAL_CONTRAST_TOPUP: () => UNIVERSAL_CONTRAST_TOPUP,
38
+ WHITE: () => WHITE,
39
+ WHITE_TEXT_CONTRAST_MARGIN: () => WHITE_TEXT_CONTRAST_MARGIN
40
+ });
41
+ module.exports = __toCommonJS(constants_exports);
42
+ var import_colorjs = __toESM(require("colorjs.io"));
43
+ const WHITE = new import_colorjs.default("#fff").to("oklch");
44
+ const BLACK = new import_colorjs.default("#000").to("oklch");
45
+ const UNIVERSAL_CONTRAST_TOPUP = 0.05;
46
+ const WHITE_TEXT_CONTRAST_MARGIN = 3.1;
47
+ const ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS = {
48
+ lighter: { min: 0.2, max: 0.4 },
49
+ darker: { min: 0.75, max: 0.98 }
50
+ };
51
+ const LIGHTNESS_EPSILON = 1e-3;
52
+ const MAX_BISECTION_ITERATIONS = 25;
53
+ const CONTRAST_COMBINATIONS = [
54
+ {
55
+ bgs: ["surface1", "surface2", "surface3"],
56
+ fgs: ["fgSurface3", "fgSurface4"],
57
+ target: 4.5
58
+ },
59
+ {
60
+ bgs: ["surface4", "surface5"],
61
+ fgs: ["fgSurface4"],
62
+ target: 4.5
63
+ },
64
+ {
65
+ bgs: ["bgFill1"],
66
+ fgs: ["fgFill"],
67
+ target: 4.5
68
+ },
69
+ {
70
+ bgs: ["bgFillInverted1"],
71
+ fgs: ["fgFillInverted"],
72
+ target: 4.5
73
+ },
74
+ {
75
+ bgs: ["bgFillInverted1"],
76
+ fgs: ["fgFillInverted"],
77
+ target: 4.5
78
+ },
79
+ {
80
+ bgs: ["surface1", "surface2", "surface3"],
81
+ fgs: ["stroke3"],
82
+ target: 3
83
+ }
84
+ ];
85
+ const DEFAULT_SEED_COLORS = {
86
+ bg: "#f8f8f8",
87
+ primary: "#3858e9",
88
+ info: "#0090ff",
89
+ success: "#4ab866",
90
+ warning: "#f0b849",
91
+ error: "#cc1818"
92
+ };
93
+ // Annotate the CommonJS export names for ESM import in node:
94
+ 0 && (module.exports = {
95
+ ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS,
96
+ BLACK,
97
+ CONTRAST_COMBINATIONS,
98
+ DEFAULT_SEED_COLORS,
99
+ LIGHTNESS_EPSILON,
100
+ MAX_BISECTION_ITERATIONS,
101
+ UNIVERSAL_CONTRAST_TOPUP,
102
+ WHITE,
103
+ WHITE_TEXT_CONTRAST_MARGIN
104
+ });
105
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/color-ramps/lib/constants.ts"],
4
+ "sourcesContent": ["/**\n * External dependencies\n */\nimport Color from 'colorjs.io';\n\n/**\n * Internal dependencies\n */\nimport type { Ramp } from './types';\n\nexport const WHITE = new Color( '#fff' ).to( 'oklch' );\nexport const BLACK = new Color( '#000' ).to( 'oklch' );\n\n// Margin added to target contrasts to counter for algorithm approximations\n// and rounding errors.\nexport const UNIVERSAL_CONTRAST_TOPUP = 0.05;\n\n// When enabling \"lighter direction\" bias, this is the amount by which\n// black text contrast needs to be greater than white text contrast.\n// The higher the value, the stronger the preference for white text.\n// The current value has been determined empirically as the highest value\n// that won't cause the algo not to be able to correctly solve all contrasts.\nexport const WHITE_TEXT_CONTRAST_MARGIN = 3.1;\n\n// These values are used as thresholds when trying to match the background\n// ramp's lightness while calculating an accent ramp. They prevent the accent\n// scale from being pinned to lightness values in the middle of the range,\n// which would cause the algorithm to struggle to satisfy the accent scale\n// constraints and therefore produce unexpected results.\nexport const ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS = {\n\tlighter: { min: 0.2, max: 0.4 },\n\tdarker: { min: 0.75, max: 0.98 },\n} as const;\n\n// Minimum lightness difference noticed by the algorithm.\nexport const LIGHTNESS_EPSILON = 1e-3;\n\nexport const MAX_BISECTION_ITERATIONS = 25;\n\nexport const CONTRAST_COMBINATIONS: {\n\tbgs: ( keyof Ramp )[];\n\tfgs: ( keyof Ramp )[];\n\ttarget: number;\n}[] = [\n\t{\n\t\tbgs: [ 'surface1', 'surface2', 'surface3' ],\n\t\tfgs: [ 'fgSurface3', 'fgSurface4' ],\n\t\ttarget: 4.5,\n\t},\n\t{\n\t\tbgs: [ 'surface4', 'surface5' ],\n\t\tfgs: [ 'fgSurface4' ],\n\t\ttarget: 4.5,\n\t},\n\t{\n\t\tbgs: [ 'bgFill1' ],\n\t\tfgs: [ 'fgFill' ],\n\t\ttarget: 4.5,\n\t},\n\t{\n\t\tbgs: [ 'bgFillInverted1' ],\n\t\tfgs: [ 'fgFillInverted' ],\n\t\ttarget: 4.5,\n\t},\n\t{\n\t\tbgs: [ 'bgFillInverted1' ],\n\t\tfgs: [ 'fgFillInverted' ],\n\t\ttarget: 4.5,\n\t},\n\t{\n\t\tbgs: [ 'surface1', 'surface2', 'surface3' ],\n\t\tfgs: [ 'stroke3' ],\n\t\ttarget: 3,\n\t},\n];\n\n// Used when generating the DTCG tokens and the static color ramps.\nexport const DEFAULT_SEED_COLORS = {\n\tbg: '#f8f8f8',\n\tprimary: '#3858e9',\n\tinfo: '#0090ff',\n\tsuccess: '#4ab866',\n\twarning: '#f0b849',\n\terror: '#cc1818',\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAkB;AAOX,MAAM,QAAQ,IAAI,eAAAA,QAAO,MAAO,EAAE,GAAI,OAAQ;AAC9C,MAAM,QAAQ,IAAI,eAAAA,QAAO,MAAO,EAAE,GAAI,OAAQ;AAI9C,MAAM,2BAA2B;AAOjC,MAAM,6BAA6B;AAOnC,MAAM,yCAAyC;AAAA,EACrD,SAAS,EAAE,KAAK,KAAK,KAAK,IAAI;AAAA,EAC9B,QAAQ,EAAE,KAAK,MAAM,KAAK,KAAK;AAChC;AAGO,MAAM,oBAAoB;AAE1B,MAAM,2BAA2B;AAEjC,MAAM,wBAIP;AAAA,EACL;AAAA,IACC,KAAK,CAAE,YAAY,YAAY,UAAW;AAAA,IAC1C,KAAK,CAAE,cAAc,YAAa;AAAA,IAClC,QAAQ;AAAA,EACT;AAAA,EACA;AAAA,IACC,KAAK,CAAE,YAAY,UAAW;AAAA,IAC9B,KAAK,CAAE,YAAa;AAAA,IACpB,QAAQ;AAAA,EACT;AAAA,EACA;AAAA,IACC,KAAK,CAAE,SAAU;AAAA,IACjB,KAAK,CAAE,QAAS;AAAA,IAChB,QAAQ;AAAA,EACT;AAAA,EACA;AAAA,IACC,KAAK,CAAE,iBAAkB;AAAA,IACzB,KAAK,CAAE,gBAAiB;AAAA,IACxB,QAAQ;AAAA,EACT;AAAA,EACA;AAAA,IACC,KAAK,CAAE,iBAAkB;AAAA,IACzB,KAAK,CAAE,gBAAiB;AAAA,IACxB,QAAQ;AAAA,EACT;AAAA,EACA;AAAA,IACC,KAAK,CAAE,YAAY,YAAY,UAAW;AAAA,IAC1C,KAAK,CAAE,SAAU;AAAA,IACjB,QAAQ;AAAA,EACT;AACD;AAGO,MAAM,sBAAsB;AAAA,EAClC,IAAI;AAAA,EACJ,SAAS;AAAA,EACT,MAAM;AAAA,EACN,SAAS;AAAA,EACT,SAAS;AAAA,EACT,OAAO;AACR;",
6
+ "names": ["Color"]
7
+ }
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var find_color_with_constraints_exports = {};
30
+ __export(find_color_with_constraints_exports, {
31
+ findColorMeetingRequirements: () => findColorMeetingRequirements
32
+ });
33
+ module.exports = __toCommonJS(find_color_with_constraints_exports);
34
+ var import_colorjs = __toESM(require("colorjs.io"));
35
+ var import_utils = require("./utils");
36
+ var import_constants = require("./constants");
37
+ var import_cache_utils = require("./cache-utils");
38
+ var import_taper_chroma = require("./taper-chroma");
39
+ function findColorMeetingRequirements(reference, seed, target, direction, {
40
+ lightnessConstraint,
41
+ taperChromaOptions,
42
+ strict = true,
43
+ debug = false
44
+ } = {}) {
45
+ if (target <= 1) {
46
+ return { color: seed.clone(), reached: true, achieved: 1 };
47
+ }
48
+ if (lightnessConstraint) {
49
+ let newL = lightnessConstraint.value;
50
+ let newC = seed.oklch.c;
51
+ if (taperChromaOptions) {
52
+ ({ l: newL, c: newC } = (0, import_taper_chroma.taperChroma)(
53
+ seed,
54
+ newL,
55
+ taperChromaOptions
56
+ ));
57
+ }
58
+ const colorWithExactL = (0, import_utils.clampToGamut)(
59
+ new import_colorjs.default("oklch", [newL, newC, seed.oklch.h])
60
+ );
61
+ const exactLContrast = (0, import_cache_utils.getCachedContrast)(reference, colorWithExactL);
62
+ if (debug) {
63
+ console.log(
64
+ `Succeeded with ${lightnessConstraint.type} lightness`,
65
+ lightnessConstraint.value,
66
+ colorWithExactL.oklch.l
67
+ );
68
+ }
69
+ if (lightnessConstraint.type === "force" || exactLContrast >= target) {
70
+ return {
71
+ color: colorWithExactL,
72
+ reached: exactLContrast >= target,
73
+ achieved: exactLContrast
74
+ };
75
+ }
76
+ }
77
+ const mostContrastingL = direction === "lighter" ? 1 : 0;
78
+ const mostContrastingColor = direction === "lighter" ? import_constants.WHITE : import_constants.BLACK;
79
+ const highestPossibleContrast = (0, import_cache_utils.getCachedContrast)(
80
+ reference,
81
+ mostContrastingColor
82
+ );
83
+ if (highestPossibleContrast < target) {
84
+ if (strict) {
85
+ throw new Error(
86
+ `Contrast target ${target.toFixed(
87
+ 2
88
+ )}:1 unreachable in ${direction} direction against ${mostContrastingColor.toString()}(boundary achieves ${highestPossibleContrast.toFixed(
89
+ 3
90
+ )}:1).`
91
+ );
92
+ }
93
+ if (debug) {
94
+ console.log(
95
+ "Did not succeeded because it reached the limit",
96
+ mostContrastingL
97
+ );
98
+ }
99
+ return {
100
+ color: mostContrastingColor,
101
+ reached: false,
102
+ achieved: highestPossibleContrast
103
+ };
104
+ }
105
+ let worseL = reference.oklch.l;
106
+ let betterL = mostContrastingL;
107
+ let bestContrastFound = highestPossibleContrast;
108
+ let resultingColor = mostContrastingColor;
109
+ for (let i = 0; i < import_constants.MAX_BISECTION_ITERATIONS && Math.abs(betterL - worseL) > import_constants.LIGHTNESS_EPSILON; i++) {
110
+ let newL = (worseL + betterL) / 2;
111
+ let newC = seed.oklch.c;
112
+ if (taperChromaOptions) {
113
+ ({ l: newL, c: newC } = (0, import_taper_chroma.taperChroma)(
114
+ seed,
115
+ newL,
116
+ taperChromaOptions
117
+ ));
118
+ }
119
+ const newColor = (0, import_utils.clampToGamut)(
120
+ new import_colorjs.default("oklch", [newL, newC, seed.oklch.h])
121
+ );
122
+ const newContrast = (0, import_cache_utils.getCachedContrast)(reference, newColor);
123
+ if (newContrast >= target) {
124
+ betterL = newL;
125
+ bestContrastFound = newContrast;
126
+ resultingColor = newColor;
127
+ } else {
128
+ worseL = newL;
129
+ }
130
+ }
131
+ return {
132
+ color: resultingColor,
133
+ reached: true,
134
+ achieved: bestContrastFound
135
+ };
136
+ }
137
+ // Annotate the CommonJS export names for ESM import in node:
138
+ 0 && (module.exports = {
139
+ findColorMeetingRequirements
140
+ });
141
+ //# sourceMappingURL=find-color-with-constraints.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/color-ramps/lib/find-color-with-constraints.ts"],
4
+ "sourcesContent": ["/**\n * External dependencies\n */\nimport Color from 'colorjs.io';\n\n/**\n * Internal dependencies\n */\nimport { clampToGamut } from './utils';\nimport {\n\tWHITE,\n\tBLACK,\n\tLIGHTNESS_EPSILON,\n\tMAX_BISECTION_ITERATIONS,\n} from './constants';\nimport { getCachedContrast } from './cache-utils';\nimport { type TaperChromaOptions, taperChroma } from './taper-chroma';\n\n/**\n * Solve for L such that:\n * - the L applied to the seed meets the contrast target against the reference\n * - the search is performed in one direction (ie lighter / darker)\n * - more constraints can be applied around lightness\n * - chroma could be tapered\n * @param reference\n * @param seed\n * @param target\n * @param direction\n * @param options\n * @param options.strict\n * @param options.debug\n * @param options.lightnessConstraint\n * @param options.lightnessConstraint.type\n * @param options.lightnessConstraint.value\n * @param options.taperChromaOptions\n */\nexport function findColorMeetingRequirements(\n\treference: Color,\n\tseed: Color,\n\ttarget: number,\n\tdirection: 'lighter' | 'darker',\n\t{\n\t\tlightnessConstraint,\n\t\ttaperChromaOptions,\n\t\tstrict = true,\n\t\tdebug = false,\n\t}: {\n\t\tlightnessConstraint?: {\n\t\t\ttype: 'force' | 'onlyIfSucceeds';\n\t\t\tvalue: number;\n\t\t};\n\t\ttaperChromaOptions?: TaperChromaOptions;\n\t\tstrict?: boolean;\n\t\tdebug?: boolean;\n\t} = {}\n): { color: Color; reached: boolean; achieved: number } {\n\t// A target of 1 means same color.\n\t// A target lower than 1 doesn't make sense.\n\tif ( target <= 1 ) {\n\t\treturn { color: seed.clone(), reached: true, achieved: 1 };\n\t}\n\n\tif ( lightnessConstraint ) {\n\t\t// Apply a specific L value.\n\t\t// Useful when pinning a step to a specific lightness, of to specify\n\t\t// min/max L values.\n\t\tlet newL = lightnessConstraint.value;\n\t\tlet newC = seed.oklch.c;\n\n\t\tif ( taperChromaOptions ) {\n\t\t\t( { l: newL, c: newC } = taperChroma(\n\t\t\t\tseed,\n\t\t\t\tnewL,\n\t\t\t\ttaperChromaOptions\n\t\t\t) );\n\t\t}\n\n\t\tconst colorWithExactL = clampToGamut(\n\t\t\tnew Color( 'oklch', [ newL, newC, seed.oklch.h ] )\n\t\t);\n\t\tconst exactLContrast = getCachedContrast( reference, colorWithExactL );\n\n\t\tif ( debug ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.log(\n\t\t\t\t`Succeeded with ${ lightnessConstraint.type } lightness`,\n\t\t\t\tlightnessConstraint.value,\n\t\t\t\tcolorWithExactL.oklch.l\n\t\t\t);\n\t\t}\n\n\t\t// If the L constraint is of \"force\" type, apply it even when it doesn't\n\t\t// meet the contrast target.\n\t\tif (\n\t\t\tlightnessConstraint.type === 'force' ||\n\t\t\texactLContrast >= target\n\t\t) {\n\t\t\treturn {\n\t\t\t\tcolor: colorWithExactL,\n\t\t\t\treached: exactLContrast >= target,\n\t\t\t\tachieved: exactLContrast,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Set the boundary based on the direction.\n\tconst mostContrastingL = direction === 'lighter' ? 1 : 0;\n\tconst mostContrastingColor = direction === 'lighter' ? WHITE : BLACK;\n\tconst highestPossibleContrast = getCachedContrast(\n\t\treference,\n\t\tmostContrastingColor\n\t);\n\n\t// If even the most contrasting color can't reach the target,\n\t// the target is unreachable.\n\tif ( highestPossibleContrast < target ) {\n\t\tif ( strict ) {\n\t\t\tthrow new Error(\n\t\t\t\t`Contrast target ${ target.toFixed(\n\t\t\t\t\t2\n\t\t\t\t) }:1 unreachable in ${ direction } direction against ${ mostContrastingColor.toString() }` +\n\t\t\t\t\t`(boundary achieves ${ highestPossibleContrast.toFixed(\n\t\t\t\t\t\t3\n\t\t\t\t\t) }:1).`\n\t\t\t);\n\t\t}\n\n\t\tif ( debug ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.log(\n\t\t\t\t'Did not succeeded because it reached the limit',\n\t\t\t\tmostContrastingL\n\t\t\t);\n\t\t}\n\t\treturn {\n\t\t\tcolor: mostContrastingColor,\n\t\t\treached: false,\n\t\t\tachieved: highestPossibleContrast,\n\t\t};\n\t}\n\n\t// Bracket: low fails, high meets.\n\t// Originally this was seed.oklch.l \u2014 although it's an assumption that works\n\t// only when we know for sure the direction of the search.\n\t// TODO: can we bring this back to seed.oklch.l ?\n\tlet worseL = reference.oklch.l;\n\tlet betterL = mostContrastingL;\n\n\tlet bestContrastFound = highestPossibleContrast;\n\tlet resultingColor = mostContrastingColor;\n\n\tfor (\n\t\tlet i = 0;\n\t\ti < MAX_BISECTION_ITERATIONS &&\n\t\tMath.abs( betterL - worseL ) > LIGHTNESS_EPSILON;\n\t\ti++\n\t) {\n\t\tlet newL = ( worseL + betterL ) / 2;\n\t\tlet newC = seed.oklch.c;\n\n\t\tif ( taperChromaOptions ) {\n\t\t\t( { l: newL, c: newC } = taperChroma(\n\t\t\t\tseed,\n\t\t\t\tnewL,\n\t\t\t\ttaperChromaOptions\n\t\t\t) );\n\t\t}\n\n\t\tconst newColor = clampToGamut(\n\t\t\tnew Color( 'oklch', [ newL, newC, seed.oklch.h ] )\n\t\t);\n\t\tconst newContrast = getCachedContrast( reference, newColor );\n\n\t\tif ( newContrast >= target ) {\n\t\t\tbetterL = newL;\n\t\t\t// Only update the resulting color when the target is met, this ensuring\n\t\t\t// at the end of the search the target is always met.\n\t\t\tbestContrastFound = newContrast;\n\t\t\tresultingColor = newColor;\n\t\t} else {\n\t\t\tworseL = newL;\n\t\t}\n\t}\n\n\treturn {\n\t\tcolor: resultingColor,\n\t\treached: true,\n\t\tachieved: bestContrastFound,\n\t};\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAkB;AAKlB,mBAA6B;AAC7B,uBAKO;AACP,yBAAkC;AAClC,0BAAqD;AAoB9C,SAAS,6BACf,WACA,MACA,QACA,WACA;AAAA,EACC;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT,QAAQ;AACT,IAQI,CAAC,GACkD;AAGvD,MAAK,UAAU,GAAI;AAClB,WAAO,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,MAAM,UAAU,EAAE;AAAA,EAC1D;AAEA,MAAK,qBAAsB;AAI1B,QAAI,OAAO,oBAAoB;AAC/B,QAAI,OAAO,KAAK,MAAM;AAEtB,QAAK,oBAAqB;AACzB,OAAE,EAAE,GAAG,MAAM,GAAG,KAAK,QAAI;AAAA,QACxB;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAEA,UAAM,sBAAkB;AAAA,MACvB,IAAI,eAAAA,QAAO,SAAS,CAAE,MAAM,MAAM,KAAK,MAAM,CAAE,CAAE;AAAA,IAClD;AACA,UAAM,qBAAiB,sCAAmB,WAAW,eAAgB;AAErE,QAAK,OAAQ;AAEZ,cAAQ;AAAA,QACP,kBAAmB,oBAAoB,IAAK;AAAA,QAC5C,oBAAoB;AAAA,QACpB,gBAAgB,MAAM;AAAA,MACvB;AAAA,IACD;AAIA,QACC,oBAAoB,SAAS,WAC7B,kBAAkB,QACjB;AACD,aAAO;AAAA,QACN,OAAO;AAAA,QACP,SAAS,kBAAkB;AAAA,QAC3B,UAAU;AAAA,MACX;AAAA,IACD;AAAA,EACD;AAGA,QAAM,mBAAmB,cAAc,YAAY,IAAI;AACvD,QAAM,uBAAuB,cAAc,YAAY,yBAAQ;AAC/D,QAAM,8BAA0B;AAAA,IAC/B;AAAA,IACA;AAAA,EACD;AAIA,MAAK,0BAA0B,QAAS;AACvC,QAAK,QAAS;AACb,YAAM,IAAI;AAAA,QACT,mBAAoB,OAAO;AAAA,UAC1B;AAAA,QACD,CAAE,qBAAsB,SAAU,sBAAuB,qBAAqB,SAAS,CAAE,sBACjE,wBAAwB;AAAA,UAC9C;AAAA,QACD,CAAE;AAAA,MACJ;AAAA,IACD;AAEA,QAAK,OAAQ;AAEZ,cAAQ;AAAA,QACP;AAAA,QACA;AAAA,MACD;AAAA,IACD;AACA,WAAO;AAAA,MACN,OAAO;AAAA,MACP,SAAS;AAAA,MACT,UAAU;AAAA,IACX;AAAA,EACD;AAMA,MAAI,SAAS,UAAU,MAAM;AAC7B,MAAI,UAAU;AAEd,MAAI,oBAAoB;AACxB,MAAI,iBAAiB;AAErB,WACK,IAAI,GACR,IAAI,6CACJ,KAAK,IAAK,UAAU,MAAO,IAAI,oCAC/B,KACC;AACD,QAAI,QAAS,SAAS,WAAY;AAClC,QAAI,OAAO,KAAK,MAAM;AAEtB,QAAK,oBAAqB;AACzB,OAAE,EAAE,GAAG,MAAM,GAAG,KAAK,QAAI;AAAA,QACxB;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAEA,UAAM,eAAW;AAAA,MAChB,IAAI,eAAAA,QAAO,SAAS,CAAE,MAAM,MAAM,KAAK,MAAM,CAAE,CAAE;AAAA,IAClD;AACA,UAAM,kBAAc,sCAAmB,WAAW,QAAS;AAE3D,QAAK,eAAe,QAAS;AAC5B,gBAAU;AAGV,0BAAoB;AACpB,uBAAiB;AAAA,IAClB,OAAO;AACN,eAAS;AAAA,IACV;AAAA,EACD;AAEA,SAAO;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,IACT,UAAU;AAAA,EACX;AACD;",
6
+ "names": ["Color"]
7
+ }