Skip to content

Commit

Permalink
Use toString() for to-string implementation; fix premultiplication bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Dec 14, 2017
1 parent 195690f commit 4a4f1ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ function rgba(ctx, [r, g, b, a]) {
r = r.evaluate(ctx);
g = g.evaluate(ctx);
b = b.evaluate(ctx);
a = a && a.evaluate(ctx);
const error = validateRGBA(r, g, b, a);
const alpha = a ? a.evaluate(ctx) : 1;
const error = validateRGBA(r, g, b, alpha);
if (error) throw new RuntimeError(error);
return new Color(r / 255, g / 255, b / 255, a);
return new Color(r / 255 * alpha, g / 255 * alpha, b / 255 * alpha, alpha);
}

function has(key, obj) {
Expand Down Expand Up @@ -101,7 +101,7 @@ CompoundExpression.register(expressions, {
if (v === null || type === 'string' || type === 'number' || type === 'boolean') {
return String(v);
} else if (v instanceof Color) {
return `rgba(${v.r * 255},${v.g * 255},${v.b * 255},${v.a})`;
return v.toString();
} else {
return JSON.stringify(v);
}
Expand All @@ -117,7 +117,7 @@ CompoundExpression.register(expressions, {
[ColorType],
(ctx, [v]) => {
const {r, g, b, a} = v.evaluate(ctx);
return [255 * r, 255 * g, 255 * b, a];
return [255 * r / a, 255 * g / a, 255 * b / a, a];
}
],
'rgb': [
Expand Down
15 changes: 15 additions & 0 deletions test/integration/expression-tests/to-rgba/alpha/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"expression": ["to-rgba", ["rgba", 0, 128, 255, 0.5]],
"inputs": [
[{}, {}]
],
"expected": {
"outputs": [[0, 128, 255, 0.5]],
"compiled": {
"result": "success",
"isZoomConstant": true,
"isFeatureConstant": true,
"type": "array<number, 4>"
}
}
}

0 comments on commit 4a4f1ff

Please sign in to comment.