Skip to content

Commit 7deb5b3

Browse files
committed
Switch back to color-normalize, but keep it in one place
1 parent b61b9bb commit 7deb5b3

3 files changed

Lines changed: 55 additions & 39 deletions

File tree

package-lock.json

Lines changed: 41 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@turf/meta": "^7.3.5",
7676
"base64-arraybuffer": "^1.0.2",
7777
"color": "^5.0.3",
78+
"color-normalize": "^2.1.1",
7879
"country-iso-search": "^0.1.1",
7980
"d3-force": "^1.2.1",
8081
"d3-format": "^1.4.5",
@@ -160,6 +161,8 @@
160161
"color": {
161162
"color-string": "^2.1.4"
162163
},
164+
"color-normalize": "^2.1.1",
165+
"color-rgba": "^3.1.1",
163166
"falafel": {
164167
"acorn": "^8.1.1"
165168
}

src/components/color/index.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
'use strict'
1+
'use strict';
22

33
const _color = require('color').default;
4+
const colorNormalize = require('color-normalize').default;
45
const { warn } = require('../../lib/loggers');
56
const { background, defaultLine, defaults, lightLine } = require('./attributes');
67

@@ -42,26 +43,14 @@ const opacity = (cstr) => (cstr ? color(cstr).alpha() : 0);
4243

4344
/**
4445
* 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]`).
4547
*
46-
* @param {*} cstr - color specifier
48+
* @param {*} input - color specifier or numeric array
4749
* @param {'float'|'uint8'} [type='float'] - `'float'` returns `[r, g, b, a]` in `[0, 1]`;
4850
* `'uint8'` returns a `Uint8Array` in `[0, 255]`.
4951
* @return {Number[]|Uint8Array}
5052
*/
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);
6554

6655
/**
6756
* Build an `rgba(...)` string from a color and an explicit opacity value.
@@ -160,8 +149,12 @@ const contrast = (cstr, lightAmount, darkAmount) => {
160149

161150
if (c.alpha() !== 1) c = color(combine(cstr, background));
162151
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);
165158

166159
return newColor.rgb().string();
167160
};

0 commit comments

Comments
 (0)