Skip to content

Commit

Permalink
Color operations, take two
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick authored and jfirebaugh committed Jun 17, 2015
1 parent a669546 commit 4c92454
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
9 changes: 8 additions & 1 deletion js/style/paint_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var reference = require('./reference');
var parseCSSColor = require('csscolorparser').parseCSSColor;
var colorOps = require('color-ops');

module.exports = {};

Expand All @@ -14,7 +15,13 @@ reference.paint.forEach(function(className) {
value = prop.default;

if (value === undefined) continue;
if (prop.type === 'color') value = parseCSSColor(value);
if (prop.type === 'color') {
if (Array.isArray(value)) {
value = colorOps[value[0]](value[2], value[1]);
} else {
value = parseCSSColor(value);
}
}

Calculated.prototype[p] = value;
}
Expand Down
15 changes: 15 additions & 0 deletions js/style/style_constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ exports.resolve = function resolve(value, constants) {

value = resolveInner(value);

function resolveArray(value) {
if (Array.isArray(value)) {
for (var x = 0; x < value.length; x++) {
value[x] = resolveArray(value[x]);
if (value[x] in constants) {
value[x] = resolveInner(value[x]);
}
}
}
return value;
}

value = resolveArray(value);

if (Array.isArray(value)) {
// avoid mutating the array in-place
value = value.slice();
Expand All @@ -38,6 +52,7 @@ exports.resolve = function resolve(value, constants) {
value.stops = value.stops.slice();

for (i = 0; i < value.stops.length; i++) {
value.stops[i][1] = resolveArray(value.stops[i][1]);
if (value.stops[i][1] in constants) {
value.stops[i] = [
value.stops[i][0],
Expand Down
25 changes: 24 additions & 1 deletion js/style/style_declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var parseCSSColor = require('csscolorparser').parseCSSColor;
var mapboxGLFunction = require('mapbox-gl-function');
var colorOps = require('color-ops');
var util = require('../util/util');

module.exports = StyleDeclaration;
Expand Down Expand Up @@ -63,9 +64,31 @@ function transitioned(calculate) {

var colorCache = {};

function parseColorArray(value) {
if (typeof value[0] === 'number') return value;

var op = value[0];
var degree = value[1];

value[2] = parseColorArray(value[2]);
if (op === 'mix') {
value[3] = parseColorArray(value[3]);
return colorOps[op](value[2], value[3], degree);
}
return colorOps[op](value[2], degree);
}

function replaceStrings(color) {
if (typeof color === 'string') return parseCSSColor(color);
color[2] = replaceStrings(color[2]);
if (color[3]) color[3] = replaceStrings(color[3]);
return color;
}

function parseColor(value) {
if (colorCache[value]) return colorCache[value];
var color = prepareColor(parseCSSColor(value));
var parsed = parseColorArray(replaceStrings(value));
var color = prepareColor(parsed);
colorCache[value] = color;
return color;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"brfs": "1.2.0",
"color-ops": "^2.0.0",
"csscolorparser": "~1.0.2",
"envify": "2.0.1",
"feature-filter": "1.0.2",
Expand Down
39 changes: 39 additions & 0 deletions test/js/style/style_constant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@ test('StyleConstant.resolve', function(t) {
});
t.end();
});

t.test('resolves color operation values', function(t) {
var simple = ["lighten", -20, "@black"];
var lighten = ["lighten", 20, ["mix", 50, "@white", "@black"]];
var darken = ["mix", 50, ["lighten", 20, "@black"], "green"];

var constants = {
"@white": { type: 'color', value: "#FFF" },
"@black": { type: 'color', value: "#000" },
"@a": "a"
};

t.deepEqual(StyleConstant.resolve(simple, constants),
["lighten", -20, "#000"]
);
t.deepEqual(StyleConstant.resolve(lighten, constants),
["lighten", 20, ["mix", 50, "#FFF", "#000"]]
);
t.deepEqual(StyleConstant.resolve(darken, constants),
["mix", 50, ["lighten", 20, "#000"], "green"]
);

t.end();
});

t.test('resolves color operations in functions', function(t) {
var fun = {
"stops": [[0, "@a"], [1, ["lighten", -20, "@a"]]]
};
var constants = {
"@a": { type: 'color', value: "#ccc" }
};

t.deepEqual(StyleConstant.resolve(fun, constants), {
"stops": [[0, "#ccc"], [1, ["lighten", -20, "#ccc"]]]
});

t.end();
});
});

test('StyleConstant.resolveAll', function(t) {
Expand Down
5 changes: 5 additions & 0 deletions test/js/style/style_declaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ test('StyleDeclaration', function(t) {
t.deepEqual(new StyleDeclaration(reference, 'red').calculate(0), [ 1, 0, 0, 1 ]);
t.deepEqual(new StyleDeclaration(reference, '#ff00ff').calculate(0), [ 1, 0, 1, 1 ]);
t.deepEqual(new StyleDeclaration(reference, { stops: [[0, '#f00'], [1, '#0f0']] }).calculate(0), [1, 0, 0, 1]);
t.deepEqual(new StyleDeclaration(reference, ['lighten', -50, '#FFF']).calculate(0),
[0.5, 0.5, 0.5, 1]);
t.deepEqual(new StyleDeclaration(reference, ['lighten', -30, ['mix', 20, '#551A8B',
['saturate', 10, '#FF0000']]]).calculate(0),
[0.2804597701149426, 0.006599053414469202, 0.035279554792739365, 1]);
// cached
t.deepEqual(new StyleDeclaration(reference, '#ff00ff').calculate(0), [ 1, 0, 1, 1 ]);
t.deepEqual(new StyleDeclaration(reference, 'rgba(255, 51, 0, 1)').calculate(0), [ 1, 0.2, 0, 1 ]);
Expand Down

0 comments on commit 4c92454

Please sign in to comment.