colorizr 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/README.md +16 -16
  2. package/esm/brightness-difference.js +7 -7
  3. package/esm/brightness-difference.js.map +1 -1
  4. package/esm/chroma.js +5 -5
  5. package/esm/chroma.js.map +1 -1
  6. package/esm/color-difference.js +5 -5
  7. package/esm/color-difference.js.map +1 -1
  8. package/esm/compare.js +15 -15
  9. package/esm/compare.js.map +1 -1
  10. package/esm/contrast.js +8 -13
  11. package/esm/contrast.js.map +1 -1
  12. package/esm/darken.js +2 -3
  13. package/esm/darken.js.map +1 -1
  14. package/esm/desaturate.js +2 -3
  15. package/esm/desaturate.js.map +1 -1
  16. package/esm/fade.js +12 -14
  17. package/esm/fade.js.map +1 -1
  18. package/esm/format-css.js +10 -11
  19. package/esm/format-css.js.map +1 -1
  20. package/esm/format-hex.js +7 -7
  21. package/esm/format-hex.js.map +1 -1
  22. package/esm/hex2hsl.js +2 -2
  23. package/esm/hex2hsl.js.map +1 -1
  24. package/esm/hex2rgb.js +3 -3
  25. package/esm/hex2rgb.js.map +1 -1
  26. package/esm/hsl2hex.js +2 -2
  27. package/esm/hsl2hex.js.map +1 -1
  28. package/esm/hsl2rgb.d.ts +1 -1
  29. package/esm/hsl2rgb.js +13 -13
  30. package/esm/hsl2rgb.js.map +1 -1
  31. package/esm/index.d.ts +11 -12
  32. package/esm/index.js +99 -133
  33. package/esm/index.js.map +1 -1
  34. package/esm/is-valid-color.js +1 -1
  35. package/esm/is-valid-color.js.map +1 -1
  36. package/esm/is-valid-hex.js +2 -2
  37. package/esm/is-valid-hex.js.map +1 -1
  38. package/esm/lighten.js +2 -3
  39. package/esm/lighten.js.map +1 -1
  40. package/esm/luminance.js +8 -8
  41. package/esm/luminance.js.map +1 -1
  42. package/esm/{utils.d.ts → modules/css-colors.d.ts} +0 -61
  43. package/esm/modules/css-colors.js +153 -0
  44. package/esm/modules/css-colors.js.map +1 -0
  45. package/esm/{hue2rgb.d.ts → modules/hue2rgb.d.ts} +0 -0
  46. package/esm/{hue2rgb.js → modules/hue2rgb.js} +2 -2
  47. package/esm/modules/hue2rgb.js.map +1 -0
  48. package/{lib → esm/modules}/parse-color.d.ts +1 -1
  49. package/esm/{parse-color.js → modules/parse-color.js} +12 -12
  50. package/esm/modules/parse-color.js.map +1 -0
  51. package/esm/{updater.d.ts → modules/updater.d.ts} +0 -0
  52. package/esm/modules/updater.js +19 -0
  53. package/esm/modules/updater.js.map +1 -0
  54. package/esm/modules/utils.d.ts +61 -0
  55. package/esm/modules/utils.js +199 -0
  56. package/esm/modules/utils.js.map +1 -0
  57. package/esm/name.js +5 -7
  58. package/esm/name.js.map +1 -1
  59. package/esm/palette.js +15 -27
  60. package/esm/palette.js.map +1 -1
  61. package/esm/parse-css.js +13 -11
  62. package/esm/parse-css.js.map +1 -1
  63. package/esm/random.js +1 -1
  64. package/esm/random.js.map +1 -1
  65. package/esm/rgb2hex.d.ts +1 -1
  66. package/esm/rgb2hex.js +11 -11
  67. package/esm/rgb2hex.js.map +1 -1
  68. package/esm/rgb2hsl.d.ts +1 -1
  69. package/esm/rgb2hsl.js +14 -14
  70. package/esm/rgb2hsl.js.map +1 -1
  71. package/esm/rotate.js +6 -7
  72. package/esm/rotate.js.map +1 -1
  73. package/esm/saturate.js +2 -3
  74. package/esm/saturate.js.map +1 -1
  75. package/esm/scheme.d.ts +1 -1
  76. package/esm/scheme.js +12 -26
  77. package/esm/scheme.js.map +1 -1
  78. package/esm/shift.js +5 -16
  79. package/esm/shift.js.map +1 -1
  80. package/esm/text-color.js +4 -4
  81. package/esm/text-color.js.map +1 -1
  82. package/lib/brightness-difference.js +10 -10
  83. package/lib/brightness-difference.js.map +1 -1
  84. package/lib/chroma.js +8 -8
  85. package/lib/chroma.js.map +1 -1
  86. package/lib/color-difference.js +7 -7
  87. package/lib/color-difference.js.map +1 -1
  88. package/lib/compare.js +18 -18
  89. package/lib/compare.js.map +1 -1
  90. package/lib/contrast.js +10 -15
  91. package/lib/contrast.js.map +1 -1
  92. package/lib/darken.js +3 -4
  93. package/lib/darken.js.map +1 -1
  94. package/lib/desaturate.js +3 -4
  95. package/lib/desaturate.js.map +1 -1
  96. package/lib/fade.js +14 -16
  97. package/lib/fade.js.map +1 -1
  98. package/lib/format-css.js +16 -17
  99. package/lib/format-css.js.map +1 -1
  100. package/lib/format-hex.js +8 -8
  101. package/lib/format-hex.js.map +1 -1
  102. package/lib/hex2hsl.js +5 -5
  103. package/lib/hex2hsl.js.map +1 -1
  104. package/lib/hex2rgb.js +4 -4
  105. package/lib/hex2rgb.js.map +1 -1
  106. package/lib/hsl2hex.js +5 -5
  107. package/lib/hsl2hex.js.map +1 -1
  108. package/lib/hsl2rgb.d.ts +1 -1
  109. package/lib/hsl2rgb.js +16 -16
  110. package/lib/hsl2rgb.js.map +1 -1
  111. package/lib/index.d.ts +11 -12
  112. package/lib/index.js +135 -164
  113. package/lib/index.js.map +1 -1
  114. package/lib/is-valid-color.js +3 -3
  115. package/lib/is-valid-color.js.map +1 -1
  116. package/lib/is-valid-hex.js +3 -3
  117. package/lib/is-valid-hex.js.map +1 -1
  118. package/lib/lighten.js +3 -4
  119. package/lib/lighten.js.map +1 -1
  120. package/lib/luminance.js +11 -11
  121. package/lib/luminance.js.map +1 -1
  122. package/lib/{utils.d.ts → modules/css-colors.d.ts} +0 -61
  123. package/lib/modules/css-colors.js +156 -0
  124. package/lib/modules/css-colors.js.map +1 -0
  125. package/lib/{hue2rgb.d.ts → modules/hue2rgb.d.ts} +0 -0
  126. package/lib/modules/hue2rgb.js +28 -0
  127. package/lib/modules/hue2rgb.js.map +1 -0
  128. package/{esm → lib/modules}/parse-color.d.ts +1 -1
  129. package/lib/modules/parse-color.js +59 -0
  130. package/lib/modules/parse-color.js.map +1 -0
  131. package/lib/{updater.d.ts → modules/updater.d.ts} +0 -0
  132. package/lib/modules/updater.js +22 -0
  133. package/lib/modules/updater.js.map +1 -0
  134. package/lib/modules/utils.d.ts +61 -0
  135. package/lib/modules/utils.js +215 -0
  136. package/lib/modules/utils.js.map +1 -0
  137. package/lib/name.js +6 -8
  138. package/lib/name.js.map +1 -1
  139. package/lib/palette.js +19 -31
  140. package/lib/palette.js.map +1 -1
  141. package/lib/parse-css.js +26 -24
  142. package/lib/parse-css.js.map +1 -1
  143. package/lib/random.js +3 -3
  144. package/lib/random.js.map +1 -1
  145. package/lib/rgb2hex.d.ts +1 -1
  146. package/lib/rgb2hex.js +12 -12
  147. package/lib/rgb2hex.js.map +1 -1
  148. package/lib/rgb2hsl.d.ts +1 -1
  149. package/lib/rgb2hsl.js +14 -14
  150. package/lib/rgb2hsl.js.map +1 -1
  151. package/lib/rotate.js +11 -12
  152. package/lib/rotate.js.map +1 -1
  153. package/lib/saturate.js +3 -4
  154. package/lib/saturate.js.map +1 -1
  155. package/lib/scheme.d.ts +1 -1
  156. package/lib/scheme.js +14 -28
  157. package/lib/scheme.js.map +1 -1
  158. package/lib/shift.js +7 -18
  159. package/lib/shift.js.map +1 -1
  160. package/lib/text-color.js +6 -6
  161. package/lib/text-color.js.map +1 -1
  162. package/package.json +34 -43
  163. package/src/brightness-difference.ts +3 -3
  164. package/src/chroma.ts +2 -2
  165. package/src/color-difference.ts +3 -3
  166. package/src/compare.ts +3 -4
  167. package/src/contrast.ts +8 -12
  168. package/src/darken.ts +1 -1
  169. package/src/desaturate.ts +1 -1
  170. package/src/fade.ts +4 -4
  171. package/src/format-css.ts +3 -3
  172. package/src/format-hex.ts +4 -4
  173. package/src/hex2hsl.ts +2 -3
  174. package/src/hex2rgb.ts +2 -3
  175. package/src/hsl2hex.ts +2 -3
  176. package/src/hsl2rgb.ts +5 -6
  177. package/src/index.ts +14 -19
  178. package/src/is-valid-color.ts +1 -1
  179. package/src/is-valid-hex.ts +2 -2
  180. package/src/lighten.ts +1 -1
  181. package/src/luminance.ts +6 -6
  182. package/src/modules/css-colors.ts +152 -0
  183. package/src/{hue2rgb.ts → modules/hue2rgb.ts} +1 -4
  184. package/src/{parse-color.ts → modules/parse-color.ts} +11 -11
  185. package/src/{updater.ts → modules/updater.ts} +6 -5
  186. package/src/modules/utils.ts +253 -0
  187. package/src/name.ts +3 -2
  188. package/src/palette.ts +9 -8
  189. package/src/parse-css.ts +8 -6
  190. package/src/rgb2hex.ts +4 -5
  191. package/src/rgb2hsl.ts +4 -5
  192. package/src/rotate.ts +3 -3
  193. package/src/saturate.ts +1 -1
  194. package/src/scheme.ts +11 -23
  195. package/src/shift.ts +4 -5
  196. package/src/text-color.ts +2 -2
  197. package/src/types/index.ts +1 -0
  198. package/esm/hue2rgb.js.map +0 -1
  199. package/esm/parse-color.js.map +0 -1
  200. package/esm/updater.js +0 -20
  201. package/esm/updater.js.map +0 -1
  202. package/esm/utils.js +0 -360
  203. package/esm/utils.js.map +0 -1
  204. package/lib/hue2rgb.js +0 -28
  205. package/lib/hue2rgb.js.map +0 -1
  206. package/lib/parse-color.js +0 -59
  207. package/lib/parse-color.js.map +0 -1
  208. package/lib/updater.js +0 -23
  209. package/lib/updater.js.map +0 -1
  210. package/lib/utils.js +0 -376
  211. package/lib/utils.js.map +0 -1
  212. package/src/utils.ts +0 -412
