Skip to content

Fast color parsing #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"3d-view": "^2.0.0",
"alpha-shape": "^1.0.0",
"arraytools": "^1.0.0",
"color-rgba": "^1.0.4",
"convex-hull": "^1.0.3",
"country-regex": "^1.1.0",
"d3": "^3.5.12",
Expand Down
21 changes: 13 additions & 8 deletions src/lib/gl_format_color.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@

'use strict';

var tinycolor = require('tinycolor2');
var isNumeric = require('fast-isnumeric');
var rgba = require('color-rgba');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add color-rgba to the package.json and push again?

I'll be interesting to see if the tests pass on CI ...


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

var str2RgbaArray = require('./str2rgbarray');

var colorDfltRgba = rgba(colorDflt);
var opacityDflt = 1;

function calculateColor(colorIn, opacityIn) {
var colorOut = str2RgbaArray(colorIn);
var colorOut = colorIn;
colorOut[3] *= opacityIn;
return colorOut;
}

function validateColor(colorIn) {
return tinycolor(colorIn).isValid() ? colorIn : colorDflt;
if(isNumeric(colorIn)) return colorDfltRgba;

var colorOut = rgba(colorIn);

return colorOut.length ? colorOut : colorDfltRgba;
}

function validateOpacity(opacityIn) {
Expand All @@ -50,11 +53,13 @@ function formatColor(containerIn, opacityIn, len) {
)
);
}
else sclFunc = validateColor;
else {
sclFunc = validateColor;
}

if(isArrayColorIn) {
getColor = function(c, i) {
return c[i] === undefined ? colorDflt : sclFunc(c[i]);
return c[i] === undefined ? colorDfltRgba : rgba(sclFunc(c[i]));
};
}
else getColor = validateColor;
Expand All @@ -73,7 +78,7 @@ function formatColor(containerIn, opacityIn, len) {
colorOut[i] = calculateColor(colori, opacityi);
}
}
else colorOut = calculateColor(colorIn, opacityIn);
else colorOut = calculateColor(rgba(colorIn), opacityIn);

return colorOut;
}
Expand Down
7 changes: 3 additions & 4 deletions src/lib/str2rgbarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

'use strict';

var tinycolor = require('tinycolor2');
var arrtools = require('arraytools');
var rgba = require('color-rgba');

function str2RgbaArray(color) {
color = tinycolor(color);
return arrtools.str2RgbaArray(color.toRgbString());
var colorOut = rgba(color);
return colorOut.length ? colorOut : [0, 0, 0, 1];
}

module.exports = str2RgbaArray;