@atlaskit/tokens 1.11.3 → 1.12.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 (102) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/artifacts/atlassian-dark-token-value-for-contrast-check.js +24 -0
  3. package/dist/cjs/artifacts/atlassian-light-token-value-for-contrast-check.js +24 -0
  4. package/dist/cjs/constants.js +3 -1
  5. package/dist/cjs/custom-theme.js +108 -0
  6. package/dist/cjs/get-token-value.js +1 -1
  7. package/dist/cjs/get-token.js +1 -1
  8. package/dist/cjs/set-global-theme.js +156 -59
  9. package/dist/cjs/utils/color-utils.js +178 -0
  10. package/dist/cjs/utils/custom-theme-loading-utils.js +47 -0
  11. package/dist/cjs/utils/custom-theme-token-contrast-check.js +74 -0
  12. package/dist/cjs/utils/generate-custom-color-ramp.js +213 -0
  13. package/dist/cjs/utils/hash.js +17 -0
  14. package/dist/cjs/utils/hct-color-utils/color-utils.js +310 -0
  15. package/dist/cjs/utils/hct-color-utils/contrast.js +188 -0
  16. package/dist/cjs/utils/hct-color-utils/hct.js +1036 -0
  17. package/dist/cjs/utils/hct-color-utils/index.js +32 -0
  18. package/dist/cjs/utils/hct-color-utils/math-utils.js +159 -0
  19. package/dist/cjs/utils/theme-loading.js +1 -1
  20. package/dist/cjs/utils/theme-state-transformer.js +1 -1
  21. package/dist/cjs/version.json +1 -1
  22. package/dist/es2019/artifacts/atlassian-dark-token-value-for-contrast-check.js +17 -0
  23. package/dist/es2019/artifacts/atlassian-light-token-value-for-contrast-check.js +17 -0
  24. package/dist/es2019/constants.js +1 -0
  25. package/dist/es2019/custom-theme.js +77 -0
  26. package/dist/es2019/get-token-value.js +1 -1
  27. package/dist/es2019/get-token.js +1 -1
  28. package/dist/es2019/set-global-theme.js +67 -13
  29. package/dist/es2019/utils/color-utils.js +154 -0
  30. package/dist/es2019/utils/custom-theme-loading-utils.js +31 -0
  31. package/dist/es2019/utils/custom-theme-token-contrast-check.js +68 -0
  32. package/dist/es2019/utils/generate-custom-color-ramp.js +187 -0
  33. package/dist/es2019/utils/hash.js +10 -0
  34. package/dist/es2019/utils/hct-color-utils/color-utils.js +286 -0
  35. package/dist/es2019/utils/hct-color-utils/contrast.js +161 -0
  36. package/dist/es2019/utils/hct-color-utils/hct.js +931 -0
  37. package/dist/es2019/utils/hct-color-utils/index.js +3 -0
  38. package/dist/es2019/utils/hct-color-utils/math-utils.js +145 -0
  39. package/dist/es2019/utils/theme-loading.js +2 -2
  40. package/dist/es2019/utils/theme-state-transformer.js +3 -1
  41. package/dist/es2019/version.json +1 -1
  42. package/dist/esm/artifacts/atlassian-dark-token-value-for-contrast-check.js +17 -0
  43. package/dist/esm/artifacts/atlassian-light-token-value-for-contrast-check.js +17 -0
  44. package/dist/esm/constants.js +1 -0
  45. package/dist/esm/custom-theme.js +98 -0
  46. package/dist/esm/get-token-value.js +1 -1
  47. package/dist/esm/get-token.js +1 -1
  48. package/dist/esm/set-global-theme.js +149 -60
  49. package/dist/esm/utils/color-utils.js +162 -0
  50. package/dist/esm/utils/custom-theme-loading-utils.js +38 -0
  51. package/dist/esm/utils/custom-theme-token-contrast-check.js +65 -0
  52. package/dist/esm/utils/generate-custom-color-ramp.js +202 -0
  53. package/dist/esm/utils/hash.js +10 -0
  54. package/dist/esm/utils/hct-color-utils/color-utils.js +285 -0
  55. package/dist/esm/utils/hct-color-utils/contrast.js +181 -0
  56. package/dist/esm/utils/hct-color-utils/hct.js +1029 -0
  57. package/dist/esm/utils/hct-color-utils/index.js +3 -0
  58. package/dist/esm/utils/hct-color-utils/math-utils.js +145 -0
  59. package/dist/esm/utils/theme-loading.js +2 -2
  60. package/dist/esm/utils/theme-state-transformer.js +1 -1
  61. package/dist/esm/version.json +1 -1
  62. package/dist/types/artifacts/atlassian-dark-token-value-for-contrast-check.d.ts +17 -0
  63. package/dist/types/artifacts/atlassian-light-token-value-for-contrast-check.d.ts +17 -0
  64. package/dist/types/constants.d.ts +1 -0
  65. package/dist/types/custom-theme.d.ts +30 -0
  66. package/dist/types/index.d.ts +1 -0
  67. package/dist/types/set-global-theme.d.ts +9 -3
  68. package/dist/types/tokens/atlassian-dark/utility/utility.d.ts +1 -1
  69. package/dist/types/tokens/atlassian-light/utility/utility.d.ts +1 -1
  70. package/dist/types/tokens/default/utility/utility.d.ts +1 -1
  71. package/dist/types/utils/color-utils.d.ts +10 -0
  72. package/dist/types/utils/custom-theme-loading-utils.d.ts +11 -0
  73. package/dist/types/utils/custom-theme-token-contrast-check.d.ts +20 -0
  74. package/dist/types/utils/generate-custom-color-ramp.d.ts +19 -0
  75. package/dist/types/utils/hash.d.ts +1 -0
  76. package/dist/types/utils/hct-color-utils/color-utils.d.ts +131 -0
  77. package/dist/types/utils/hct-color-utils/contrast.d.ts +78 -0
  78. package/dist/types/utils/hct-color-utils/hct.d.ts +137 -0
  79. package/dist/types/utils/hct-color-utils/index.d.ts +3 -0
  80. package/dist/types/utils/hct-color-utils/math-utils.d.ts +86 -0
  81. package/dist/types-ts4.5/artifacts/atlassian-dark-token-value-for-contrast-check.d.ts +17 -0
  82. package/dist/types-ts4.5/artifacts/atlassian-light-token-value-for-contrast-check.d.ts +17 -0
  83. package/dist/types-ts4.5/constants.d.ts +1 -0
  84. package/dist/types-ts4.5/custom-theme.d.ts +30 -0
  85. package/dist/types-ts4.5/index.d.ts +1 -0
  86. package/dist/types-ts4.5/set-global-theme.d.ts +9 -3
  87. package/dist/types-ts4.5/tokens/atlassian-dark/utility/utility.d.ts +1 -1
  88. package/dist/types-ts4.5/tokens/atlassian-light/utility/utility.d.ts +1 -1
  89. package/dist/types-ts4.5/tokens/default/utility/utility.d.ts +1 -1
  90. package/dist/types-ts4.5/utils/color-utils.d.ts +27 -0
  91. package/dist/types-ts4.5/utils/custom-theme-loading-utils.d.ts +11 -0
  92. package/dist/types-ts4.5/utils/custom-theme-token-contrast-check.d.ts +20 -0
  93. package/dist/types-ts4.5/utils/generate-custom-color-ramp.d.ts +19 -0
  94. package/dist/types-ts4.5/utils/hash.d.ts +1 -0
  95. package/dist/types-ts4.5/utils/hct-color-utils/color-utils.d.ts +131 -0
  96. package/dist/types-ts4.5/utils/hct-color-utils/contrast.d.ts +78 -0
  97. package/dist/types-ts4.5/utils/hct-color-utils/hct.d.ts +137 -0
  98. package/dist/types-ts4.5/utils/hct-color-utils/index.d.ts +3 -0
  99. package/dist/types-ts4.5/utils/hct-color-utils/math-utils.d.ts +86 -0
  100. package/package.json +35 -36
  101. package/report.api.md +24 -1
  102. package/tmp/api-report-tmp.d.ts +0 -1132
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+
3
+ var _typeof = require("@babel/runtime/helpers/typeof");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.alphaFromArgb = alphaFromArgb;
8
+ exports.argbFromLinrgb = argbFromLinrgb;
9
+ exports.argbFromLstar = argbFromLstar;
10
+ exports.argbFromRgb = argbFromRgb;
11
+ exports.argbFromRgba = argbFromRgba;
12
+ exports.argbFromXyz = argbFromXyz;
13
+ exports.blueFromArgb = blueFromArgb;
14
+ exports.delinearized = delinearized;
15
+ exports.greenFromArgb = greenFromArgb;
16
+ exports.isOpaque = isOpaque;
17
+ exports.linearized = linearized;
18
+ exports.lstarFromArgb = lstarFromArgb;
19
+ exports.lstarFromY = lstarFromY;
20
+ exports.redFromArgb = redFromArgb;
21
+ exports.rgbaFromArgb = rgbaFromArgb;
22
+ exports.whitePointD65 = whitePointD65;
23
+ exports.xyzFromArgb = xyzFromArgb;
24
+ exports.yFromLstar = yFromLstar;
25
+ var mathUtils = _interopRequireWildcard(require("./math-utils"));
26
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
27
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
28
+ /**
29
+ * Below lines are copied from @material/material-color-utilities.
30
+ * Do not modify it.
31
+ */
32
+
33
+ /**
34
+ * @license
35
+ * Copyright 2021 Google LLC
36
+ *
37
+ * Licensed under the Apache License, Version 2.0 (the "License");
38
+ * you may not use this file except in compliance with the License.
39
+ * You may obtain a copy of the License at
40
+ *
41
+ * http://www.apache.org/licenses/LICENSE-2.0
42
+ *
43
+ * Unless required by applicable law or agreed to in writing, software
44
+ * distributed under the License is distributed on an "AS IS" BASIS,
45
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46
+ * See the License for the specific language governing permissions and
47
+ * limitations under the License.
48
+ */
49
+
50
+ // This file is automatically generated. Do not modify it.
51
+
52
+ /**
53
+ * Color science utilities.
54
+ *
55
+ * Utility methods for color science constants and color space
56
+ * conversions that aren't HCT or CAM16.
57
+ */
58
+
59
+ var SRGB_TO_XYZ = [[0.41233895, 0.35762064, 0.18051042], [0.2126, 0.7152, 0.0722], [0.01932141, 0.11916382, 0.95034478]];
60
+ var XYZ_TO_SRGB = [[3.2413774792388685, -1.5376652402851851, -0.49885366846268053], [-0.9691452513005321, 1.8758853451067872, 0.04156585616912061], [0.05562093689691305, -0.20395524564742123, 1.0571799111220335]];
61
+ var WHITE_POINT_D65 = [95.047, 100.0, 108.883];
62
+
63
+ /**
64
+ * Converts a color from RGB components to ARGB format.
65
+ */
66
+ function argbFromRgb(red, green, blue) {
67
+ return (255 << 24 | (red & 255) << 16 | (green & 255) << 8 | blue & 255) >>> 0;
68
+ }
69
+
70
+ /**
71
+ * Converts a color from linear RGB components to ARGB format.
72
+ */
73
+ function argbFromLinrgb(linrgb) {
74
+ var r = delinearized(linrgb[0]);
75
+ var g = delinearized(linrgb[1]);
76
+ var b = delinearized(linrgb[2]);
77
+ return argbFromRgb(r, g, b);
78
+ }
79
+
80
+ /**
81
+ * Returns the alpha component of a color in ARGB format.
82
+ */
83
+ function alphaFromArgb(argb) {
84
+ return argb >> 24 & 255;
85
+ }
86
+
87
+ /**
88
+ * Returns the red component of a color in ARGB format.
89
+ */
90
+ function redFromArgb(argb) {
91
+ return argb >> 16 & 255;
92
+ }
93
+
94
+ /**
95
+ * Returns the green component of a color in ARGB format.
96
+ */
97
+ function greenFromArgb(argb) {
98
+ return argb >> 8 & 255;
99
+ }
100
+
101
+ /**
102
+ * Returns the blue component of a color in ARGB format.
103
+ */
104
+ function blueFromArgb(argb) {
105
+ return argb & 255;
106
+ }
107
+
108
+ /**
109
+ * Returns whether a color in ARGB format is opaque.
110
+ */
111
+ function isOpaque(argb) {
112
+ return alphaFromArgb(argb) >= 255;
113
+ }
114
+
115
+ /**
116
+ * Converts a color from ARGB to XYZ.
117
+ */
118
+ function argbFromXyz(x, y, z) {
119
+ var matrix = XYZ_TO_SRGB;
120
+ var linearR = matrix[0][0] * x + matrix[0][1] * y + matrix[0][2] * z;
121
+ var linearG = matrix[1][0] * x + matrix[1][1] * y + matrix[1][2] * z;
122
+ var linearB = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z;
123
+ var r = delinearized(linearR);
124
+ var g = delinearized(linearG);
125
+ var b = delinearized(linearB);
126
+ return argbFromRgb(r, g, b);
127
+ }
128
+
129
+ /**
130
+ * Converts a color from XYZ to ARGB.
131
+ */
132
+ function xyzFromArgb(argb) {
133
+ var r = linearized(redFromArgb(argb));
134
+ var g = linearized(greenFromArgb(argb));
135
+ var b = linearized(blueFromArgb(argb));
136
+ return mathUtils.matrixMultiply([r, g, b], SRGB_TO_XYZ);
137
+ }
138
+
139
+ /**
140
+ * Converts an L* value to an ARGB representation.
141
+ *
142
+ * @param lstar L* in L*a*b*
143
+ * @return ARGB representation of grayscale color with lightness
144
+ * matching L*
145
+ */
146
+ function argbFromLstar(lstar) {
147
+ var y = yFromLstar(lstar);
148
+ var component = delinearized(y);
149
+ return argbFromRgb(component, component, component);
150
+ }
151
+
152
+ /**
153
+ * Computes the L* value of a color in ARGB representation.
154
+ *
155
+ * @param argb ARGB representation of a color
156
+ * @return L*, from L*a*b*, coordinate of the color
157
+ */
158
+ function lstarFromArgb(argb) {
159
+ var y = xyzFromArgb(argb)[1];
160
+ return 116.0 * labF(y / 100.0) - 16.0;
161
+ }
162
+
163
+ /**
164
+ * Converts an L* value to a Y value.
165
+ *
166
+ * L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
167
+ *
168
+ * L* measures perceptual luminance, a linear scale. Y in XYZ
169
+ * measures relative luminance, a logarithmic scale.
170
+ *
171
+ * @param lstar L* in L*a*b*
172
+ * @return Y in XYZ
173
+ */
174
+ function yFromLstar(lstar) {
175
+ return 100.0 * labInvf((lstar + 16.0) / 116.0);
176
+ }
177
+
178
+ /**
179
+ * Converts a Y value to an L* value.
180
+ *
181
+ * L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
182
+ *
183
+ * L* measures perceptual luminance, a linear scale. Y in XYZ
184
+ * measures relative luminance, a logarithmic scale.
185
+ *
186
+ * @param y Y in XYZ
187
+ * @return L* in L*a*b*
188
+ */
189
+ function lstarFromY(y) {
190
+ return labF(y / 100.0) * 116.0 - 16.0;
191
+ }
192
+
193
+ /**
194
+ * Linearizes an RGB component.
195
+ *
196
+ * @param rgbComponent 0 <= rgb_component <= 255, represents R/G/B
197
+ * channel
198
+ * @return 0.0 <= output <= 100.0, color channel converted to
199
+ * linear RGB space
200
+ */
201
+ function linearized(rgbComponent) {
202
+ var normalized = rgbComponent / 255.0;
203
+ if (normalized <= 0.040449936) {
204
+ return normalized / 12.92 * 100.0;
205
+ } else {
206
+ return Math.pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Delinearizes an RGB component.
212
+ *
213
+ * @param rgbComponent 0.0 <= rgb_component <= 100.0, represents
214
+ * linear R/G/B channel
215
+ * @return 0 <= output <= 255, color channel converted to regular
216
+ * RGB space
217
+ */
218
+ function delinearized(rgbComponent) {
219
+ var normalized = rgbComponent / 100.0;
220
+ var delinearized = 0.0;
221
+ if (normalized <= 0.0031308) {
222
+ delinearized = normalized * 12.92;
223
+ } else {
224
+ delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
225
+ }
226
+ return mathUtils.clampInt(0, 255, Math.round(delinearized * 255.0));
227
+ }
228
+
229
+ /**
230
+ * Returns the standard white point; white on a sunny day.
231
+ *
232
+ * @return The white point
233
+ */
234
+ function whitePointD65() {
235
+ return WHITE_POINT_D65;
236
+ }
237
+
238
+ /**
239
+ * RGBA component
240
+ *
241
+ * @param r Red value should be between 0-255
242
+ * @param g Green value should be between 0-255
243
+ * @param b Blue value should be between 0-255
244
+ * @param a Alpha value should be between 0-255
245
+ */
246
+
247
+ /**
248
+ * Return RGBA from a given int32 color
249
+ *
250
+ * @param argb ARGB representation of a int32 color.
251
+ * @return RGBA representation of a int32 color.
252
+ */
253
+ function rgbaFromArgb(argb) {
254
+ var r = redFromArgb(argb);
255
+ var g = greenFromArgb(argb);
256
+ var b = blueFromArgb(argb);
257
+ var a = alphaFromArgb(argb);
258
+ return {
259
+ r: r,
260
+ g: g,
261
+ b: b,
262
+ a: a
263
+ };
264
+ }
265
+
266
+ /**
267
+ * Return int32 color from a given RGBA component
268
+ *
269
+ * @param rgba RGBA representation of a int32 color.
270
+ * @returns ARGB representation of a int32 color.
271
+ */
272
+ function argbFromRgba(_ref) {
273
+ var r = _ref.r,
274
+ g = _ref.g,
275
+ b = _ref.b,
276
+ a = _ref.a;
277
+ var rValue = clampComponent(r);
278
+ var gValue = clampComponent(g);
279
+ var bValue = clampComponent(b);
280
+ var aValue = clampComponent(a);
281
+ return aValue << 24 | rValue << 16 | gValue << 8 | bValue;
282
+ }
283
+ function clampComponent(value) {
284
+ if (value < 0) {
285
+ return 0;
286
+ }
287
+ if (value > 255) {
288
+ return 255;
289
+ }
290
+ return value;
291
+ }
292
+ function labF(t) {
293
+ var e = 216.0 / 24389.0;
294
+ var kappa = 24389.0 / 27.0;
295
+ if (t > e) {
296
+ return Math.pow(t, 1.0 / 3.0);
297
+ } else {
298
+ return (kappa * t + 16) / 116;
299
+ }
300
+ }
301
+ function labInvf(ft) {
302
+ var e = 216.0 / 24389.0;
303
+ var kappa = 24389.0 / 27.0;
304
+ var ft3 = ft * ft * ft;
305
+ if (ft3 > e) {
306
+ return ft3;
307
+ } else {
308
+ return (116 * ft - 16) / kappa;
309
+ }
310
+ }
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Contrast = void 0;
9
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
+ var utils = _interopRequireWildcard(require("./color-utils"));
12
+ var math = _interopRequireWildcard(require("./math-utils"));
13
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+ /**
16
+ * Below lines are copied from @material/material-color-utilities.
17
+ * Do not modify it.
18
+ */
19
+ /**
20
+ * @license
21
+ * Copyright 2022 Google LLC
22
+ *
23
+ * Licensed under the Apache License, Version 2.0 (the "License");
24
+ * you may not use this file except in compliance with the License.
25
+ * You may obtain a copy of the License at
26
+ *
27
+ * http://www.apache.org/licenses/LICENSE-2.0
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ // material_color_utilities is designed to have a consistent API across
36
+ // platforms and modular components that can be moved around easily. Using a
37
+ // class as a namespace facilitates this.
38
+ //
39
+ // tslint:disable:class-as-namespace
40
+ /**
41
+ * Utility methods for calculating contrast given two colors, or calculating a
42
+ * color given one color and a contrast ratio.
43
+ *
44
+ * Contrast ratio is calculated using XYZ's Y. When linearized to match human
45
+ * perception, Y becomes HCT's tone and L*a*b*'s' L*. Informally, this is the
46
+ * lightness of a color.
47
+ *
48
+ * Methods refer to tone, T in the the HCT color space.
49
+ * Tone is equivalent to L* in the L*a*b* color space, or L in the LCH color
50
+ * space.
51
+ */
52
+ var Contrast = /*#__PURE__*/function () {
53
+ function Contrast() {
54
+ (0, _classCallCheck2.default)(this, Contrast);
55
+ }
56
+ (0, _createClass2.default)(Contrast, null, [{
57
+ key: "ratioOfTones",
58
+ value:
59
+ /**
60
+ * Returns a contrast ratio, which ranges from 1 to 21.
61
+ *
62
+ * @param toneA Tone between 0 and 100. Values outside will be clamped.
63
+ * @param toneB Tone between 0 and 100. Values outside will be clamped.
64
+ */
65
+ function ratioOfTones(toneA, toneB) {
66
+ toneA = math.clampDouble(0.0, 100.0, toneA);
67
+ toneB = math.clampDouble(0.0, 100.0, toneB);
68
+ return Contrast.ratioOfYs(utils.yFromLstar(toneA), utils.yFromLstar(toneB));
69
+ }
70
+ }, {
71
+ key: "ratioOfYs",
72
+ value: function ratioOfYs(y1, y2) {
73
+ var lighter = y1 > y2 ? y1 : y2;
74
+ var darker = lighter === y2 ? y1 : y2;
75
+ return (lighter + 5.0) / (darker + 5.0);
76
+ }
77
+
78
+ /**
79
+ * Returns a tone >= tone parameter that ensures ratio parameter.
80
+ * Return value is between 0 and 100.
81
+ * Returns -1 if ratio cannot be achieved with tone parameter.
82
+ *
83
+ * @param tone Tone return value must contrast with.
84
+ * Range is 0 to 100. Invalid values will result in -1 being returned.
85
+ * @param ratio Contrast ratio of return value and tone.
86
+ * Range is 1 to 21, invalid values have undefined behavior.
87
+ */
88
+ }, {
89
+ key: "lighter",
90
+ value: function lighter(tone, ratio) {
91
+ if (tone < 0.0 || tone > 100.0) {
92
+ return -1.0;
93
+ }
94
+ var darkY = utils.yFromLstar(tone);
95
+ var lightY = ratio * (darkY + 5.0) - 5.0;
96
+ var realContrast = Contrast.ratioOfYs(lightY, darkY);
97
+ var delta = Math.abs(realContrast - ratio);
98
+ if (realContrast < ratio && delta > 0.04) {
99
+ return -1;
100
+ }
101
+
102
+ // Ensure gamut mapping, which requires a 'range' on tone, will still result
103
+ // the correct ratio by darkening slightly.
104
+ var returnValue = utils.lstarFromY(lightY) + 0.4;
105
+ if (returnValue < 0 || returnValue > 100) {
106
+ return -1;
107
+ }
108
+ return returnValue;
109
+ }
110
+
111
+ /**
112
+ * Returns a tone <= tone parameter that ensures ratio parameter.
113
+ * Return value is between 0 and 100.
114
+ * Returns -1 if ratio cannot be achieved with tone parameter.
115
+ *
116
+ * @param tone Tone return value must contrast with.
117
+ * Range is 0 to 100. Invalid values will result in -1 being returned.
118
+ * @param ratio Contrast ratio of return value and tone.
119
+ * Range is 1 to 21, invalid values have undefined behavior.
120
+ */
121
+ }, {
122
+ key: "darker",
123
+ value: function darker(tone, ratio) {
124
+ if (tone < 0.0 || tone > 100.0) {
125
+ return -1.0;
126
+ }
127
+ var lightY = utils.yFromLstar(tone);
128
+ var darkY = (lightY + 5.0) / ratio - 5.0;
129
+ var realContrast = Contrast.ratioOfYs(lightY, darkY);
130
+ var delta = Math.abs(realContrast - ratio);
131
+ if (realContrast < ratio && delta > 0.04) {
132
+ return -1;
133
+ }
134
+
135
+ // Ensure gamut mapping, which requires a 'range' on tone, will still result
136
+ // the correct ratio by darkening slightly.
137
+ var returnValue = utils.lstarFromY(darkY) - 0.4;
138
+ if (returnValue < 0 || returnValue > 100) {
139
+ return -1;
140
+ }
141
+ return returnValue;
142
+ }
143
+
144
+ /**
145
+ * Returns a tone >= tone parameter that ensures ratio parameter.
146
+ * Return value is between 0 and 100.
147
+ * Returns 100 if ratio cannot be achieved with tone parameter.
148
+ *
149
+ * This method is unsafe because the returned value is guaranteed to be in
150
+ * bounds for tone, i.e. between 0 and 100. However, that value may not reach
151
+ * the ratio with tone. For example, there is no color lighter than T100.
152
+ *
153
+ * @param tone Tone return value must contrast with.
154
+ * Range is 0 to 100. Invalid values will result in 100 being returned.
155
+ * @param ratio Desired contrast ratio of return value and tone parameter.
156
+ * Range is 1 to 21, invalid values have undefined behavior.
157
+ */
158
+ }, {
159
+ key: "lighterUnsafe",
160
+ value: function lighterUnsafe(tone, ratio) {
161
+ var lighterSafe = Contrast.lighter(tone, ratio);
162
+ return lighterSafe < 0.0 ? 100.0 : lighterSafe;
163
+ }
164
+
165
+ /**
166
+ * Returns a tone >= tone parameter that ensures ratio parameter.
167
+ * Return value is between 0 and 100.
168
+ * Returns 100 if ratio cannot be achieved with tone parameter.
169
+ *
170
+ * This method is unsafe because the returned value is guaranteed to be in
171
+ * bounds for tone, i.e. between 0 and 100. However, that value may not reach
172
+ * the [ratio with [tone]. For example, there is no color darker than T0.
173
+ *
174
+ * @param tone Tone return value must contrast with.
175
+ * Range is 0 to 100. Invalid values will result in 0 being returned.
176
+ * @param ratio Desired contrast ratio of return value and tone parameter.
177
+ * Range is 1 to 21, invalid values have undefined behavior.
178
+ */
179
+ }, {
180
+ key: "darkerUnsafe",
181
+ value: function darkerUnsafe(tone, ratio) {
182
+ var darkerSafe = Contrast.darker(tone, ratio);
183
+ return darkerSafe < 0.0 ? 0.0 : darkerSafe;
184
+ }
185
+ }]);
186
+ return Contrast;
187
+ }();
188
+ exports.Contrast = Contrast;