@@ -1,24 +1,24 @@
1
- import hex2hsl from './hex2hsl';
2
- import hex2rgb from './hex2rgb';
3
- import hsl2hex from './hsl2hex';
4
- import hsl2rgb from './hsl2rgb';
5
- import isValidHex from './is-valid-hex';
6
- import parseCSS from './parse-css';
7
- import rgb2hex from './rgb2hex';
8
- import rgb2hsl from './rgb2hsl';
9
1
  import { invariant, isHSL, isPlainObject, isRGB, isString, limit, messages } from './utils';
10
2
 
11
- import { Colors, HSL, PlainObject, RGB, RGBArray } from './types';
3
+ import hex2hsl from '../hex2hsl';
4
+ import hex2rgb from '../hex2rgb';
5
+ import hsl2hex from '../hsl2hex';
6
+ import hsl2rgb from '../hsl2rgb';
7
+ import isValidHex from '../is-valid-hex';
8
+ import parseCSS from '../parse-css';
9
+ import rgb2hex from '../rgb2hex';
10
+ import rgb2hsl from '../rgb2hsl';
11
+ import { Colors, HSL, PlainObject, RGB, RGBArray } from '../types';
12
12
 
13
13
  export default function parseColor(color: string | HSL | RGB | RGBArray): Colors {
14
- invariant(!color, messages.input);
14
+ invariant(!!color, messages.input);
15
15
 
16
16
  const output: PlainObject = {};
17
17
 
18
18
  if (isString(color)) {
19
19
  const hex = parseCSS(color) as string;
20
20
 
21
- invariant(!isValidHex(hex), 'input is not valid');
21
+ invariant(isValidHex(hex), 'input is not valid');
22
22
 
23
23
  output.hex = hex;
24
24
  output.rgb = hex2rgb(hex);
@@ -1,15 +1,16 @@
1
- import hex2hsl from './hex2hsl';
2
- import parseCSS from './parse-css';
3
- import { shift } from './shift';
4
1
  import { constrain, invariant, isNumber, isString, messages } from './utils';
5
2
 
3
+ import hex2hsl from '../hex2hsl';
4
+ import parseCSS from '../parse-css';
5
+ import { shift } from '../shift';
6
+
6
7
  /**
7
8
  * Update color properties
8
9
  */
9
10
  export default function updater(type: 'h' | 's' | 'l', sign: '+' | '-') {
10
11
  return (input: string, amount: number) => {
11
- invariant(!isString(input), messages.inputString);
12
- invariant(!isNumber(amount), messages.amount);
12
+ invariant(isString(input), messages.inputString);
13
+ invariant(isNumber(amount), messages.amount);
13
14
 
14
15
  const hex = parseCSS(input);
15
16
  const hsl = hex2hsl(hex);
@@ -0,0 +1,253 @@
1
+ import { HSL, PlainObject, RGB, RGBArray } from '../types';
2
+
3
+ export const HSLKeys = ['h', 's', 'l'];
4
+ export const RGBKeys = ['r', 'g', 'b'];
5
+
6
+ /**
7
+ * Constrain value into the range
8
+ */
9
+ export function constrain(input: number, amount: number, range: number[], sign: string): number {
10
+ invariant(arguments.length === 4, 'All parameters are required');
11
+
12
+ const [min, max] = range;
13
+ let value = expr(input + sign + amount);
14
+
15
+ if (value < min) {
16
+ value = min;
17
+ } else if (value > max) {
18
+ value = max;
19
+ }
20
+
21
+ return Math.abs(value);
22
+ }
23
+
24
+ /**
25
+ * Constrain an angle
26
+ */
27
+ export function constrainDegrees(input: number, amount: number): number {
28
+ invariant(isNumber(input), 'input is required');
29
+
30
+ let value = input + amount;
31
+
32
+ if (value > 360) {
33
+ value %= 360;
34
+ }
35
+
36
+ if (value < 0) {
37
+ value += 360;
38
+ }
39
+
40
+ return Math.abs(value);
41
+ }
42
+
43
+ /**
44
+ * Parse math string expressions
45
+ */
46
+ export function expr(input: string): number {
47
+ const chars = [...input];
48
+ const n: string[] = [];
49
+ const op: string[] = [];
50
+
51
+ let parsed;
52
+ let index = 0;
53
+ let last = true;
54
+
55
+ n[index] = '';
56
+
57
+ // Parse the string
58
+ for (const char of chars) {
59
+ if (Number.isNaN(parseInt(char, 10)) && char !== '.' && !last) {
60
+ op[index] = char;
61
+ index++;
62
+ n[index] = '';
63
+ last = true;
64
+ } else {
65
+ n[index] += char;
66
+ last = false;
67
+ }
68
+ }
69
+
70
+ // Calculate the expression
71
+ parsed = parseFloat(n[0]);
72
+
73
+ for (const [o, element] of op.entries()) {
74
+ const value = parseFloat(n[o + 1]);
75
+
76
+ switch (element) {
77
+ case '+':
78
+ parsed += value;
79
+ break;
80
+ case '-':
81
+ parsed -= value;
82
+ break;
83
+ case '*':
84
+ parsed *= value;
85
+ break;
86
+ case '/':
87
+ parsed /= value;
88
+ break;
89
+ default:
90
+ break;
91
+ }
92
+ }
93
+
94
+ return parsed;
95
+ }
96
+
97
+ export function invariant(condition: boolean, message: string): asserts condition {
98
+ if (condition) {
99
+ return;
100
+ }
101
+
102
+ /* istanbul ignore else */
103
+ if (process.env.NODE_ENV !== 'production') {
104
+ if (message === undefined) {
105
+ throw new Error('invariant requires an error message argument');
106
+ }
107
+ }
108
+
109
+ let error;
110
+
111
+ if (!message) {
112
+ throw new Error(
113
+ 'Minified exception occurred; use the non-minified dev environment ' +
114
+ 'for the full error message and additional helpful warnings.',
115
+ );
116
+ } else {
117
+ error = new Error(message);
118
+ }
119
+
120
+ error.name = 'colorizr';
121
+
122
+ throw error;
123
+ }
124
+
125
+ /**
126
+ * Check if an object contains HSL values
127
+ */
128
+ export function isHSL(input: any): input is HSL {
129
+ if (!isPlainObject(input)) {
130
+ return false;
131
+ }
132
+
133
+ const entries = Object.entries(input);
134
+
135
+ return (
136
+ !!entries.length &&
137
+ entries.every(
138
+ ([key, value]) => HSLKeys.includes(key) && value >= 0 && value <= (key === 'h' ? 360 : 100),
139
+ )
140
+ );
141
+ }
142
+
143
+ /**
144
+ * Check if the input is a number and not NaN
145
+ */
146
+ export function isNumber(input: any): input is number {
147
+ return typeof input === 'number' && !Number.isNaN(input);
148
+ }
149
+
150
+ /**
151
+ * Check if the input is an object
152
+ */
153
+ export function isPlainObject(input: any): input is PlainObject {
154
+ if (!input) {
155
+ return false;
156
+ }
157
+
158
+ const { toString } = Object.prototype;
159
+ const prototype = Object.getPrototypeOf(input);
160
+
161
+ return (
162
+ toString.call(input) === '[object Object]' &&
163
+ (prototype === null || prototype === Object.getPrototypeOf({}))
164
+ );
165
+ }
166
+
167
+ /**
168
+ * Check if an object contains RGB values.
169
+ */
170
+ export function isRGB(input: any): input is RGB {
171
+ if (!isPlainObject(input)) {
172
+ return false;
173
+ }
174
+
175
+ const entries = Object.entries(input);
176
+
177
+ return (
178
+ !!entries.length &&
179
+ entries.every(([key, value]) => RGBKeys.includes(key) && value >= 0 && value <= 255)
180
+ );
181
+ }
182
+
183
+ /**
184
+ * Check if an array contains RGB values.
185
+ */
186
+ export function isRGBArray(input: any): input is RGBArray {
187
+ return Array.isArray(input) && input.length === 3 && input.every(d => d >= 0 && d <= 255);
188
+ }
189
+
190
+ /**
191
+ * Check if the input is a string
192
+ */
193
+ export function isString(input: any): input is string {
194
+ return typeof input === 'string';
195
+ }
196
+
197
+ /**
198
+ * Limit values per type.
199
+ */
200
+ export function limit(input: number, type: string): number {
201
+ invariant(isNumber(input), 'Input is not a number');
202
+
203
+ /* istanbul ignore else */
204
+ if (RGBKeys.includes(type)) {
205
+ return Math.max(Math.min(input, 255), 0);
206
+ }
207
+
208
+ if (['s', 'l'].includes(type)) {
209
+ return Math.max(Math.min(input, 100), 0);
210
+ }
211
+
212
+ if (type === 'h') {
213
+ return Math.max(Math.min(input, 360), 0);
214
+ }
215
+
216
+ throw new Error('Invalid type');
217
+ }
218
+
219
+ export const messages = {
220
+ amount: 'amount must be a number',
221
+ left: 'left is required and must be a string',
222
+ right: 'right is required and must be a string',
223
+ input: 'input is required',
224
+ inputString: 'input is required and must be a string',
225
+ invalid: 'invalid input',
226
+ options: 'invalid options',
227
+ };
228
+
229
+ /**
230
+ * Creates an object composed of the picked source properties.
231
+ */
232
+ export function pick(input: PlainObject, options: string[]): PlainObject {
233
+ if (!Array.isArray(options)) {
234
+ throw new TypeError('options must be an array');
235
+ }
236
+
237
+ return options
238
+ .filter(d => typeof input[d] !== 'undefined')
239
+ .reduce((acc: PlainObject, d) => {
240
+ acc[d] = input[d];
241
+
242
+ return acc;
243
+ }, {});
244
+ }
245
+
246
+ /**
247
+ * Round decimal numbers.
248
+ */
249
+ export function round(input: number, digits = 2): number {
250
+ const factor = 10 ** digits;
251
+
252
+ return Math.round(input * factor) / factor;
253
+ }
package/src/name.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { cssColors, invariant, isString, messages } from './utils';
1
+ import { cssColors } from './modules/css-colors';
2
+ import { invariant, isString, messages } from './modules/utils';
2
3
  import parseCSS from './parse-css';
3
4
 
4
5
  export default function name(input: string): string {
5
- invariant(!isString(input), messages.inputString);
6
+ invariant(isString(input), messages.inputString);
6
7
 
7
8
  const hex = parseCSS(input);
8
9
 
package/src/palette.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import hex2hsl from './hex2hsl';
2
2
  import hsl2hex from './hsl2hex';
3
+ import { invariant, isPlainObject, isString, messages } from './modules/utils';
3
4
  import parseCSS from './parse-css';
4
5
  import rotate from './rotate';
5
- import { invariant, isPlainObject, isString, messages } from './utils';
6
-
7
6
  import { PaletteOptions } from './types';
8
7
 
9
8
  export default function palette(input: string, options: PaletteOptions = {}): string[] {
10
- invariant(!isString(input), messages.inputString);
11
- invariant(!isPlainObject(options), messages.options);
9
+ invariant(isString(input), messages.inputString);
10
+ invariant(isPlainObject(options), messages.options);
12
11
 
13
12
  const { lightness, saturation, size = 6, type } = options;
14
13
  const hsl = hex2hsl(parseCSS(input));
@@ -18,17 +17,19 @@ export default function palette(input: string, options: PaletteOptions = {}): st
18
17
  case 'monochromatic': {
19
18
  const step = 80 / size;
20
19
 
21
- for (let i = size; i > 0; i--) {
22
- output.push(hsl2hex({ ...hsl, l: step * i }));
20
+ for (let index = size; index > 0; index--) {
21
+ output.push(hsl2hex({ ...hsl, l: step * index }));
23
22
  }
24
23
 
25
24
  break;
26
25
  }
27
26
  default: {
28
27
  const step = 360 / size;
28
+
29
29
  output.push(hsl2hex({ ...hsl, l: lightness || hsl.l, s: saturation || hsl.s }));
30
- for (let i = 1; i < size; i++) {
31
- const color = rotate(input, hsl.h + step * i);
30
+
31
+ for (let index = 1; index < size; index++) {
32
+ const color = rotate(input, hsl.h + step * index);
32
33
 
33
34
  output.push(hsl2hex({ ...hex2hsl(color), l: lightness || hsl.l, s: saturation || hsl.s }));
34
35
  }
package/src/parse-css.ts CHANGED
@@ -2,11 +2,11 @@ import hex2hsl from './hex2hsl';
2
2
  import hex2rgb from './hex2rgb';
3
3
  import hsl2hex from './hsl2hex';
4
4
  import hsl2rgb from './hsl2rgb';
5
+ import isValidHex from './is-valid-hex';
6
+ import { cssColors } from './modules/css-colors';
7
+ import { invariant, isString, messages } from './modules/utils';
5
8
  import rgb2hex from './rgb2hex';
6
9
  import rgb2hsl from './rgb2hsl';
7
- import { cssColors, invariant, isString, messages } from './utils';
8
- import isValidHex from './is-valid-hex';
9
-
10
10
  import { ColorTypes, Return } from './types';
11
11
 
12
12
  /**
@@ -16,7 +16,7 @@ export default function parseCSS<T extends ColorTypes = 'hex'>(
16
16
  input: string,
17
17
  output?: T,
18
18
  ): Return<T> {
19
- invariant(!isString(input), messages.inputString);
19
+ invariant(isString(input), messages.inputString);
20
20
  let result: any;
21
21
 
22
22
  const parsedInput = cssColors[input.toLowerCase() as keyof typeof cssColors] || input;
@@ -42,9 +42,10 @@ export default function parseCSS<T extends ColorTypes = 'hex'>(
42
42
  /(hsl|rgb)a?\((\d+)(?:,\s*|\s+)(\d+)%?(?:,\s*|\s+)(\d+)%?[^)]*\)/i,
43
43
  );
44
44
 
45
- invariant(!matches || matches.length !== 5, 'invalid CSS string');
45
+ invariant(Array.isArray(matches), 'invalid CSS string');
46
+ invariant(matches.length === 5, 'invalid CSS string');
46
47
 
47
- const [, model, hORr, sORg, lORb] = matches!;
48
+ const [, model, hORr, sORg, lORb] = matches;
48
49
  let hex;
49
50
  let hsl;
50
51
  let rgb;
@@ -76,6 +77,7 @@ export default function parseCSS<T extends ColorTypes = 'hex'>(
76
77
  result = rgb;
77
78
  break;
78
79
  }
80
+
79
81
  case 'hex':
80
82
  default: {
81
83
  result = hex;
package/src/rgb2hex.ts CHANGED
@@ -1,13 +1,12 @@
1
- import { invariant, isRGB, isRGBArray, messages } from './utils';
2
-
1
+ import { invariant, isRGB, isRGBArray, messages } from './modules/utils';
3
2
  import { RGB, RGBArray } from './types';
4
3
 
5
4
  /**
6
- * Convert a RGA object to hex.
5
+ * Convert an RGA object to hex.
7
6
  */
8
7
  export default function rgb2hex(input: RGB | RGBArray): string {
9
- invariant(!input, messages.input);
10
- invariant(!isRGBArray(input) && !isRGB(input), messages.invalid);
8
+ invariant(!!input, messages.input);
9
+ invariant(isRGBArray(input) || isRGB(input), messages.invalid);
11
10
 
12
11
  let r: number;
13
12
  let g: number;
package/src/rgb2hsl.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { invariant, isRGB, limit, messages } from './utils';
2
-
3
- import { RGB, HSL, RGBArray } from './types';
1
+ import { invariant, isRGB, limit, messages } from './modules/utils';
2
+ import { HSL, RGB, RGBArray } from './types';
4
3
 
5
4
  export default function rgb2hsl(input: RGB | RGBArray): HSL {
6
- invariant(!input, messages.input);
5
+ invariant(!!input, messages.input);
7
6
 
8
7
  let rgb: RGB = input as RGB;
9
8
 
@@ -11,7 +10,7 @@ export default function rgb2hsl(input: RGB | RGBArray): HSL {
11
10
  rgb = { r: input[0], g: input[1], b: input[2] };
12
11
  }
13
12
 
14
- invariant(!isRGB(rgb), messages.invalid);
13
+ invariant(isRGB(rgb), messages.invalid);
15
14
 
16
15
  const rLimit = limit(rgb.r, 'r') / 255;
17
16
  const gLimit = limit(rgb.g, 'g') / 255;
package/src/rotate.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import hex2hsl from './hex2hsl';
2
+ import { constrainDegrees, invariant, isNumber, isString, messages } from './modules/utils';
2
3
  import parseCSS from './parse-css';
3
4
  import { shift } from './shift';
4
- import { constrainDegrees, invariant, isNumber, isString, messages } from './utils';
5
5
 
6
6
  /**
7
7
  * Change the color hue
8
8
  */
9
9
  export default function rotate(input: string, degrees = 15): string {
10
- invariant(!isString(input), messages.inputString);
11
- invariant(!isNumber(degrees), 'degrees must be a number');
10
+ invariant(isString(input), messages.inputString);
11
+ invariant(isNumber(degrees), 'degrees must be a number');
12
12
 
13
13
  const hex = parseCSS(input);
14
14
  const { h } = hex2hsl(hex);
package/src/saturate.ts CHANGED
@@ -1,4 +1,4 @@
1
- import updater from './updater';
1
+ import updater from './modules/updater';
2
2
 
3
3
  /**
4
4
  * Increase color saturation
package/src/scheme.ts CHANGED
@@ -1,56 +1,44 @@
1
+ import { invariant, isString, messages } from './modules/utils';
1
2
  import parseCSS from './parse-css';
2
3
  import rotate from './rotate';
3
- import { invariant, isString, messages } from './utils';
4
-
5
4
  import { Scheme } from './types';
6
5
 
7
6
  /**
8
- * Get the scheme scheme for a color.
7
+ * Get the scheme for a color.
9
8
  */
10
9
  export default function scheme(input: string, type: Scheme = 'complementary'): string[] {
11
- invariant(!isString(input), messages.inputString);
10
+ invariant(isString(input), messages.inputString);
12
11
 
13
12
  const hex = parseCSS(input);
14
13
  const output: string[] = [];
15
14
 
16
15
  switch (type) {
17
16
  case 'analogous': {
18
- output.push(rotate(hex, -30));
19
- output.push(hex);
20
- output.push(rotate(hex, 30));
17
+ output.push(rotate(hex, -30), hex, rotate(hex, 30));
21
18
  break;
22
19
  }
23
20
  case 'complementary': {
24
- output.push(hex);
25
- output.push(rotate(hex, 180));
21
+ output.push(hex, rotate(hex, 180));
26
22
  break;
27
23
  }
24
+
28
25
  case 'split':
29
26
  case 'split-complementary': {
30
- output.push(hex);
31
- output.push(rotate(hex, 150));
32
- output.push(rotate(hex, 210));
27
+ output.push(hex, rotate(hex, 150), rotate(hex, 210));
33
28
  break;
34
29
  }
35
30
  case 'triadic': {
36
- output.push(hex);
37
- output.push(rotate(hex, 120));
38
- output.push(rotate(hex, 240));
31
+ output.push(hex, rotate(hex, 120), rotate(hex, 240));
39
32
  break;
40
33
  }
34
+
41
35
  case 'tetradic':
42
36
  case 'rectangle': {
43
- output.push(hex);
44
- output.push(rotate(hex, 60));
45
- output.push(rotate(hex, 180));
46
- output.push(rotate(hex, 240));
37
+ output.push(hex, rotate(hex, 60), rotate(hex, 180), rotate(hex, 240));
47
38
  break;
48
39
  }
49
40
  case 'square': {
50
- output.push(hex);
51
- output.push(rotate(hex, 90));
52
- output.push(rotate(hex, 180));
53
- output.push(rotate(hex, 270));
41
+ output.push(hex, rotate(hex, 90), rotate(hex, 180), rotate(hex, 270));
54
42
  break;
55
43
  }
56
44
  default: {
package/src/shift.ts CHANGED
@@ -1,16 +1,15 @@
1
- import hsl2hex from './hsl2hex';
2
1
  import hex2hsl from './hex2hsl';
2
+ import hsl2hex from './hsl2hex';
3
+ import { HSLKeys, invariant, isPlainObject, isString, messages, pick } from './modules/utils';
3
4
  import parseCSS from './parse-css';
4
- import { HSLKeys, invariant, isPlainObject, isString, messages, pick } from './utils';
5
-
6
5
  import { HSL, RGB } from './types';
7
6
 
8
7
  /**
9
8
  * Shift color properties
10
9
  */
11
10
  export function shift(input: string, options: Partial<HSL | RGB>): string {
12
- invariant(!isString(input), messages.inputString);
13
- invariant(!isPlainObject(options), messages.options);
11
+ invariant(isString(input), messages.inputString);
12
+ invariant(isPlainObject(options), messages.options);
14
13
 
15
14
  return hsl2hex({ ...hex2hsl(parseCSS(input)), ...pick(options, HSLKeys) });
16
15
  }
package/src/text-color.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import hex2rgb from './hex2rgb';
2
+ import { invariant, isString, messages } from './modules/utils';
2
3
  import parseCSS from './parse-css';
3
- import { invariant, isString, messages } from './utils';
4
4
 
5
5
  /**
6
6
  * Get the contrasted color for a given hex.
7
7
  */
8
8
  export default function textColor(input: string): string {
9
- invariant(!isString(input), messages.inputString);
9
+ invariant(isString(input), messages.inputString);
10
10
 
11
11
  const { r, g, b } = hex2rgb(parseCSS(input));
12
12
  const yiq = (r * 299 + g * 587 + b * 114) / 1000;
@@ -1,3 +1,4 @@
1
+ /* eslint-disable typescript-sort-keys/interface */
1
2
  export interface Analysis {
2
3
  brightnessDifference: number;
3
4
  colorDifference: number;
@@ -1 +0,0 @@
1
- {"version":3,"file":"hue2rgb.js","sourceRoot":"","sources":["../src/hue2rgb.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErD;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,KAAa,EAAE,MAAc,EAAE,CAAS;IACtE,SAAS,CACP,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EACrD,kCAAkC,CACnC,CAAC;IACF,IAAI,GAAG,GAAG,CAAC,CAAC;IAEZ,IAAI,GAAG,GAAG,CAAC,EAAE;QACX,GAAG,IAAI,CAAC,CAAC;KACV;IAED,IAAI,GAAG,GAAG,CAAC,EAAE;QACX,GAAG,IAAI,CAAC,CAAC;KACV;IAED,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,EAAE;QACf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KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;gBACtB,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;gBACtB,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;aACvB,CAAC;YACF,MAAM,CAAC,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;SAClC;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;SAClC;QAED,MAAM,CAAC,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KAClC;SAAM;QACL,MAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;KACjC;IAED,OAAO,MAAgB,CAAC;AAC1B,CAAC"}
package/esm/updater.js DELETED
@@ -1,20 +0,0 @@
1
- import hex2hsl from './hex2hsl';
2
- import parseCSS from './parse-css';
3
- import { shift } from './shift';
4
- import { constrain, invariant, isNumber, isString, messages } from './utils';
5
- /**
6
- * Update color properties
7
- */
8
- export default function updater(type, sign) {
9
- return function (input, amount) {
10
- var _a;
11
- invariant(!isString(input), messages.inputString);
12
- invariant(!isNumber(amount), messages.amount);
13
- var hex = parseCSS(input);
14
- var hsl = hex2hsl(hex);
15
- return shift(hex, (_a = {},
16
- _a[type] = constrain(hsl[type], amount, [0, 100], sign),
17
- _a));
18
- };
19
- }
20
- //# sourceMappingURL=updater.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"updater.js","sourceRoot":"","sources":["../src/updater.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,WAAW,CAAC;AAChC,OAAO,QAAQ,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE7E;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAqB,EAAE,IAAe;IACpE,OAAO,UAAC,KAAa,EAAE,MAAc;;QACnC,SAAS,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;QAClD,SAAS,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;QAE9C,IAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC5B,IAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;QAEzB,OAAO,KAAK,CAAC,GAAG;YACd,GAAC,IAAI,IAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC;gBACpD,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}