From 105621c67593dd3036e19da0033480f32ffa1655 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 14 Dec 2017 13:41:08 -0800 Subject: [PATCH] Use toString() for to-string implementation; fix premultiplication bugs --- src/style-spec/expression/definitions/index.js | 8 ++++---- .../expression-tests/to-rgba/alpha/test.json | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/integration/expression-tests/to-rgba/alpha/test.json diff --git a/src/style-spec/expression/definitions/index.js b/src/style-spec/expression/definitions/index.js index e398d409de6..38388b19a1b 100644 --- a/src/style-spec/expression/definitions/index.js +++ b/src/style-spec/expression/definitions/index.js @@ -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); + a = a ? a.evaluate(ctx) : 1; const error = validateRGBA(r, g, b, a); if (error) throw new RuntimeError(error); - return new Color(r / 255, g / 255, b / 255, a); + return new Color(r / 255 * a, g / 255 * a, b / 255 * a, a); } function has(key, obj) { @@ -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); } @@ -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': [ diff --git a/test/integration/expression-tests/to-rgba/alpha/test.json b/test/integration/expression-tests/to-rgba/alpha/test.json new file mode 100644 index 00000000000..56656a7adf2 --- /dev/null +++ b/test/integration/expression-tests/to-rgba/alpha/test.json @@ -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" + } + } +}