material 3.7.2 → 3.8.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 (313) hide show
  1. package/NOTICE +223 -0
  2. package/README.md +5 -3
  3. package/dist/components/extended-fab/types.d.ts +3 -0
  4. package/dist/components/fab/types.d.ts +3 -0
  5. package/dist/components/slider/features/controller.js +24 -8
  6. package/dist/elements/css/card.css +1 -1
  7. package/dist/elements/css/card.js +1 -1
  8. package/dist/elements/css/dialog.css +1 -1
  9. package/dist/elements/css/dialog.js +1 -1
  10. package/dist/elements/css/menu.css +1 -1
  11. package/dist/elements/css/menu.js +1 -1
  12. package/dist/elements/css/select.css +1 -1
  13. package/dist/elements/css/select.js +1 -1
  14. package/dist/elements/css/slider.css +1 -1
  15. package/dist/elements/css/slider.js +1 -1
  16. package/dist/elements/css/tabs.css +1 -1
  17. package/dist/elements/css/tabs.js +1 -1
  18. package/dist/elements/css/timepicker.css +1 -1
  19. package/dist/elements/css/timepicker.js +1 -1
  20. package/dist/elements/extended-fab.d.ts +1 -0
  21. package/dist/elements/fab.d.ts +1 -0
  22. package/dist/elements/preupgrade/badge.css +1 -1
  23. package/dist/elements/preupgrade/bottom-app-bar.css +1 -1
  24. package/dist/elements/preupgrade/bottom-sheet.css +1 -1
  25. package/dist/elements/preupgrade/button-group.css +1 -1
  26. package/dist/elements/preupgrade/button.css +1 -1
  27. package/dist/elements/preupgrade/card.css +1 -1
  28. package/dist/elements/preupgrade/carousel.css +1 -1
  29. package/dist/elements/preupgrade/checkbox.css +1 -1
  30. package/dist/elements/preupgrade/chips.css +1 -1
  31. package/dist/elements/preupgrade/datepicker.css +1 -1
  32. package/dist/elements/preupgrade/dialog.css +1 -1
  33. package/dist/elements/preupgrade/divider.css +1 -1
  34. package/dist/elements/preupgrade/drawer.css +1 -1
  35. package/dist/elements/preupgrade/extended-fab.css +1 -1
  36. package/dist/elements/preupgrade/fab-menu.css +1 -1
  37. package/dist/elements/preupgrade/fab.css +1 -1
  38. package/dist/elements/preupgrade/icon-button.css +1 -1
  39. package/dist/elements/preupgrade/list.css +1 -1
  40. package/dist/elements/preupgrade/loading-indicator.css +1 -1
  41. package/dist/elements/preupgrade/menu.css +1 -1
  42. package/dist/elements/preupgrade/navigation-bar.css +1 -1
  43. package/dist/elements/preupgrade/navigation-rail.css +1 -1
  44. package/dist/elements/preupgrade/progress.css +1 -1
  45. package/dist/elements/preupgrade/radios.css +1 -1
  46. package/dist/elements/preupgrade/search.css +1 -1
  47. package/dist/elements/preupgrade/select.css +1 -1
  48. package/dist/elements/preupgrade/side-sheet.css +1 -1
  49. package/dist/elements/preupgrade/slider.css +1 -1
  50. package/dist/elements/preupgrade/snackbar.css +1 -1
  51. package/dist/elements/preupgrade/split-button.css +1 -1
  52. package/dist/elements/preupgrade/switch.css +1 -1
  53. package/dist/elements/preupgrade/tabs.css +1 -1
  54. package/dist/elements/preupgrade/text-field.css +1 -1
  55. package/dist/elements/preupgrade/timepicker.css +1 -1
  56. package/dist/elements/preupgrade/toolbar.css +1 -1
  57. package/dist/elements/preupgrade/tooltip.css +1 -1
  58. package/dist/elements/preupgrade/top-app-bar.css +1 -1
  59. package/dist/elements/preupgrade.css +1 -1
  60. package/dist/styles/badge.css +1 -1
  61. package/dist/styles/base-lite.css +2 -2
  62. package/dist/styles/base.css +2 -2
  63. package/dist/styles/bottom-app-bar.css +1 -1
  64. package/dist/styles/bottom-sheet.css +1 -1
  65. package/dist/styles/button-group.css +1 -1
  66. package/dist/styles/button.css +1 -1
  67. package/dist/styles/card.css +2 -2
  68. package/dist/styles/carousel.css +1 -1
  69. package/dist/styles/checkbox.css +1 -1
  70. package/dist/styles/chips.css +1 -1
  71. package/dist/styles/contrast.css +2 -2
  72. package/dist/styles/datepicker.css +1 -1
  73. package/dist/styles/dialog.css +2 -2
  74. package/dist/styles/divider.css +1 -1
  75. package/dist/styles/drawer.css +1 -1
  76. package/dist/styles/extended-fab.css +1 -1
  77. package/dist/styles/fab-menu.css +1 -1
  78. package/dist/styles/fab.css +1 -1
  79. package/dist/styles/icon-button.css +1 -1
  80. package/dist/styles/list.css +1 -1
  81. package/dist/styles/loading-indicator.css +1 -1
  82. package/dist/styles/menu.css +2 -2
  83. package/dist/styles/navigation-bar.css +1 -1
  84. package/dist/styles/navigation-rail.css +1 -1
  85. package/dist/styles/progress.css +1 -1
  86. package/dist/styles/radios.css +1 -1
  87. package/dist/styles/search.css +1 -1
  88. package/dist/styles/select.css +2 -2
  89. package/dist/styles/side-sheet.css +1 -1
  90. package/dist/styles/slider.css +2 -2
  91. package/dist/styles/snackbar.css +1 -1
  92. package/dist/styles/split-button.css +1 -1
  93. package/dist/styles/switch.css +1 -1
  94. package/dist/styles/tabs.css +2 -2
  95. package/dist/styles/text-field.css +1 -1
  96. package/dist/styles/timepicker.css +2 -2
  97. package/dist/styles/toolbar.css +1 -1
  98. package/dist/styles/tooltip.css +1 -1
  99. package/dist/styles/top-app-bar.css +1 -1
  100. package/dist/styles/typography.css +1 -1
  101. package/dist/styles/utilities.css +1 -1
  102. package/dist/styles.css +2 -2
  103. package/dist/theme/image.d.ts +2 -0
  104. package/dist/theme/image.js +5 -0
  105. package/dist/theme/index.d.ts +23 -0
  106. package/dist/theme/index.js +32 -0
  107. package/dist/theme/roles.js +68 -0
  108. package/dist/theme/variants.d.ts +9 -0
  109. package/dist/theme/variants.js +17 -0
  110. package/dist/theme/vendor/contrast/contrast.js +72 -0
  111. package/dist/theme/vendor/dislike/dislike_analyzer.js +31 -0
  112. package/dist/theme/vendor/dynamiccolor/color_spec_2021.d.ts +63 -0
  113. package/dist/theme/vendor/dynamiccolor/color_spec_2021.js +663 -0
  114. package/dist/theme/vendor/dynamiccolor/contrast_curve.js +42 -0
  115. package/dist/theme/vendor/dynamiccolor/dynamic_color.d.ts +24 -0
  116. package/dist/theme/vendor/dynamiccolor/dynamic_color.js +264 -0
  117. package/dist/theme/vendor/dynamiccolor/dynamic_scheme.d.ts +87 -0
  118. package/dist/theme/vendor/dynamiccolor/dynamic_scheme.js +418 -0
  119. package/dist/theme/vendor/dynamiccolor/material_dynamic_colors.d.ts +124 -0
  120. package/dist/theme/vendor/dynamiccolor/material_dynamic_colors.js +311 -0
  121. package/dist/theme/vendor/dynamiccolor/tone_delta_pair.js +27 -0
  122. package/dist/theme/vendor/dynamiccolor/variant.js +28 -0
  123. package/dist/theme/vendor/hct/cam16.js +249 -0
  124. package/dist/theme/vendor/hct/hct.d.ts +23 -0
  125. package/dist/theme/vendor/hct/hct.js +86 -0
  126. package/dist/theme/vendor/hct/hct_solver.js +390 -0
  127. package/dist/theme/vendor/hct/viewing_conditions.js +73 -0
  128. package/dist/theme/vendor/palettes/tonal_palette.js +114 -0
  129. package/dist/theme/vendor/quantize/lab_point_provider.js +31 -0
  130. package/dist/theme/vendor/quantize/quantizer_celebi.js +25 -0
  131. package/dist/theme/vendor/quantize/quantizer_map.js +31 -0
  132. package/dist/theme/vendor/quantize/quantizer_wsmeans.js +178 -0
  133. package/dist/theme/vendor/quantize/quantizer_wu.js +342 -0
  134. package/dist/theme/vendor/scheme/scheme_content.js +30 -0
  135. package/dist/theme/vendor/scheme/scheme_expressive.js +30 -0
  136. package/dist/theme/vendor/scheme/scheme_fidelity.js +30 -0
  137. package/dist/theme/vendor/scheme/scheme_monochrome.js +30 -0
  138. package/dist/theme/vendor/scheme/scheme_neutral.js +30 -0
  139. package/dist/theme/vendor/scheme/scheme_tonal_spot.js +30 -0
  140. package/dist/theme/vendor/scheme/scheme_vibrant.js +30 -0
  141. package/dist/theme/vendor/score/score.js +100 -0
  142. package/dist/theme/vendor/temperature/temperature_cache.js +198 -0
  143. package/dist/theme/vendor/utils/color_utils.js +173 -0
  144. package/dist/theme/vendor/utils/image_utils.js +72 -0
  145. package/dist/theme/vendor/utils/math_utils.js +75 -0
  146. package/dist/theme/vendor/utils/string_utils.js +61 -0
  147. package/dist/themes/autumn-contrast.css +2 -2
  148. package/dist/themes/autumn.css +2 -2
  149. package/dist/themes/baseline-contrast.css +2 -2
  150. package/dist/themes/baseline.css +2 -2
  151. package/dist/themes/brownbeige-contrast.css +2 -2
  152. package/dist/themes/brownbeige.css +2 -2
  153. package/dist/themes/content-contrast.css +2 -2
  154. package/dist/themes/content.css +2 -2
  155. package/dist/themes/desert-contrast.css +2 -2
  156. package/dist/themes/desert.css +2 -2
  157. package/dist/themes/expressive-contrast.css +2 -2
  158. package/dist/themes/expressive.css +2 -2
  159. package/dist/themes/fidelity-contrast.css +2 -2
  160. package/dist/themes/fidelity.css +2 -2
  161. package/dist/themes/forest-contrast.css +2 -2
  162. package/dist/themes/forest.css +2 -2
  163. package/dist/themes/fruit-salad-contrast.css +2 -2
  164. package/dist/themes/fruit-salad.css +2 -2
  165. package/dist/themes/highcontrast-contrast.css +2 -2
  166. package/dist/themes/highcontrast.css +2 -2
  167. package/dist/themes/monochrome-contrast.css +2 -2
  168. package/dist/themes/monochrome.css +2 -2
  169. package/dist/themes/neutral-contrast.css +2 -2
  170. package/dist/themes/neutral.css +2 -2
  171. package/dist/themes/ocean-contrast.css +2 -2
  172. package/dist/themes/ocean.css +2 -2
  173. package/dist/themes/rainbow-contrast.css +2 -2
  174. package/dist/themes/rainbow.css +2 -2
  175. package/dist/themes/sageivory-contrast.css +2 -2
  176. package/dist/themes/sageivory.css +2 -2
  177. package/dist/themes/spring-contrast.css +2 -2
  178. package/dist/themes/spring.css +2 -2
  179. package/dist/themes/summer-contrast.css +2 -2
  180. package/dist/themes/summer.css +2 -2
  181. package/dist/themes/sunset-contrast.css +2 -2
  182. package/dist/themes/sunset.css +2 -2
  183. package/dist/themes/tealcaramel-contrast.css +2 -2
  184. package/dist/themes/tealcaramel.css +2 -2
  185. package/dist/themes/vibrant-contrast.css +2 -2
  186. package/dist/themes/vibrant.css +2 -2
  187. package/package.json +15 -1
  188. package/src/styles/abstract/_base.scss +0 -1
  189. package/src/styles/abstract/_direction.scss +121 -152
  190. package/src/styles/abstract/_emit.scss +7 -16
  191. package/src/styles/abstract/_floating-placement.scss +9 -10
  192. package/src/styles/abstract/_functions.scss +64 -79
  193. package/src/styles/abstract/_mixins.scss +355 -459
  194. package/src/styles/abstract/_theme.scss +122 -136
  195. package/src/styles/abstract/_variables.scss +320 -390
  196. package/src/styles/abstract/chips/_assist.scss +2 -3
  197. package/src/styles/abstract/chips/_filter.scss +2 -3
  198. package/src/styles/abstract/chips/_input.scss +2 -3
  199. package/src/styles/abstract/chips/_suggestion.scss +2 -3
  200. package/src/styles/base/_body-theme.scss +10 -15
  201. package/src/styles/base/_document.scss +10 -33
  202. package/src/styles/base/_reset.scss +20 -40
  203. package/src/styles/base/_tokens.scss +6 -21
  204. package/src/styles/base/_typescale-tokens.scss +12 -16
  205. package/src/styles/base/_typography.scss +28 -37
  206. package/src/styles/components/_badge.scss +171 -193
  207. package/src/styles/components/_bottom-app-bar.scss +72 -90
  208. package/src/styles/components/_bottom-sheet.scss +188 -232
  209. package/src/styles/components/_button-group.scss +140 -189
  210. package/src/styles/components/_button.scss +382 -478
  211. package/src/styles/components/_card.scss +349 -473
  212. package/src/styles/components/_carousel.scss +134 -156
  213. package/src/styles/components/_checkbox.scss +208 -249
  214. package/src/styles/components/_chips.scss +239 -304
  215. package/src/styles/components/_datepicker.scss +172 -222
  216. package/src/styles/components/_dialog.scss +428 -553
  217. package/src/styles/components/_divider.scss +37 -46
  218. package/src/styles/components/_drawer.scss +571 -678
  219. package/src/styles/components/_extended-fab.scss +263 -294
  220. package/src/styles/components/_fab-menu.scss +225 -267
  221. package/src/styles/components/_fab.scss +197 -218
  222. package/src/styles/components/_icon-button.scss +483 -588
  223. package/src/styles/components/_list.scss +115 -144
  224. package/src/styles/components/_loading-indicator.scss +23 -29
  225. package/src/styles/components/_menu.scss +543 -699
  226. package/src/styles/components/_navigation-bar.scss +189 -234
  227. package/src/styles/components/_navigation-rail.scss +256 -312
  228. package/src/styles/components/_progress.scss +60 -85
  229. package/src/styles/components/_radios.scss +170 -207
  230. package/src/styles/components/_search.scss +369 -455
  231. package/src/styles/components/_select.scss +78 -185
  232. package/src/styles/components/_side-sheet.scss +234 -292
  233. package/src/styles/components/_slider.scss +289 -367
  234. package/src/styles/components/_snackbar.scss +149 -198
  235. package/src/styles/components/_split-button.scss +139 -228
  236. package/src/styles/components/_switch.scss +282 -349
  237. package/src/styles/components/_tabs.scss +350 -547
  238. package/src/styles/components/_text-field.scss +788 -980
  239. package/src/styles/components/_timepicker.scss +435 -506
  240. package/src/styles/components/_toolbar.scss +178 -242
  241. package/src/styles/components/_tooltip.scss +152 -197
  242. package/src/styles/components/_top-app-bar.scss +206 -230
  243. package/src/styles/elements/_badge.scss +42 -61
  244. package/src/styles/elements/_bottom-app-bar.scss +14 -20
  245. package/src/styles/elements/_bottom-sheet.scss +0 -3
  246. package/src/styles/elements/_button-group.scss +80 -80
  247. package/src/styles/elements/_button.scss +72 -88
  248. package/src/styles/elements/_card.scss +48 -57
  249. package/src/styles/elements/_carousel.scss +2 -5
  250. package/src/styles/elements/_checkbox.scss +32 -43
  251. package/src/styles/elements/_chips.scss +18 -22
  252. package/src/styles/elements/_config.scss +11 -26
  253. package/src/styles/elements/_datepicker.scss +17 -22
  254. package/src/styles/elements/_dialog.scss +0 -3
  255. package/src/styles/elements/_divider.scss +11 -13
  256. package/src/styles/elements/_drawer.scss +9 -14
  257. package/src/styles/elements/_extended-fab.scss +36 -40
  258. package/src/styles/elements/_fab-menu.scss +21 -25
  259. package/src/styles/elements/_fab.scss +17 -20
  260. package/src/styles/elements/_field.scss +93 -114
  261. package/src/styles/elements/_icon-button.scss +34 -38
  262. package/src/styles/elements/_index.scss +0 -4
  263. package/src/styles/elements/_list.scss +19 -26
  264. package/src/styles/elements/_loading-indicator.scss +6 -9
  265. package/src/styles/elements/_menu.scss +0 -3
  266. package/src/styles/elements/_navigation-bar.scss +5 -9
  267. package/src/styles/elements/_navigation-rail.scss +10 -15
  268. package/src/styles/elements/_progress.scss +15 -18
  269. package/src/styles/elements/_radios.scss +24 -49
  270. package/src/styles/elements/_search.scss +19 -24
  271. package/src/styles/elements/_select.scss +4 -9
  272. package/src/styles/elements/_side-sheet.scss +0 -3
  273. package/src/styles/elements/_slider.scss +15 -35
  274. package/src/styles/elements/_snackbar.scss +0 -3
  275. package/src/styles/elements/_split-button.scss +33 -39
  276. package/src/styles/elements/_switch.scss +50 -90
  277. package/src/styles/elements/_tabs.scss +9 -13
  278. package/src/styles/elements/_text-field.scss +150 -178
  279. package/src/styles/elements/_timepicker.scss +0 -3
  280. package/src/styles/elements/_toolbar.scss +30 -35
  281. package/src/styles/elements/_tooltip.scss +0 -3
  282. package/src/styles/elements/_top-app-bar.scss +38 -45
  283. package/src/styles/main.scss +0 -15
  284. package/src/styles/themes/_autumn.scss +258 -258
  285. package/src/styles/themes/_base-theme.scss +112 -134
  286. package/src/styles/themes/_baseline.scss +291 -314
  287. package/src/styles/themes/_brownbeige.scss +270 -250
  288. package/src/styles/themes/_content.scss +268 -248
  289. package/src/styles/themes/_desert.scss +270 -250
  290. package/src/styles/themes/_expressive.scss +270 -250
  291. package/src/styles/themes/_fidelity.scss +268 -248
  292. package/src/styles/themes/_forest.scss +260 -259
  293. package/src/styles/themes/_fruit-salad.scss +270 -250
  294. package/src/styles/themes/_highcontrast.scss +183 -172
  295. package/src/styles/themes/_index.scss +0 -1
  296. package/src/styles/themes/_monochrome.scss +254 -234
  297. package/src/styles/themes/_neutral.scss +270 -250
  298. package/src/styles/themes/_ocean.scss +260 -259
  299. package/src/styles/themes/_rainbow.scss +270 -250
  300. package/src/styles/themes/_sageivory.scss +270 -250
  301. package/src/styles/themes/_spring.scss +256 -255
  302. package/src/styles/themes/_summer.scss +270 -250
  303. package/src/styles/themes/_sunset.scss +260 -259
  304. package/src/styles/themes/_tealcaramel.scss +270 -250
  305. package/src/styles/themes/_vibrant.scss +270 -250
  306. package/src/styles/utilities/_colors.scss +44 -50
  307. package/src/styles/utilities/_flexbox.scss +48 -59
  308. package/src/styles/utilities/_layout.scss +516 -591
  309. package/src/styles/utilities/_ripple.scss +61 -70
  310. package/src/styles/utilities/_spacing.scss +120 -127
  311. package/src/styles/utilities/_typography.scss +45 -58
  312. package/src/styles/utilities/_visibility.scss +61 -76
  313. package/dist/package.json +0 -488
