@udixio/theme 1.0.0-beta.9 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/.eslintrc.mjs +22 -0
  2. package/CHANGELOG.md +174 -0
  3. package/README.md +6 -68
  4. package/dist/API.d.ts +14 -0
  5. package/dist/API.d.ts.map +1 -0
  6. package/dist/LICENSE +202 -0
  7. package/dist/adapter/adapter.abstract.d.ts +10 -0
  8. package/dist/adapter/adapter.abstract.d.ts.map +1 -0
  9. package/dist/{config → adapter}/config.interface.d.ts +6 -5
  10. package/dist/adapter/config.interface.d.ts.map +1 -0
  11. package/dist/adapter/define-config.d.ts +3 -0
  12. package/dist/adapter/define-config.d.ts.map +1 -0
  13. package/dist/adapter/file-adapter.mixin.d.ts +18 -0
  14. package/dist/adapter/file-adapter.mixin.d.ts.map +1 -0
  15. package/dist/adapter/index.d.ts +4 -0
  16. package/dist/adapter/index.d.ts.map +1 -0
  17. package/dist/adapters/index.d.ts +3 -0
  18. package/dist/adapters/index.d.ts.map +1 -0
  19. package/dist/adapters/node.adapter.d.ts +7 -0
  20. package/dist/adapters/node.adapter.d.ts.map +1 -0
  21. package/dist/adapters/vite.adapter.d.ts +3 -0
  22. package/dist/adapters/vite.adapter.d.ts.map +1 -0
  23. package/dist/app.container.d.ts +5 -5
  24. package/dist/app.container.d.ts.map +1 -0
  25. package/dist/app.module.d.ts +1 -0
  26. package/dist/app.module.d.ts.map +1 -0
  27. package/dist/bootstrap.d.ts +3 -0
  28. package/dist/bootstrap.d.ts.map +1 -0
  29. package/dist/color/color.api.d.ts +39 -0
  30. package/dist/color/color.api.d.ts.map +1 -0
  31. package/dist/color/color.manager.d.ts +24 -0
  32. package/dist/color/color.manager.d.ts.map +1 -0
  33. package/dist/color/color.module.d.ts +1 -0
  34. package/dist/color/color.module.d.ts.map +1 -0
  35. package/dist/color/color.utils.d.ts +8 -0
  36. package/dist/color/color.utils.d.ts.map +1 -0
  37. package/dist/color/configurable-color.d.ts +31 -0
  38. package/dist/color/configurable-color.d.ts.map +1 -0
  39. package/dist/color/default-color.d.ts +3 -0
  40. package/dist/color/default-color.d.ts.map +1 -0
  41. package/dist/color/index.d.ts +5 -4
  42. package/dist/color/index.d.ts.map +1 -0
  43. package/dist/index.cjs +3215 -0
  44. package/dist/index.d.ts +7 -4
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +3175 -7
  47. package/dist/material-color-utilities/contrastCurve.d.ts +1 -0
  48. package/dist/material-color-utilities/contrastCurve.d.ts.map +1 -0
  49. package/dist/material-color-utilities/dynamic_color.d.ts +89 -76
  50. package/dist/material-color-utilities/dynamic_color.d.ts.map +1 -0
  51. package/dist/material-color-utilities/hct_solver.d.ts +131 -0
  52. package/dist/material-color-utilities/hct_solver.d.ts.map +1 -0
  53. package/dist/material-color-utilities/htc.d.ts +77 -0
  54. package/dist/material-color-utilities/htc.d.ts.map +1 -0
  55. package/dist/material-color-utilities/index.d.ts +1 -0
  56. package/dist/material-color-utilities/index.d.ts.map +1 -0
  57. package/dist/material-color-utilities/toneDeltaPair.d.ts +19 -25
  58. package/dist/material-color-utilities/toneDeltaPair.d.ts.map +1 -0
  59. package/dist/plugin/index.d.ts +4 -0
  60. package/dist/plugin/index.d.ts.map +1 -0
  61. package/dist/plugin/plugin.abstract.d.ts +19 -5
  62. package/dist/plugin/plugin.abstract.d.ts.map +1 -0
  63. package/dist/plugin/plugin.api.d.ts +10 -0
  64. package/dist/plugin/plugin.api.d.ts.map +1 -0
  65. package/dist/plugin/plugin.module.d.ts +1 -0
  66. package/dist/plugin/plugin.module.d.ts.map +1 -0
  67. package/dist/plugins/font/font.plugin.d.ts +50 -0
  68. package/dist/plugins/font/font.plugin.d.ts.map +1 -0
  69. package/dist/plugins/font/index.d.ts +2 -0
  70. package/dist/plugins/font/index.d.ts.map +1 -0
  71. package/dist/plugins/index.d.ts +2 -0
  72. package/dist/plugins/index.d.ts.map +1 -0
  73. package/dist/theme/index.d.ts +7 -3
  74. package/dist/theme/index.d.ts.map +1 -0
  75. package/dist/theme/scheme.d.ts +20 -0
  76. package/dist/theme/scheme.d.ts.map +1 -0
  77. package/dist/theme/scheme.manager.d.ts +31 -0
  78. package/dist/theme/scheme.manager.d.ts.map +1 -0
  79. package/dist/theme/theme.api.d.ts +23 -0
  80. package/dist/theme/theme.api.d.ts.map +1 -0
  81. package/dist/theme/theme.module.d.ts +1 -0
  82. package/dist/theme/theme.module.d.ts.map +1 -0
  83. package/dist/theme/variant.d.ts +36 -0
  84. package/dist/theme/variant.d.ts.map +1 -0
  85. package/dist/theme/variant.manager.d.ts +14 -0
  86. package/dist/theme/variant.manager.d.ts.map +1 -0
  87. package/dist/theme/variants/expressive.variant.d.ts +3 -0
  88. package/dist/theme/variants/expressive.variant.d.ts.map +1 -0
  89. package/dist/theme/variants/index.d.ts +11 -0
  90. package/dist/theme/variants/index.d.ts.map +1 -0
  91. package/dist/theme/variants/neutral.variant.d.ts +3 -0
  92. package/dist/theme/variants/neutral.variant.d.ts.map +1 -0
  93. package/dist/theme/variants/tonal-spot.variant.d.ts +3 -0
  94. package/dist/theme/variants/tonal-spot.variant.d.ts.map +1 -0
  95. package/dist/theme/variants/vibrant.variant.d.ts +3 -0
  96. package/dist/theme/variants/vibrant.variant.d.ts.map +1 -0
  97. package/package.json +24 -87
  98. package/src/API.ts +23 -0
  99. package/src/adapter/adapter.abstract.ts +64 -0
  100. package/src/{config → adapter}/config.interface.ts +5 -5
  101. package/src/adapter/define-config.ts +11 -0
  102. package/src/adapter/file-adapter.mixin.ts +72 -0
  103. package/src/adapter/index.ts +3 -0
  104. package/src/adapters/index.ts +2 -0
  105. package/src/adapters/node.adapter.ts +49 -0
  106. package/src/adapters/vite.adapter.ts +79 -0
  107. package/src/app.container.ts +12 -36
  108. package/src/app.module.ts +2 -2
  109. package/src/bootstrap.ts +6 -0
  110. package/src/color/color.api.ts +75 -0
  111. package/src/color/color.manager.ts +213 -0
  112. package/src/color/color.module.ts +4 -4
  113. package/src/color/color.utils.ts +126 -0
  114. package/src/color/configurable-color.ts +67 -0
  115. package/src/color/default-color.ts +832 -0
  116. package/src/color/index.ts +4 -4
  117. package/src/index.test.ts +5 -0
  118. package/src/index.ts +6 -4
  119. package/src/material-color-utilities/dynamic_color.ts +286 -222
  120. package/src/material-color-utilities/hct_solver.ts +536 -0
  121. package/src/material-color-utilities/htc.ts +198 -0
  122. package/src/material-color-utilities/toneDeltaPair.ts +29 -11
  123. package/src/plugin/index.ts +3 -0
  124. package/src/plugin/plugin.abstract.ts +45 -5
  125. package/src/plugin/plugin.api.ts +51 -0
  126. package/src/plugin/plugin.module.ts +2 -2
  127. package/src/plugins/font/font.plugin.ts +203 -0
  128. package/src/plugins/font/index.ts +1 -0
  129. package/src/plugins/index.ts +1 -0
  130. package/src/theme/index.ts +6 -3
  131. package/src/theme/{services/scheme.service.ts → scheme.manager.ts} +39 -19
  132. package/src/theme/{entities/scheme.entity.ts → scheme.ts} +20 -4
  133. package/src/theme/{services/theme.service.ts → theme.api.ts} +23 -19
  134. package/src/theme/theme.module.ts +6 -4
  135. package/src/theme/variant.manager.ts +58 -0
  136. package/src/theme/variant.ts +53 -0
  137. package/src/theme/variants/expressive.variant.ts +81 -0
  138. package/src/theme/variants/index.ts +16 -0
  139. package/src/theme/variants/neutral.variant.ts +45 -0
  140. package/src/theme/variants/tonal-spot.variant.ts +35 -0
  141. package/src/theme/variants/vibrant.variant.ts +72 -0
  142. package/tsconfig.json +13 -0
  143. package/tsconfig.lib.json +33 -0
  144. package/tsconfig.spec.json +36 -0
  145. package/vite.config.ts +54 -0
  146. package/LICENSE +0 -21
  147. package/dist/app.service.d.ts +0 -13
  148. package/dist/color/color.interface.d.ts +0 -8
  149. package/dist/color/entities/color.entity.d.ts +0 -42
  150. package/dist/color/entities/index.d.ts +0 -1
  151. package/dist/color/models/default-color.model.d.ts +0 -3
  152. package/dist/color/models/index.d.ts +0 -1
  153. package/dist/color/services/color-manager.service.d.ts +0 -18
  154. package/dist/color/services/color.service.d.ts +0 -21
  155. package/dist/color/services/index.d.ts +0 -2
  156. package/dist/config/config.module.d.ts +0 -2
  157. package/dist/config/config.service.d.ts +0 -12
  158. package/dist/config/index.d.ts +0 -2
  159. package/dist/main.d.ts +0 -3
  160. package/dist/plugin/plugin.service.d.ts +0 -10
  161. package/dist/plugins/tailwind/Tailwind.plugin.d.ts +0 -14
  162. package/dist/plugins/tailwind/main.d.ts +0 -10
  163. package/dist/plugins/tailwind/plugins-tailwind/state.d.ts +0 -4
  164. package/dist/plugins/tailwind/plugins-tailwind/themer.d.ts +0 -4
  165. package/dist/theme/entities/index.d.ts +0 -2
  166. package/dist/theme/entities/scheme.entity.d.ts +0 -15
  167. package/dist/theme/entities/variant.entity.d.ts +0 -7
  168. package/dist/theme/models/index.d.ts +0 -1
  169. package/dist/theme/models/variant.model.d.ts +0 -8
  170. package/dist/theme/services/index.d.ts +0 -3
  171. package/dist/theme/services/scheme.service.d.ts +0 -17
  172. package/dist/theme/services/theme.service.d.ts +0 -22
  173. package/dist/theme/services/variant.service.d.ts +0 -13
  174. package/dist/theme.cjs.development.js +0 -1983
  175. package/dist/theme.cjs.development.js.map +0 -1
  176. package/dist/theme.cjs.production.min.js +0 -2
  177. package/dist/theme.cjs.production.min.js.map +0 -1
  178. package/dist/theme.esm.js +0 -1955
  179. package/dist/theme.esm.js.map +0 -1
  180. package/src/app.service.spec.ts +0 -15
  181. package/src/app.service.ts +0 -23
  182. package/src/color/color.interface.ts +0 -13
  183. package/src/color/entities/color.entity.ts +0 -71
  184. package/src/color/entities/index.ts +0 -1
  185. package/src/color/models/default-color.model.ts +0 -300
  186. package/src/color/models/index.ts +0 -1
  187. package/src/color/services/color-manager.service.ts +0 -191
  188. package/src/color/services/color.service.spec.ts +0 -28
  189. package/src/color/services/color.service.ts +0 -75
  190. package/src/color/services/index.ts +0 -2
  191. package/src/config/config.module.ts +0 -7
  192. package/src/config/config.service.ts +0 -74
  193. package/src/config/index.ts +0 -2
  194. package/src/main.ts +0 -14
  195. package/src/plugin/plugin.service.ts +0 -30
  196. package/src/plugins/tailwind/Tailwind.plugin.ts +0 -53
  197. package/src/plugins/tailwind/main.ts +0 -18
  198. package/src/plugins/tailwind/plugins-tailwind/state.ts +0 -88
  199. package/src/plugins/tailwind/plugins-tailwind/themer.ts +0 -53
  200. package/src/theme/entities/index.ts +0 -2
  201. package/src/theme/entities/variant.entity.ts +0 -39
  202. package/src/theme/models/index.ts +0 -1
  203. package/src/theme/models/variant.model.ts +0 -63
  204. package/src/theme/services/index.ts +0 -3
  205. package/src/theme/services/variant.service.ts +0 -52
