Skip to content

Commit 9b654dd

Browse files
authored
Merge pull request #1443 from dfcreative/fast-color-parsing
Fast color parsing
2 parents ec57703 + 9158bdb commit 9b654dd

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"3d-view": "^2.0.0",
5858
"alpha-shape": "^1.0.0",
5959
"arraytools": "^1.0.0",
60+
"color-rgba": "^1.0.4",
6061
"convex-hull": "^1.0.3",
6162
"country-regex": "^1.1.0",
6263
"d3": "^3.5.12",

src/lib/gl_format_color.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@
99

1010
'use strict';
1111

12-
var tinycolor = require('tinycolor2');
1312
var isNumeric = require('fast-isnumeric');
13+
var rgba = require('color-rgba');
1414

1515
var Colorscale = require('../components/colorscale');
1616
var colorDflt = require('../components/color/attributes').defaultLine;
1717

18-
var str2RgbaArray = require('./str2rgbarray');
19-
18+
var colorDfltRgba = rgba(colorDflt);
2019
var opacityDflt = 1;
2120

2221
function calculateColor(colorIn, opacityIn) {
23-
var colorOut = str2RgbaArray(colorIn);
22+
var colorOut = colorIn;
2423
colorOut[3] *= opacityIn;
2524
return colorOut;
2625
}
2726

2827
function validateColor(colorIn) {
29-
return tinycolor(colorIn).isValid() ? colorIn : colorDflt;
28+
if(isNumeric(colorIn)) return colorDfltRgba;
29+
30+
var colorOut = rgba(colorIn);
31+
32+
return colorOut.length ? colorOut : colorDfltRgba;
3033
}
3134

3235
function validateOpacity(opacityIn) {
@@ -50,11 +53,13 @@ function formatColor(containerIn, opacityIn, len) {
5053
)
5154
);
5255
}
53-
else sclFunc = validateColor;
56+
else {
57+
sclFunc = validateColor;
58+
}
5459

5560
if(isArrayColorIn) {
5661
getColor = function(c, i) {
57-
return c[i] === undefined ? colorDflt : sclFunc(c[i]);
62+
return c[i] === undefined ? colorDfltRgba : rgba(sclFunc(c[i]));
5863
};
5964
}
6065
else getColor = validateColor;
@@ -73,7 +78,7 @@ function formatColor(containerIn, opacityIn, len) {
7378
colorOut[i] = calculateColor(colori, opacityi);
7479
}
7580
}
76-
else colorOut = calculateColor(colorIn, opacityIn);
81+
else colorOut = calculateColor(rgba(colorIn), opacityIn);
7782

7883
return colorOut;
7984
}

src/lib/str2rgbarray.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
'use strict';
1111

12-
var tinycolor = require('tinycolor2');
13-
var arrtools = require('arraytools');
12+
var rgba = require('color-rgba');
1413

1514
function str2RgbaArray(color) {
16-
color = tinycolor(color);
17-
return arrtools.str2RgbaArray(color.toRgbString());
15+
var colorOut = rgba(color);
16+
return colorOut.length ? colorOut : [0, 0, 0, 1];
1817
}
1918

2019
module.exports = str2RgbaArray;

0 commit comments

Comments
 (0)