@@ -0,0 +1,86 @@
1
+ /*!
2
+ * @license
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import * as utils from "../utils/color_utils.js";
18
+ import { Cam16 } from "./cam16.js";
19
+ import { HctSolver } from "./hct_solver.js";
20
+ import { ViewingConditions } from "./viewing_conditions.js";
21
+ export class Hct {
22
+ static from(hue, chroma, tone) {
23
+ return new Hct(HctSolver.solveToInt(hue, chroma, tone));
24
+ }
25
+ static fromInt(argb) {
26
+ return new Hct(argb);
27
+ }
28
+ toInt() {
29
+ return this.argb;
30
+ }
31
+ get hue() {
32
+ return this.internalHue;
33
+ }
34
+ set hue(newHue) {
35
+ this.setInternalState(HctSolver.solveToInt(newHue, this.internalChroma, this.internalTone));
36
+ }
37
+ get chroma() {
38
+ return this.internalChroma;
39
+ }
40
+ set chroma(newChroma) {
41
+ this.setInternalState(HctSolver.solveToInt(this.internalHue, newChroma, this.internalTone));
42
+ }
43
+ get tone() {
44
+ return this.internalTone;
45
+ }
46
+ set tone(newTone) {
47
+ this.setInternalState(HctSolver.solveToInt(this.internalHue, this.internalChroma, newTone));
48
+ }
49
+ setValue(propertyName, value) {
50
+ this[propertyName] = value;
51
+ }
52
+ toString() {
53
+ return `HCT(${this.hue.toFixed(0)}, ${this.chroma.toFixed(0)}, ${this.tone.toFixed(0)})`;
54
+ }
55
+ static isBlue(hue) {
56
+ return hue >= 250 && hue < 270;
57
+ }
58
+ static isYellow(hue) {
59
+ return hue >= 105 && hue < 125;
60
+ }
61
+ static isCyan(hue) {
62
+ return hue >= 170 && hue < 207;
63
+ }
64
+ constructor(argb) {
65
+ this.argb = argb;
66
+ const cam = Cam16.fromInt(argb);
67
+ this.internalHue = cam.hue;
68
+ this.internalChroma = cam.chroma;
69
+ this.internalTone = utils.lstarFromArgb(argb);
70
+ this.argb = argb;
71
+ }
72
+ setInternalState(argb) {
73
+ const cam = Cam16.fromInt(argb);
74
+ this.internalHue = cam.hue;
75
+ this.internalChroma = cam.chroma;
76
+ this.internalTone = utils.lstarFromArgb(argb);
77
+ this.argb = argb;
78
+ }
79
+ inViewingConditions(vc) {
80
+ const cam = Cam16.fromInt(this.toInt());
81
+ const viewedInVc = cam.xyzInViewingConditions(vc);
82
+ const recastInVc = Cam16.fromXyzInViewingConditions(viewedInVc[0], viewedInVc[1], viewedInVc[2], ViewingConditions.make());
83
+ const recastHct = Hct.from(recastInVc.hue, recastInVc.chroma, utils.lstarFromY(viewedInVc[1]));
84
+ return recastHct;
85
+ }
86
+ }
@@ -0,0 +1,390 @@
1
+ /*!
2
+ * @license
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import * as colorUtils from "../utils/color_utils.js";
18
+ import * as mathUtils from "../utils/math_utils.js";
19
+ import { Cam16 } from "./cam16.js";
20
+ import { ViewingConditions } from "./viewing_conditions.js";
21
+ export class HctSolver {
22
+ static sanitizeRadians(angle) {
23
+ return (angle + Math.PI * 8) % (Math.PI * 2);
24
+ }
25
+ static trueDelinearized(rgbComponent) {
26
+ const normalized = rgbComponent / 100.0;
27
+ let delinearized = 0.0;
28
+ if (normalized <= 0.0031308) {
29
+ delinearized = normalized * 12.92;
30
+ }
31
+ else {
32
+ delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
33
+ }
34
+ return delinearized * 255.0;
35
+ }
36
+ static chromaticAdaptation(component) {
37
+ const af = Math.pow(Math.abs(component), 0.42);
38
+ return mathUtils.signum(component) * 400.0 * af / (af + 27.13);
39
+ }
40
+ static hueOf(linrgb) {
41
+ const scaledDiscount = mathUtils.matrixMultiply(linrgb, HctSolver.SCALED_DISCOUNT_FROM_LINRGB);
42
+ const rA = HctSolver.chromaticAdaptation(scaledDiscount[0]);
43
+ const gA = HctSolver.chromaticAdaptation(scaledDiscount[1]);
44
+ const bA = HctSolver.chromaticAdaptation(scaledDiscount[2]);
45
+ const a = (11.0 * rA + -12.0 * gA + bA) / 11.0;
46
+ const b = (rA + gA - 2.0 * bA) / 9.0;
47
+ return Math.atan2(b, a);
48
+ }
49
+ static areInCyclicOrder(a, b, c) {
50
+ const deltaAB = HctSolver.sanitizeRadians(b - a);
51
+ const deltaAC = HctSolver.sanitizeRadians(c - a);
52
+ return deltaAB < deltaAC;
53
+ }
54
+ static intercept(source, mid, target) {
55
+ return (mid - source) / (target - source);
56
+ }
57
+ static lerpPoint(source, t, target) {
58
+ return [
59
+ source[0] + (target[0] - source[0]) * t,
60
+ source[1] + (target[1] - source[1]) * t,
61
+ source[2] + (target[2] - source[2]) * t,
62
+ ];
63
+ }
64
+ static setCoordinate(source, coordinate, target, axis) {
65
+ const t = HctSolver.intercept(source[axis], coordinate, target[axis]);
66
+ return HctSolver.lerpPoint(source, t, target);
67
+ }
68
+ static isBounded(x) {
69
+ return 0.0 <= x && x <= 100.0;
70
+ }
71
+ static nthVertex(y, n) {
72
+ const kR = HctSolver.Y_FROM_LINRGB[0];
73
+ const kG = HctSolver.Y_FROM_LINRGB[1];
74
+ const kB = HctSolver.Y_FROM_LINRGB[2];
75
+ const coordA = n % 4 <= 1 ? 0.0 : 100.0;
76
+ const coordB = n % 2 === 0 ? 0.0 : 100.0;
77
+ if (n < 4) {
78
+ const g = coordA;
79
+ const b = coordB;
80
+ const r = (y - g * kG - b * kB) / kR;
81
+ if (HctSolver.isBounded(r)) {
82
+ return [r, g, b];
83
+ }
84
+ else {
85
+ return [-1.0, -1.0, -1.0];
86
+ }
87
+ }
88
+ else if (n < 8) {
89
+ const b = coordA;
90
+ const r = coordB;
91
+ const g = (y - r * kR - b * kB) / kG;
92
+ if (HctSolver.isBounded(g)) {
93
+ return [r, g, b];
94
+ }
95
+ else {
96
+ return [-1.0, -1.0, -1.0];
97
+ }
98
+ }
99
+ else {
100
+ const r = coordA;
101
+ const g = coordB;
102
+ const b = (y - r * kR - g * kG) / kB;
103
+ if (HctSolver.isBounded(b)) {
104
+ return [r, g, b];
105
+ }
106
+ else {
107
+ return [-1.0, -1.0, -1.0];
108
+ }
109
+ }
110
+ }
111
+ static bisectToSegment(y, targetHue) {
112
+ let left = [-1.0, -1.0, -1.0];
113
+ let right = left;
114
+ let leftHue = 0.0;
115
+ let rightHue = 0.0;
116
+ let initialized = false;
117
+ let uncut = true;
118
+ for (let n = 0; n < 12; n++) {
119
+ const mid = HctSolver.nthVertex(y, n);
120
+ if (mid[0] < 0) {
121
+ continue;
122
+ }
123
+ const midHue = HctSolver.hueOf(mid);
124
+ if (!initialized) {
125
+ left = mid;
126
+ right = mid;
127
+ leftHue = midHue;
128
+ rightHue = midHue;
129
+ initialized = true;
130
+ continue;
131
+ }
132
+ if (uncut || HctSolver.areInCyclicOrder(leftHue, midHue, rightHue)) {
133
+ uncut = false;
134
+ if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
135
+ right = mid;
136
+ rightHue = midHue;
137
+ }
138
+ else {
139
+ left = mid;
140
+ leftHue = midHue;
141
+ }
142
+ }
143
+ }
144
+ return [left, right];
145
+ }
146
+ static midpoint(a, b) {
147
+ return [
148
+ (a[0] + b[0]) / 2,
149
+ (a[1] + b[1]) / 2,
150
+ (a[2] + b[2]) / 2,
151
+ ];
152
+ }
153
+ static criticalPlaneBelow(x) {
154
+ return Math.floor(x - 0.5);
155
+ }
156
+ static criticalPlaneAbove(x) {
157
+ return Math.ceil(x - 0.5);
158
+ }
159
+ static bisectToLimit(y, targetHue) {
160
+ const segment = HctSolver.bisectToSegment(y, targetHue);
161
+ let left = segment[0];
162
+ let leftHue = HctSolver.hueOf(left);
163
+ let right = segment[1];
164
+ for (let axis = 0; axis < 3; axis++) {
165
+ if (left[axis] !== right[axis]) {
166
+ let lPlane = -1;
167
+ let rPlane = 255;
168
+ if (left[axis] < right[axis]) {
169
+ lPlane = HctSolver.criticalPlaneBelow(HctSolver.trueDelinearized(left[axis]));
170
+ rPlane = HctSolver.criticalPlaneAbove(HctSolver.trueDelinearized(right[axis]));
171
+ }
172
+ else {
173
+ lPlane = HctSolver.criticalPlaneAbove(HctSolver.trueDelinearized(left[axis]));
174
+ rPlane = HctSolver.criticalPlaneBelow(HctSolver.trueDelinearized(right[axis]));
175
+ }
176
+ for (let i = 0; i < 8; i++) {
177
+ if (Math.abs(rPlane - lPlane) <= 1) {
178
+ break;
179
+ }
180
+ else {
181
+ const mPlane = Math.floor((lPlane + rPlane) / 2.0);
182
+ const midPlaneCoordinate = HctSolver.CRITICAL_PLANES[mPlane];
183
+ const mid = HctSolver.setCoordinate(left, midPlaneCoordinate, right, axis);
184
+ const midHue = HctSolver.hueOf(mid);
185
+ if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
186
+ right = mid;
187
+ rPlane = mPlane;
188
+ }
189
+ else {
190
+ left = mid;
191
+ leftHue = midHue;
192
+ lPlane = mPlane;
193
+ }
194
+ }
195
+ }
196
+ }
197
+ }
198
+ return HctSolver.midpoint(left, right);
199
+ }
200
+ static inverseChromaticAdaptation(adapted) {
201
+ const adaptedAbs = Math.abs(adapted);
202
+ const base = Math.max(0, 27.13 * adaptedAbs / (400.0 - adaptedAbs));
203
+ return mathUtils.signum(adapted) * Math.pow(base, 1.0 / 0.42);
204
+ }
205
+ static findResultByJ(hueRadians, chroma, y) {
206
+ let j = Math.sqrt(y) * 11.0;
207
+ const viewingConditions = ViewingConditions.DEFAULT;
208
+ const tInnerCoeff = 1 / Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73);
209
+ const eHue = 0.25 * (Math.cos(hueRadians + 2.0) + 3.8);
210
+ const p1 = eHue * (50000.0 / 13.0) * viewingConditions.nc * viewingConditions.ncb;
211
+ const hSin = Math.sin(hueRadians);
212
+ const hCos = Math.cos(hueRadians);
213
+ for (let iterationRound = 0; iterationRound < 5; iterationRound++) {
214
+ const jNormalized = j / 100.0;
215
+ const alpha = chroma === 0.0 || j === 0.0 ? 0.0 : chroma / Math.sqrt(jNormalized);
216
+ const t = Math.pow(alpha * tInnerCoeff, 1.0 / 0.9);
217
+ const ac = viewingConditions.aw *
218
+ Math.pow(jNormalized, 1.0 / viewingConditions.c / viewingConditions.z);
219
+ const p2 = ac / viewingConditions.nbb;
220
+ const gamma = 23.0 * (p2 + 0.305) * t /
221
+ (23.0 * p1 + 11 * t * hCos + 108.0 * t * hSin);
222
+ const a = gamma * hCos;
223
+ const b = gamma * hSin;
224
+ const rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
225
+ const gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
226
+ const bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
227
+ const rCScaled = HctSolver.inverseChromaticAdaptation(rA);
228
+ const gCScaled = HctSolver.inverseChromaticAdaptation(gA);
229
+ const bCScaled = HctSolver.inverseChromaticAdaptation(bA);
230
+ const linrgb = mathUtils.matrixMultiply([rCScaled, gCScaled, bCScaled], HctSolver.LINRGB_FROM_SCALED_DISCOUNT);
231
+ if (linrgb[0] < 0 || linrgb[1] < 0 || linrgb[2] < 0) {
232
+ return 0;
233
+ }
234
+ const kR = HctSolver.Y_FROM_LINRGB[0];
235
+ const kG = HctSolver.Y_FROM_LINRGB[1];
236
+ const kB = HctSolver.Y_FROM_LINRGB[2];
237
+ const fnj = kR * linrgb[0] + kG * linrgb[1] + kB * linrgb[2];
238
+ if (fnj <= 0) {
239
+ return 0;
240
+ }
241
+ if (iterationRound === 4 || Math.abs(fnj - y) < 0.002) {
242
+ if (linrgb[0] > 100.01 || linrgb[1] > 100.01 || linrgb[2] > 100.01) {
243
+ return 0;
244
+ }
245
+ return colorUtils.argbFromLinrgb(linrgb);
246
+ }
247
+ j = j - (fnj - y) * j / (2 * fnj);
248
+ }
249
+ return 0;
250
+ }
251
+ static solveToInt(hueDegrees, chroma, lstar) {
252
+ if (chroma < 0.0001 || lstar < 0.0001 || lstar > 99.9999) {
253
+ return colorUtils.argbFromLstar(lstar);
254
+ }
255
+ hueDegrees = mathUtils.sanitizeDegreesDouble(hueDegrees);
256
+ const hueRadians = hueDegrees / 180 * Math.PI;
257
+ const y = colorUtils.yFromLstar(lstar);
258
+ const exactAnswer = HctSolver.findResultByJ(hueRadians, chroma, y);
259
+ if (exactAnswer !== 0) {
260
+ return exactAnswer;
261
+ }
262
+ const linrgb = HctSolver.bisectToLimit(y, hueRadians);
263
+ return colorUtils.argbFromLinrgb(linrgb);
264
+ }
265
+ static solveToCam(hueDegrees, chroma, lstar) {
266
+ return Cam16.fromInt(HctSolver.solveToInt(hueDegrees, chroma, lstar));
267
+ }
268
+ }
269
+ HctSolver.SCALED_DISCOUNT_FROM_LINRGB = [
270
+ [
271
+ 0.001200833568784504,
272
+ 0.002389694492170889,
273
+ 0.0002795742885861124,
274
+ ],
275
+ [
276
+ 0.0005891086651375999,
277
+ 0.0029785502573438758,
278
+ 0.0003270666104008398,
279
+ ],
280
+ [
281
+ 0.00010146692491640572,
282
+ 0.0005364214359186694,
283
+ 0.0032979401770712076,
284
+ ],
285
+ ];
286
+ HctSolver.LINRGB_FROM_SCALED_DISCOUNT = [
287
+ [
288
+ 1373.2198709594231,
289
+ -1100.4251190754821,
290
+ -7.278681089101213,
291
+ ],
292
+ [
293
+ -271.815969077903,
294
+ 559.6580465940733,
295
+ -32.46047482791194,
296
+ ],
297
+ [
298
+ 1.9622899599665666,
299
+ -57.173814538844006,
300
+ 308.7233197812385,
301
+ ],
302
+ ];
303
+ HctSolver.Y_FROM_LINRGB = [0.2126, 0.7152, 0.0722];
304
+ HctSolver.CRITICAL_PLANES = [
305
+ 0.015176349177441876, 0.045529047532325624, 0.07588174588720938,
306
+ 0.10623444424209313, 0.13658714259697685, 0.16693984095186062,
307
+ 0.19729253930674434, 0.2276452376616281, 0.2579979360165119,
308
+ 0.28835063437139563, 0.3188300904430532, 0.350925934958123,
309
+ 0.3848314933096426, 0.42057480301049466, 0.458183274052838,
310
+ 0.4976837250274023, 0.5391024159806381, 0.5824650784040898,
311
+ 0.6277969426914107, 0.6751227633498623, 0.7244668422128921,
312
+ 0.775853049866786, 0.829304845476233, 0.8848452951698498,
313
+ 0.942497089126609, 1.0022825574869039, 1.0642236851973577,
314
+ 1.1283421258858297, 1.1946592148522128, 1.2631959812511864,
315
+ 1.3339731595349034, 1.407011200216447, 1.4823302800086415,
316
+ 1.5599503113873272, 1.6398909516233677, 1.7221716113234105,
317
+ 1.8068114625156377, 1.8938294463134073, 1.9832442801866852,
318
+ 2.075074464868551, 2.1693382909216234, 2.2660538449872063,
319
+ 2.36523901573795, 2.4669114995532007, 2.5710888059345764,
320
+ 2.6777882626779785, 2.7870270208169257, 2.898822059350997,
321
+ 3.0131901897720907, 3.1301480604002863, 3.2497121605402226,
322
+ 3.3718988244681087, 3.4967242352587946, 3.624204428461639,
323
+ 3.754355295633311, 3.887192587735158, 4.022731918402185,
324
+ 4.160988767090289, 4.301978482107941, 4.445716283538092,
325
+ 4.592217266055746, 4.741496401646282, 4.893568542229298,
326
+ 5.048448422192488, 5.20615066083972, 5.3666897647573375,
327
+ 5.5300801301023865, 5.696336044816294, 5.865471690767354,
328
+ 6.037501145825082, 6.212438385869475, 6.390297286737924,
329
+ 6.571091626112461, 6.7548350853498045, 6.941541251256611,
330
+ 7.131223617812143, 7.323895587840543, 7.5195704746346665,
331
+ 7.7182615035334345, 7.919981813454504, 8.124744458384042,
332
+ 8.332562408825165, 8.543448553206703, 8.757415699253682,
333
+ 8.974476575321063, 9.194643831691977, 9.417930041841839,
334
+ 9.644347703669503, 9.873909240696694, 10.106627003236781,
335
+ 10.342513269534024, 10.58158024687427, 10.8238400726681,
336
+ 11.069304815507364, 11.317986476196008, 11.569896988756009,
337
+ 11.825048221409341, 12.083451977536606, 12.345119996613247,
338
+ 12.610063955123938, 12.878295467455942, 13.149826086772048,
339
+ 13.42466730586372, 13.702830557985108, 13.984327217668513,
340
+ 14.269168601521828, 14.55736596900856, 14.848930523210871,
341
+ 15.143873411576273, 15.44220572664832, 15.743938506781891,
342
+ 16.04908273684337, 16.35764934889634, 16.66964922287304,
343
+ 16.985093187232053, 17.30399201960269, 17.62635644741625,
344
+ 17.95219714852476, 18.281524751807332, 18.614349837764564,
345
+ 18.95068293910138, 19.290534541298456, 19.633915083172692,
346
+ 19.98083495742689, 20.331304511189067, 20.685334046541502,
347
+ 21.042933821039977, 21.404114048223256, 21.76888489811322,
348
+ 22.137256497705877, 22.50923893145328, 22.884842241736916,
349
+ 23.264076429332462, 23.6469514538663, 24.033477234264016,
350
+ 24.42366364919083, 24.817520537484558, 25.21505769858089,
351
+ 25.61628489293138, 26.021211842414342, 26.429848230738664,
352
+ 26.842203703840827, 27.258287870275353, 27.678110301598522,
353
+ 28.10168053274597, 28.529008062403893, 28.96010235337422,
354
+ 29.39497283293396, 29.83362889318845, 30.276079891419332,
355
+ 30.722335150426627, 31.172403958865512, 31.62629557157785,
356
+ 32.08401920991837, 32.54558406207592, 33.010999283389665,
357
+ 33.4802739966603, 33.953417292456834, 34.430438229418264,
358
+ 34.911345834551085, 35.39614910352207, 35.88485700094671,
359
+ 36.37747846067349, 36.87402238606382, 37.37449765026789,
360
+ 37.87891309649659, 38.38727753828926, 38.89959975977785,
361
+ 39.41588851594697, 39.93615253289054, 40.460400508064545,
362
+ 40.98864111053629, 41.520882981230194, 42.05713473317016,
363
+ 42.597404951718396, 43.141702194811224, 43.6900349931913,
364
+ 44.24241185063697, 44.798841244188324, 45.35933162437017,
365
+ 45.92389141541209, 46.49252901546552, 47.065252796817916,
366
+ 47.64207110610409, 48.22299226451468, 48.808024568002054,
367
+ 49.3971762874833, 49.9904556690408, 50.587870934119984,
368
+ 51.189430279724725, 51.79514187861014, 52.40501387947288,
369
+ 53.0190544071392, 53.637271562750364, 54.259673423945976,
370
+ 54.88626804504493, 55.517063457223934, 56.15206766869424,
371
+ 56.79128866487574, 57.43473440856916, 58.08241284012621,
372
+ 58.734331877617365, 59.39049941699807, 60.05092333227251,
373
+ 60.715611475655585, 61.38457167773311, 62.057811747619894,
374
+ 62.7353394731159, 63.417162620860914, 64.10328893648692,
375
+ 64.79372614476921, 65.48848194977529, 66.18756403501224,
376
+ 66.89098006357258, 67.59873767827808, 68.31084450182222,
377
+ 69.02730813691093, 69.74813616640164, 70.47333615344107,
378
+ 71.20291564160104, 71.93688215501312, 72.67524319850172,
379
+ 73.41800625771542, 74.16517879925733, 74.9167682708136,
380
+ 75.67278210128072, 76.43322770089146, 77.1981124613393,
381
+ 77.96744375590167, 78.74122893956174, 79.51947534912904,
382
+ 80.30219030335869, 81.08938110306934, 81.88105503125999,
383
+ 82.67721935322541, 83.4778813166706, 84.28304815182372,
384
+ 85.09272707154808, 85.90692527145302, 86.72564993000343,
385
+ 87.54890820862819, 88.3767072518277, 89.2090541872801,
386
+ 90.04595612594655, 90.88742016217518, 91.73345337380438,
387
+ 92.58406282226491, 93.43925555268066, 94.29903859396902,
388
+ 95.16341895893969, 96.03240364439274, 96.9059996312159,
389
+ 97.78421388448044, 98.6670533535366, 99.55452497210776,
390
+ ];
@@ -0,0 +1,73 @@
1
+ /*!
2
+ * @license
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import * as utils from "../utils/color_utils.js";
18
+ import * as math from "../utils/math_utils.js";
19
+ export class ViewingConditions {
20
+ static make(whitePoint = utils.whitePointD65(), adaptingLuminance = (200.0 / Math.PI) * utils.yFromLstar(50.0) / 100.0, backgroundLstar = 50.0, surround = 2.0, discountingIlluminant = false) {
21
+ const xyz = whitePoint;
22
+ const rW = xyz[0] * 0.401288 + xyz[1] * 0.650173 + xyz[2] * -0.051461;
23
+ const gW = xyz[0] * -0.250268 + xyz[1] * 1.204414 + xyz[2] * 0.045854;
24
+ const bW = xyz[0] * -0.002079 + xyz[1] * 0.048952 + xyz[2] * 0.953127;
25
+ const f = 0.8 + surround / 10.0;
26
+ const c = f >= 0.9 ? math.lerp(0.59, 0.69, (f - 0.9) * 10.0) :
27
+ math.lerp(0.525, 0.59, (f - 0.8) * 10.0);
28
+ let d = discountingIlluminant ?
29
+ 1.0 :
30
+ f * (1.0 - (1.0 / 3.6) * Math.exp((-adaptingLuminance - 42.0) / 92.0));
31
+ d = d > 1.0 ? 1.0 : d < 0.0 ? 0.0 : d;
32
+ const nc = f;
33
+ const rgbD = [
34
+ d * (100.0 / rW) + 1.0 - d,
35
+ d * (100.0 / gW) + 1.0 - d,
36
+ d * (100.0 / bW) + 1.0 - d,
37
+ ];
38
+ const k = 1.0 / (5.0 * adaptingLuminance + 1.0);
39
+ const k4 = k * k * k * k;
40
+ const k4F = 1.0 - k4;
41
+ const fl = k4 * adaptingLuminance +
42
+ 0.1 * k4F * k4F * Math.cbrt(5.0 * adaptingLuminance);
43
+ const n = utils.yFromLstar(backgroundLstar) / whitePoint[1];
44
+ const z = 1.48 + Math.sqrt(n);
45
+ const nbb = 0.725 / Math.pow(n, 0.2);
46
+ const ncb = nbb;
47
+ const rgbAFactors = [
48
+ Math.pow((fl * rgbD[0] * rW) / 100.0, 0.42),
49
+ Math.pow((fl * rgbD[1] * gW) / 100.0, 0.42),
50
+ Math.pow((fl * rgbD[2] * bW) / 100.0, 0.42),
51
+ ];
52
+ const rgbA = [
53
+ (400.0 * rgbAFactors[0]) / (rgbAFactors[0] + 27.13),
54
+ (400.0 * rgbAFactors[1]) / (rgbAFactors[1] + 27.13),
55
+ (400.0 * rgbAFactors[2]) / (rgbAFactors[2] + 27.13),
56
+ ];
57
+ const aw = (2.0 * rgbA[0] + rgbA[1] + 0.05 * rgbA[2]) * nbb;
58
+ return new ViewingConditions(n, aw, nbb, ncb, c, nc, rgbD, fl, Math.pow(fl, 0.25), z);
59
+ }
60
+ constructor(n, aw, nbb, ncb, c, nc, rgbD, fl, fLRoot, z) {
61
+ this.n = n;
62
+ this.aw = aw;
63
+ this.nbb = nbb;
64
+ this.ncb = ncb;
65
+ this.c = c;
66
+ this.nc = nc;
67
+ this.rgbD = rgbD;
68
+ this.fl = fl;
69
+ this.fLRoot = fLRoot;
70
+ this.z = z;
71
+ }
72
+ }
73
+ ViewingConditions.DEFAULT = ViewingConditions.make();
@@ -0,0 +1,114 @@
1
+ /*!
2
+ * @license
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import { Hct } from "../hct/hct.js";
18
+ export class TonalPalette {
19
+ static fromInt(argb) {
20
+ const hct = Hct.fromInt(argb);
21
+ return TonalPalette.fromHct(hct);
22
+ }
23
+ static fromHct(hct) {
24
+ return new TonalPalette(hct.hue, hct.chroma, hct);
25
+ }
26
+ static fromHueAndChroma(hue, chroma) {
27
+ const keyColor = new KeyColor(hue, chroma).create();
28
+ return new TonalPalette(hue, chroma, keyColor);
29
+ }
30
+ constructor(hue, chroma, keyColor) {
31
+ this.hue = hue;
32
+ this.chroma = chroma;
33
+ this.keyColor = keyColor;
34
+ this.cache = new Map();
35
+ }
36
+ tone(tone) {
37
+ let argb = this.cache.get(tone);
38
+ if (argb === undefined) {
39
+ if (tone == 99 && Hct.isYellow(this.hue)) {
40
+ argb = this.averageArgb(this.tone(98), this.tone(100));
41
+ }
42
+ else {
43
+ argb = Hct.from(this.hue, this.chroma, tone).toInt();
44
+ }
45
+ this.cache.set(tone, argb);
46
+ }
47
+ return argb;
48
+ }
49
+ getHct(tone) {
50
+ return Hct.fromInt(this.tone(tone));
51
+ }
52
+ averageArgb(argb1, argb2) {
53
+ const red1 = (argb1 >>> 16) & 0xff;
54
+ const green1 = (argb1 >>> 8) & 0xff;
55
+ const blue1 = argb1 & 0xff;
56
+ const red2 = (argb2 >>> 16) & 0xff;
57
+ const green2 = (argb2 >>> 8) & 0xff;
58
+ const blue2 = argb2 & 0xff;
59
+ const red = Math.round((red1 + red2) / 2);
60
+ const green = Math.round((green1 + green2) / 2);
61
+ const blue = Math.round((blue1 + blue2) / 2);
62
+ return (255 << 24 | (red & 255) << 16 | (green & 255) << 8 |
63
+ (blue & 255)) >>>
64
+ 0;
65
+ }
66
+ }
67
+ class KeyColor {
68
+ constructor(hue, requestedChroma) {
69
+ this.hue = hue;
70
+ this.requestedChroma = requestedChroma;
71
+ this.chromaCache = new Map();
72
+ this.maxChromaValue = 200.0;
73
+ }
74
+ create() {
75
+ const pivotTone = 50;
76
+ const toneStepSize = 1;
77
+ const epsilon = 0.01;
78
+ let lowerTone = 0;
79
+ let upperTone = 100;
80
+ while (lowerTone < upperTone) {
81
+ const midTone = Math.floor((lowerTone + upperTone) / 2);
82
+ const isAscending = this.maxChroma(midTone) < this.maxChroma(midTone + toneStepSize);
83
+ const sufficientChroma = this.maxChroma(midTone) >= this.requestedChroma - epsilon;
84
+ if (sufficientChroma) {
85
+ if (Math.abs(lowerTone - pivotTone) < Math.abs(upperTone - pivotTone)) {
86
+ upperTone = midTone;
87
+ }
88
+ else {
89
+ if (lowerTone === midTone) {
90
+ return Hct.from(this.hue, this.requestedChroma, lowerTone);
91
+ }
92
+ lowerTone = midTone;
93
+ }
94
+ }
95
+ else {
96
+ if (isAscending) {
97
+ lowerTone = midTone + toneStepSize;
98
+ }
99
+ else {
100
+ upperTone = midTone;
101
+ }
102
+ }
103
+ }
104
+ return Hct.from(this.hue, this.requestedChroma, lowerTone);
105
+ }
106
+ maxChroma(tone) {
107
+ if (this.chromaCache.has(tone)) {
108
+ return this.chromaCache.get(tone);
109
+ }
110
+ const chroma = Hct.from(this.hue, this.maxChromaValue, tone).chroma;
111
+ this.chromaCache.set(tone, chroma);
112
+ return chroma;
113
+ }
114
+ }
@@ -0,0 +1,31 @@
1
+ /*!
2
+ * @license
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import * as utils from "../utils/color_utils.js";
18
+ export class LabPointProvider {
19
+ fromInt(argb) {
20
+ return utils.labFromArgb(argb);
21
+ }
22
+ toInt(point) {
23
+ return utils.argbFromLab(point[0], point[1], point[2]);
24
+ }
25
+ distance(from, to) {
26
+ const dL = from[0] - to[0];
27
+ const dA = from[1] - to[1];
28
+ const dB = from[2] - to[2];
29
+ return dL * dL + dA * dA + dB * dB;
30
+ }
31
+ }