|
1 | | -'use strict' |
| 1 | +'use strict'; |
2 | 2 |
|
3 | 3 | const _color = require('color').default; |
| 4 | +const colorNormalize = require('color-normalize').default; |
4 | 5 | const { warn } = require('../../lib/loggers'); |
5 | 6 | const { background, defaultLine, defaults, lightLine } = require('./attributes'); |
6 | 7 |
|
@@ -42,26 +43,14 @@ const opacity = (cstr) => (cstr ? color(cstr).alpha() : 0); |
42 | 43 |
|
43 | 44 | /** |
44 | 45 | * Convert a color specifier to a 4-element `[r, g, b, a]` representation. |
| 46 | + * Accepts strings, numeric float arrays (`[0, 1]`), or uint8 arrays (`[0, 255]`). |
45 | 47 | * |
46 | | - * @param {*} cstr - color specifier |
| 48 | + * @param {*} input - color specifier or numeric array |
47 | 49 | * @param {'float'|'uint8'} [type='float'] - `'float'` returns `[r, g, b, a]` in `[0, 1]`; |
48 | 50 | * `'uint8'` returns a `Uint8Array` in `[0, 255]`. |
49 | 51 | * @return {Number[]|Uint8Array} |
50 | 52 | */ |
51 | | -const normalize = (cstr, type) => { |
52 | | - // color's `.rgb().array()` omits alpha when it is 1, so default `a` |
53 | | - // back to 1 to keep the 4-element shape callers expect. |
54 | | - const [r, g, b, a = 1] = color(cstr).rgb().array(); |
55 | | - if (type === 'uint8') { |
56 | | - const out = new Uint8Array(4); |
57 | | - out[0] = r; |
58 | | - out[1] = g; |
59 | | - out[2] = b; |
60 | | - out[3] = Math.floor(a * 255); |
61 | | - return out; |
62 | | - } |
63 | | - return [r / 255, g / 255, b / 255, a]; |
64 | | -}; |
| 53 | +const normalize = (input, type) => colorNormalize(input, type); |
65 | 54 |
|
66 | 55 | /** |
67 | 56 | * Build an `rgba(...)` string from a color and an explicit opacity value. |
@@ -160,8 +149,12 @@ const contrast = (cstr, lightAmount, darkAmount) => { |
160 | 149 |
|
161 | 150 | if (c.alpha() !== 1) c = color(combine(cstr, background)); |
162 | 151 | const newColor = c.isDark() |
163 | | - ? (lightAmount ? adjustLightness(c, lightAmount) : color(background)) |
164 | | - : (darkAmount ? adjustLightness(c, -darkAmount) : color(defaultLine)); |
| 152 | + ? lightAmount |
| 153 | + ? adjustLightness(c, lightAmount) |
| 154 | + : color(background) |
| 155 | + : darkAmount |
| 156 | + ? adjustLightness(c, -darkAmount) |
| 157 | + : color(defaultLine); |
165 | 158 |
|
166 | 159 | return newColor.rgb().string(); |
167 | 160 | }; |
|
0 commit comments