From f43637cc33a335d51aaac087a86a9b09441aa7ed Mon Sep 17 00:00:00 2001 From: Olivier Guyot Date: Tue, 22 Oct 2019 16:41:52 +0200 Subject: [PATCH] Shader Builder / support strings in formatColor --- src/ol/webgl/ShaderBuilder.js | 8 ++++---- test/spec/ol/webgl/shaderbuilder.test.js | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ol/webgl/ShaderBuilder.js b/src/ol/webgl/ShaderBuilder.js index 31b0a9f95a..f4615fc593 100644 --- a/src/ol/webgl/ShaderBuilder.js +++ b/src/ol/webgl/ShaderBuilder.js @@ -26,13 +26,13 @@ export function formatArray(array) { /** * Will normalize and converts to string a color array compatible with GLSL. - * @param {Array} colorArray Color in [r, g, b, a] array form, with RGB components in the - * 0..255 range and the alpha component in the 0..1 range. Note that if the A component is + * @param {string|import("../color.js").Color} color Color either in string format or [r, g, b, a] array format, + * with RGB components in the 0..255 range and the alpha component in the 0..1 range. Note that if the A component is * missing, only 3 values will be output. * @returns {string} The color components concatenated in `1.0, 1.0, 1.0, 1.0` form. */ -export function formatColor(colorArray) { - return colorArray.map(function(c, i) { +export function formatColor(color) { + return asArray(color).map(function(c, i) { return i < 3 ? c / 255 : c; }).map(formatNumber).join(', '); } diff --git a/test/spec/ol/webgl/shaderbuilder.test.js b/test/spec/ol/webgl/shaderbuilder.test.js index ace6047082..0f985a1c77 100644 --- a/test/spec/ol/webgl/shaderbuilder.test.js +++ b/test/spec/ol/webgl/shaderbuilder.test.js @@ -29,6 +29,11 @@ describe('ol.webgl.ShaderBuilder', function() { it('normalizes color and outputs numbers with dot separators', function() { expect(formatColor([100, 0, 255, 1])).to.eql('0.39215686274509803, 0.0, 1.0, 1.0'); }); + it('handles colors in string format', function() { + expect(formatColor('red')).to.eql('1.0, 0.0, 0.0, 1.0'); + expect(formatColor('rgb(100, 0, 255)')).to.eql('0.39215686274509803, 0.0, 1.0, 1.0'); + expect(formatColor('rgba(100, 0, 255, 0.3)')).to.eql('0.39215686274509803, 0.0, 1.0, 0.3'); + }); }); describe('getSymbolVertexShader', function() {