@@ -0,0 +1,536 @@
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
+
18
+ // This file is automatically generated. Do not modify it.
19
+
20
+ // material_color_utilities is designed to have a consistent API across
21
+ // platforms and modular components that can be moved around easily. Using a
22
+ // class as a namespace facilitates this.
23
+ //
24
+ // tslint:disable:class-as-namespace
25
+ import {
26
+ argbFromLinrgb,
27
+ argbFromLstar,
28
+ Cam16,
29
+ matrixMultiply,
30
+ sanitizeDegreesDouble,
31
+ signum,
32
+ ViewingConditions,
33
+ yFromLstar
34
+ } from '@material/material-color-utilities';
35
+
36
+ /**
37
+ * A class that solves the HCT equation.
38
+ */
39
+ export class HctSolver {
40
+ static SCALED_DISCOUNT_FROM_LINRGB = [
41
+ [0.001200833568784504, 0.002389694492170889, 0.0002795742885861124],
42
+ [0.0005891086651375999, 0.0029785502573438758, 0.0003270666104008398],
43
+ [0.00010146692491640572, 0.0005364214359186694, 0.0032979401770712076],
44
+ ];
45
+
46
+ static LINRGB_FROM_SCALED_DISCOUNT = [
47
+ [1373.2198709594231, -1100.4251190754821, -7.278681089101213],
48
+ [-271.815969077903, 559.6580465940733, -32.46047482791194],
49
+ [1.9622899599665666, -57.173814538844006, 308.7233197812385],
50
+ ];
51
+
52
+ static Y_FROM_LINRGB = [0.2126, 0.7152, 0.0722];
53
+
54
+ static CRITICAL_PLANES = [
55
+ 0.015176349177441876, 0.045529047532325624, 0.07588174588720938,
56
+ 0.10623444424209313, 0.13658714259697685, 0.16693984095186062,
57
+ 0.19729253930674434, 0.2276452376616281, 0.2579979360165119,
58
+ 0.28835063437139563, 0.3188300904430532, 0.350925934958123,
59
+ 0.3848314933096426, 0.42057480301049466, 0.458183274052838,
60
+ 0.4976837250274023, 0.5391024159806381, 0.5824650784040898,
61
+ 0.6277969426914107, 0.6751227633498623, 0.7244668422128921,
62
+ 0.775853049866786, 0.829304845476233, 0.8848452951698498, 0.942497089126609,
63
+ 1.0022825574869039, 1.0642236851973577, 1.1283421258858297,
64
+ 1.1946592148522128, 1.2631959812511864, 1.3339731595349034,
65
+ 1.407011200216447, 1.4823302800086415, 1.5599503113873272,
66
+ 1.6398909516233677, 1.7221716113234105, 1.8068114625156377,
67
+ 1.8938294463134073, 1.9832442801866852, 2.075074464868551,
68
+ 2.1693382909216234, 2.2660538449872063, 2.36523901573795,
69
+ 2.4669114995532007, 2.5710888059345764, 2.6777882626779785,
70
+ 2.7870270208169257, 2.898822059350997, 3.0131901897720907,
71
+ 3.1301480604002863, 3.2497121605402226, 3.3718988244681087,
72
+ 3.4967242352587946, 3.624204428461639, 3.754355295633311, 3.887192587735158,
73
+ 4.022731918402185, 4.160988767090289, 4.301978482107941, 4.445716283538092,
74
+ 4.592217266055746, 4.741496401646282, 4.893568542229298, 5.048448422192488,
75
+ 5.20615066083972, 5.3666897647573375, 5.5300801301023865, 5.696336044816294,
76
+ 5.865471690767354, 6.037501145825082, 6.212438385869475, 6.390297286737924,
77
+ 6.571091626112461, 6.7548350853498045, 6.941541251256611, 7.131223617812143,
78
+ 7.323895587840543, 7.5195704746346665, 7.7182615035334345,
79
+ 7.919981813454504, 8.124744458384042, 8.332562408825165, 8.543448553206703,
80
+ 8.757415699253682, 8.974476575321063, 9.194643831691977, 9.417930041841839,
81
+ 9.644347703669503, 9.873909240696694, 10.106627003236781,
82
+ 10.342513269534024, 10.58158024687427, 10.8238400726681, 11.069304815507364,
83
+ 11.317986476196008, 11.569896988756009, 11.825048221409341,
84
+ 12.083451977536606, 12.345119996613247, 12.610063955123938,
85
+ 12.878295467455942, 13.149826086772048, 13.42466730586372,
86
+ 13.702830557985108, 13.984327217668513, 14.269168601521828,
87
+ 14.55736596900856, 14.848930523210871, 15.143873411576273,
88
+ 15.44220572664832, 15.743938506781891, 16.04908273684337, 16.35764934889634,
89
+ 16.66964922287304, 16.985093187232053, 17.30399201960269, 17.62635644741625,
90
+ 17.95219714852476, 18.281524751807332, 18.614349837764564,
91
+ 18.95068293910138, 19.290534541298456, 19.633915083172692,
92
+ 19.98083495742689, 20.331304511189067, 20.685334046541502,
93
+ 21.042933821039977, 21.404114048223256, 21.76888489811322,
94
+ 22.137256497705877, 22.50923893145328, 22.884842241736916,
95
+ 23.264076429332462, 23.6469514538663, 24.033477234264016, 24.42366364919083,
96
+ 24.817520537484558, 25.21505769858089, 25.61628489293138,
97
+ 26.021211842414342, 26.429848230738664, 26.842203703840827,
98
+ 27.258287870275353, 27.678110301598522, 28.10168053274597,
99
+ 28.529008062403893, 28.96010235337422, 29.39497283293396, 29.83362889318845,
100
+ 30.276079891419332, 30.722335150426627, 31.172403958865512,
101
+ 31.62629557157785, 32.08401920991837, 32.54558406207592, 33.010999283389665,
102
+ 33.4802739966603, 33.953417292456834, 34.430438229418264,
103
+ 34.911345834551085, 35.39614910352207, 35.88485700094671, 36.37747846067349,
104
+ 36.87402238606382, 37.37449765026789, 37.87891309649659, 38.38727753828926,
105
+ 38.89959975977785, 39.41588851594697, 39.93615253289054, 40.460400508064545,
106
+ 40.98864111053629, 41.520882981230194, 42.05713473317016,
107
+ 42.597404951718396, 43.141702194811224, 43.6900349931913, 44.24241185063697,
108
+ 44.798841244188324, 45.35933162437017, 45.92389141541209, 46.49252901546552,
109
+ 47.065252796817916, 47.64207110610409, 48.22299226451468,
110
+ 48.808024568002054, 49.3971762874833, 49.9904556690408, 50.587870934119984,
111
+ 51.189430279724725, 51.79514187861014, 52.40501387947288, 53.0190544071392,
112
+ 53.637271562750364, 54.259673423945976, 54.88626804504493,
113
+ 55.517063457223934, 56.15206766869424, 56.79128866487574, 57.43473440856916,
114
+ 58.08241284012621, 58.734331877617365, 59.39049941699807, 60.05092333227251,
115
+ 60.715611475655585, 61.38457167773311, 62.057811747619894, 62.7353394731159,
116
+ 63.417162620860914, 64.10328893648692, 64.79372614476921, 65.48848194977529,
117
+ 66.18756403501224, 66.89098006357258, 67.59873767827808, 68.31084450182222,
118
+ 69.02730813691093, 69.74813616640164, 70.47333615344107, 71.20291564160104,
119
+ 71.93688215501312, 72.67524319850172, 73.41800625771542, 74.16517879925733,
120
+ 74.9167682708136, 75.67278210128072, 76.43322770089146, 77.1981124613393,
121
+ 77.96744375590167, 78.74122893956174, 79.51947534912904, 80.30219030335869,
122
+ 81.08938110306934, 81.88105503125999, 82.67721935322541, 83.4778813166706,
123
+ 84.28304815182372, 85.09272707154808, 85.90692527145302, 86.72564993000343,
124
+ 87.54890820862819, 88.3767072518277, 89.2090541872801, 90.04595612594655,
125
+ 90.88742016217518, 91.73345337380438, 92.58406282226491, 93.43925555268066,
126
+ 94.29903859396902, 95.16341895893969, 96.03240364439274, 96.9059996312159,
127
+ 97.78421388448044, 98.6670533535366, 99.55452497210776,
128
+ ];
129
+
130
+ /**
131
+ * Finds an sRGB color with the given hue, chroma, and L*, if
132
+ * possible.
133
+ *
134
+ * @param hueDegrees The desired hue, in degrees.
135
+ * @param chroma The desired chroma.
136
+ * @param lstar The desired L*.
137
+ * @return A hexadecimal representing the sRGB color. The color
138
+ * has sufficiently close hue, chroma, and L* to the desired
139
+ * values, if possible; otherwise, the hue and L* will be
140
+ * sufficiently close, and chroma will be maximized.
141
+ */
142
+ static solveToInt(hueDegrees: number, chroma: number, lstar: number): number {
143
+ if (chroma < 0.0001 || lstar < 0.0001 || lstar > 99.9999) {
144
+ return argbFromLstar(lstar);
145
+ }
146
+ hueDegrees = sanitizeDegreesDouble(hueDegrees);
147
+ const hueRadians = (hueDegrees / 180) * Math.PI;
148
+ const y = yFromLstar(lstar);
149
+ const exactAnswer = HctSolver.findResultByJ(hueRadians, chroma, y);
150
+ if (exactAnswer !== 0) {
151
+ return exactAnswer;
152
+ }
153
+ const linrgb = HctSolver.bisectToLimit(y, hueRadians);
154
+ return argbFromLinrgb(linrgb);
155
+ }
156
+
157
+ /**
158
+ * Finds an sRGB color with the given hue, chroma, and L*, if
159
+ * possible.
160
+ *
161
+ * @param hueDegrees The desired hue, in degrees.
162
+ * @param chroma The desired chroma.
163
+ * @param lstar The desired L*.
164
+ * @return An CAM16 object representing the sRGB color. The color
165
+ * has sufficiently close hue, chroma, and L* to the desired
166
+ * values, if possible; otherwise, the hue and L* will be
167
+ * sufficiently close, and chroma will be maximized.
168
+ */
169
+ static solveToCam(hueDegrees: number, chroma: number, lstar: number): Cam16 {
170
+ return Cam16.fromInt(HctSolver.solveToInt(hueDegrees, chroma, lstar));
171
+ }
172
+
173
+ /**
174
+ * Sanitizes a small enough angle in radians.
175
+ *
176
+ * @param angle An angle in radians; must not deviate too much
177
+ * from 0.
178
+ * @return A coterminal angle between 0 and 2pi.
179
+ */
180
+ private static sanitizeRadians(angle: number): number {
181
+ return (angle + Math.PI * 8) % (Math.PI * 2);
182
+ }
183
+
184
+ /**
185
+ * Delinearizes an RGB component, returning a floating-point
186
+ * number.
187
+ *
188
+ * @param rgbComponent 0.0 <= rgb_component <= 100.0, represents
189
+ * linear R/G/B channel
190
+ * @return 0.0 <= output <= 255.0, color channel converted to
191
+ * regular RGB space
192
+ */
193
+ private static trueDelinearized(rgbComponent: number): number {
194
+ const normalized = rgbComponent / 100.0;
195
+ let delinearized = 0.0;
196
+ if (normalized <= 0.0031308) {
197
+ delinearized = normalized * 12.92;
198
+ } else {
199
+ delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
200
+ }
201
+ return delinearized * 255.0;
202
+ }
203
+
204
+ private static chromaticAdaptation(component: number): number {
205
+ const af = Math.pow(Math.abs(component), 0.42);
206
+ return (signum(component) * 400.0 * af) / (af + 27.13);
207
+ }
208
+
209
+ /**
210
+ * Returns the hue of a linear RGB color in CAM16.
211
+ *
212
+ * @param linrgb The linear RGB coordinates of a color.
213
+ * @return The hue of the color in CAM16, in radians.
214
+ */
215
+ private static hueOf(linrgb: number[]): number {
216
+ const scaledDiscount = matrixMultiply(
217
+ linrgb,
218
+ HctSolver.SCALED_DISCOUNT_FROM_LINRGB,
219
+ );
220
+ const rA = HctSolver.chromaticAdaptation(scaledDiscount[0]);
221
+ const gA = HctSolver.chromaticAdaptation(scaledDiscount[1]);
222
+ const bA = HctSolver.chromaticAdaptation(scaledDiscount[2]);
223
+ // redness-greenness
224
+ const a = (11.0 * rA + -12.0 * gA + bA) / 11.0;
225
+ // yellowness-blueness
226
+ const b = (rA + gA - 2.0 * bA) / 9.0;
227
+ return Math.atan2(b, a);
228
+ }
229
+
230
+ private static areInCyclicOrder(a: number, b: number, c: number): boolean {
231
+ const deltaAB = HctSolver.sanitizeRadians(b - a);
232
+ const deltaAC = HctSolver.sanitizeRadians(c - a);
233
+ return deltaAB < deltaAC;
234
+ }
235
+
236
+ /**
237
+ * Solves the lerp equation.
238
+ *
239
+ * @param source The starting number.
240
+ * @param mid The number in the middle.
241
+ * @param target The ending number.
242
+ * @return A number t such that lerp(source, target, t) = mid.
243
+ */
244
+ private static intercept(
245
+ source: number,
246
+ mid: number,
247
+ target: number,
248
+ ): number {
249
+ return (mid - source) / (target - source);
250
+ }
251
+
252
+ private static lerpPoint(
253
+ source: number[],
254
+ t: number,
255
+ target: number[],
256
+ ): number[] {
257
+ return [
258
+ source[0] + (target[0] - source[0]) * t,
259
+ source[1] + (target[1] - source[1]) * t,
260
+ source[2] + (target[2] - source[2]) * t,
261
+ ];
262
+ }
263
+
264
+ /**
265
+ * Intersects a segment with a plane.
266
+ *
267
+ * @param source The coordinates of point A.
268
+ * @param coordinate The R-, G-, or B-coordinate of the plane.
269
+ * @param target The coordinates of point B.
270
+ * @param axis The axis the plane is perpendicular with. (0: R, 1:
271
+ * G, 2: B)
272
+ * @return The intersection point of the segment AB with the plane
273
+ * R=coordinate, G=coordinate, or B=coordinate
274
+ */
275
+ private static setCoordinate(
276
+ source: number[],
277
+ coordinate: number,
278
+ target: number[],
279
+ axis: number,
280
+ ): number[] {
281
+ const t = HctSolver.intercept(source[axis], coordinate, target[axis]);
282
+ return HctSolver.lerpPoint(source, t, target);
283
+ }
284
+
285
+ private static isBounded(x: number): boolean {
286
+ return 0.0 <= x && x <= 100.0;
287
+ }
288
+
289
+ /**
290
+ * Returns the nth possible vertex of the polygonal intersection.
291
+ *
292
+ * @param y The Y value of the plane.
293
+ * @param n The zero-based index of the point. 0 <= n <= 11.
294
+ * @return The nth possible vertex of the polygonal intersection
295
+ * of the y plane and the RGB cube, in linear RGB coordinates, if
296
+ * it exists. If this possible vertex lies outside of the cube,
297
+ * [-1.0, -1.0, -1.0] is returned.
298
+ */
299
+ private static nthVertex(y: number, n: number): number[] {
300
+ const kR = HctSolver.Y_FROM_LINRGB[0];
301
+ const kG = HctSolver.Y_FROM_LINRGB[1];
302
+ const kB = HctSolver.Y_FROM_LINRGB[2];
303
+ const coordA = n % 4 <= 1 ? 0.0 : 100.0;
304
+ const coordB = n % 2 === 0 ? 0.0 : 100.0;
305
+ if (n < 4) {
306
+ const g = coordA;
307
+ const b = coordB;
308
+ const r = (y - g * kG - b * kB) / kR;
309
+ if (HctSolver.isBounded(r)) {
310
+ return [r, g, b];
311
+ } else {
312
+ return [-1.0, -1.0, -1.0];
313
+ }
314
+ } else if (n < 8) {
315
+ const b = coordA;
316
+ const r = coordB;
317
+ const g = (y - r * kR - b * kB) / kG;
318
+ if (HctSolver.isBounded(g)) {
319
+ return [r, g, b];
320
+ } else {
321
+ return [-1.0, -1.0, -1.0];
322
+ }
323
+ } else {
324
+ const r = coordA;
325
+ const g = coordB;
326
+ const b = (y - r * kR - g * kG) / kB;
327
+ if (HctSolver.isBounded(b)) {
328
+ return [r, g, b];
329
+ } else {
330
+ return [-1.0, -1.0, -1.0];
331
+ }
332
+ }
333
+ }
334
+
335
+ /**
336
+ * Finds the segment containing the desired color.
337
+ *
338
+ * @param y The Y value of the color.
339
+ * @param targetHue The hue of the color.
340
+ * @return A list of two sets of linear RGB coordinates, each
341
+ * corresponding to an endpoint of the segment containing the
342
+ * desired color.
343
+ */
344
+ private static bisectToSegment(y: number, targetHue: number): number[][] {
345
+ let left = [-1.0, -1.0, -1.0];
346
+ let right = left;
347
+ let leftHue = 0.0;
348
+ let rightHue = 0.0;
349
+ let initialized = false;
350
+ let uncut = true;
351
+ for (let n = 0; n < 12; n++) {
352
+ const mid = HctSolver.nthVertex(y, n);
353
+ if (mid[0] < 0) {
354
+ continue;
355
+ }
356
+ const midHue = HctSolver.hueOf(mid);
357
+ if (!initialized) {
358
+ left = mid;
359
+ right = mid;
360
+ leftHue = midHue;
361
+ rightHue = midHue;
362
+ initialized = true;
363
+ continue;
364
+ }
365
+ if (uncut || HctSolver.areInCyclicOrder(leftHue, midHue, rightHue)) {
366
+ uncut = false;
367
+ if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
368
+ right = mid;
369
+ rightHue = midHue;
370
+ } else {
371
+ left = mid;
372
+ leftHue = midHue;
373
+ }
374
+ }
375
+ }
376
+ return [left, right];
377
+ }
378
+
379
+ private static midpoint(a: number[], b: number[]): number[] {
380
+ return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2];
381
+ }
382
+
383
+ private static criticalPlaneBelow(x: number): number {
384
+ return Math.floor(x - 0.5);
385
+ }
386
+
387
+ private static criticalPlaneAbove(x: number): number {
388
+ return Math.ceil(x - 0.5);
389
+ }
390
+
391
+ /**
392
+ * Finds a color with the given Y and hue on the boundary of the
393
+ * cube.
394
+ *
395
+ * @param y The Y value of the color.
396
+ * @param targetHue The hue of the color.
397
+ * @return The desired color, in linear RGB coordinates.
398
+ */
399
+ private static bisectToLimit(y: number, targetHue: number): number[] {
400
+ const segment = HctSolver.bisectToSegment(y, targetHue);
401
+ let left = segment[0];
402
+ let leftHue = HctSolver.hueOf(left);
403
+ let right = segment[1];
404
+ for (let axis = 0; axis < 3; axis++) {
405
+ if (left[axis] !== right[axis]) {
406
+ let lPlane = -1;
407
+ let rPlane = 255;
408
+ if (left[axis] < right[axis]) {
409
+ lPlane = HctSolver.criticalPlaneBelow(
410
+ HctSolver.trueDelinearized(left[axis]),
411
+ );
412
+ rPlane = HctSolver.criticalPlaneAbove(
413
+ HctSolver.trueDelinearized(right[axis]),
414
+ );
415
+ } else {
416
+ lPlane = HctSolver.criticalPlaneAbove(
417
+ HctSolver.trueDelinearized(left[axis]),
418
+ );
419
+ rPlane = HctSolver.criticalPlaneBelow(
420
+ HctSolver.trueDelinearized(right[axis]),
421
+ );
422
+ }
423
+ for (let i = 0; i < 8; i++) {
424
+ if (Math.abs(rPlane - lPlane) <= 1) {
425
+ break;
426
+ } else {
427
+ const mPlane = Math.floor((lPlane + rPlane) / 2.0);
428
+ const midPlaneCoordinate = HctSolver.CRITICAL_PLANES[mPlane];
429
+ const mid = HctSolver.setCoordinate(
430
+ left,
431
+ midPlaneCoordinate,
432
+ right,
433
+ axis,
434
+ );
435
+ const midHue = HctSolver.hueOf(mid);
436
+ if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
437
+ right = mid;
438
+ rPlane = mPlane;
439
+ } else {
440
+ left = mid;
441
+ leftHue = midHue;
442
+ lPlane = mPlane;
443
+ }
444
+ }
445
+ }
446
+ }
447
+ }
448
+ return HctSolver.midpoint(left, right);
449
+ }
450
+
451
+ private static inverseChromaticAdaptation(adapted: number): number {
452
+ const adaptedAbs = Math.abs(adapted);
453
+ const base = Math.max(0, (27.13 * adaptedAbs) / (400.0 - adaptedAbs));
454
+ return signum(adapted) * Math.pow(base, 1.0 / 0.42);
455
+ }
456
+
457
+ /**
458
+ * Finds a color with the given hue, chroma, and Y.
459
+ *
460
+ * @param hueRadians The desired hue in radians.
461
+ * @param chroma The desired chroma.
462
+ * @param y The desired Y.
463
+ * @return The desired color as a hexadecimal integer, if found; 0
464
+ * otherwise.
465
+ */
466
+ private static findResultByJ(
467
+ hueRadians: number,
468
+ chroma: number,
469
+ y: number,
470
+ ): number {
471
+ // Initial estimate of j.
472
+ let j = Math.sqrt(y) * 11.0;
473
+ // ===========================================================
474
+ // Operations inlined from Cam16 to avoid repeated calculation
475
+ // ===========================================================
476
+ const viewingConditions = ViewingConditions.DEFAULT;
477
+ const tInnerCoeff =
478
+ 1 / Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73);
479
+ const eHue = 0.25 * (Math.cos(hueRadians + 2.0) + 3.8);
480
+ const p1 =
481
+ eHue * (50000.0 / 13.0) * viewingConditions.nc * viewingConditions.ncb;
482
+ const hSin = Math.sin(hueRadians);
483
+ const hCos = Math.cos(hueRadians);
484
+ for (let iterationRound = 0; iterationRound < 5; iterationRound++) {
485
+ // ===========================================================
486
+ // Operations inlined from Cam16 to avoid repeated calculation
487
+ // ===========================================================
488
+ const jNormalized = j / 100.0;
489
+ const alpha =
490
+ chroma === 0.0 || j === 0.0 ? 0.0 : chroma / Math.sqrt(jNormalized);
491
+ const t = Math.pow(alpha * tInnerCoeff, 1.0 / 0.9);
492
+ const ac =
493
+ viewingConditions.aw *
494
+ Math.pow(jNormalized, 1.0 / viewingConditions.c / viewingConditions.z);
495
+ const p2 = ac / viewingConditions.nbb;
496
+ const gamma =
497
+ (23.0 * (p2 + 0.305) * t) /
498
+ (23.0 * p1 + 11 * t * hCos + 108.0 * t * hSin);
499
+ const a = gamma * hCos;
500
+ const b = gamma * hSin;
501
+ const rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
502
+ const gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
503
+ const bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
504
+ const rCScaled = HctSolver.inverseChromaticAdaptation(rA);
505
+ const gCScaled = HctSolver.inverseChromaticAdaptation(gA);
506
+ const bCScaled = HctSolver.inverseChromaticAdaptation(bA);
507
+ const linrgb = matrixMultiply(
508
+ [rCScaled, gCScaled, bCScaled],
509
+ HctSolver.LINRGB_FROM_SCALED_DISCOUNT,
510
+ );
511
+ // ===========================================================
512
+ // Operations inlined from Cam16 to avoid repeated calculation
513
+ // ===========================================================
514
+ if (linrgb[0] < 0 || linrgb[1] < 0 || linrgb[2] < 0) {
515
+ return 0;
516
+ }
517
+ const kR = HctSolver.Y_FROM_LINRGB[0];
518
+ const kG = HctSolver.Y_FROM_LINRGB[1];
519
+ const kB = HctSolver.Y_FROM_LINRGB[2];
520
+ const fnj = kR * linrgb[0] + kG * linrgb[1] + kB * linrgb[2];
521
+ if (fnj <= 0) {
522
+ return 0;
523
+ }
524
+ if (iterationRound === 4 || Math.abs(fnj - y) < 0.002) {
525
+ if (linrgb[0] > 100.01 || linrgb[1] > 100.01 || linrgb[2] > 100.01) {
526
+ return 0;
527
+ }
528
+ return argbFromLinrgb(linrgb);
529
+ }
530
+ // Iterates with Newton method,
531
+ // Using 2 * fn(j) / j as the approximation of fn'(j)
532
+ j = j - ((fnj - y) * j) / (2 * fnj);
533
+ }
534
+ return 0;
535
+ }
536
+ }