Skip to content

Commit

Permalink
Fix to-rgba with zero opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Mar 27, 2018
1 parent 6b848e5 commit 8c53f20
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ CompoundExpression.register(expressions, {
array(NumberType, 4),
[ColorType],
(ctx, [v]) => {
const {r, g, b, a} = v.evaluate(ctx);
return [255 * r / a, 255 * g / a, 255 * b / a, a];
return v.evaluate(ctx).toArray();
}
],
'rgb': [
Expand Down
15 changes: 12 additions & 3 deletions src/style-spec/util/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,18 @@ class Color {
* translucentGreen.toString(); // = "rgba(26,207,26,0.73)"
*/
toString(): string {
const transformRgb = (value: number) => Math.round(value * 255 / this.a);
const rgb = [this.r, this.g, this.b].map(transformRgb);
return `rgba(${rgb.concat(this.a).join(',')})`;
const [r, g, b, a] = this.toArray();
return `rgba(${Math.round(r)},${Math.round(g)},${Math.round(b)},${a})`;
}

toArray(): [number, number, number, number] {
const {r, g, b, a} = this;
return a === 0 ? [0, 0, 0, 0] : [
r * 255 / a,
g * 255 / a,
b * 255 / a,
a
];
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/integration/expression-tests/to-rgba/zero/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"expression": ["to-rgba", ["rgba", 0, 0, 0, 0]],
"inputs": [
[{}, {}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": true,
"isZoomConstant": true,
"type": "array<number, 4>"
},
"outputs": [[0, 0, 0, 0]],
"serialized": ["to-rgba", ["rgba", 0, 0, 0, 0]]
}
}

0 comments on commit 8c53f20

Please sign in to comment.