From f0cac767184937b8ddbec2914d5edaa003a91de9 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Tue, 3 Aug 2021 23:51:24 +0200 Subject: [PATCH] Add expression for Math.abs --- src/ol/style/expressions.js | 12 ++++++++++++ test/browser/spec/ol/style/expressions.test.js | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/ol/style/expressions.js b/src/ol/style/expressions.js index 8f9aa6bb1d..5c33fb0da9 100644 --- a/src/ol/style/expressions.js +++ b/src/ol/style/expressions.js @@ -27,6 +27,7 @@ import {asArray, isStringColor} from '../color.js'; * * `['clamp', value, low, high]` clamps `value` between `low` and `high` * * `['%', value1, value2]` returns the result of `value1 % value2` (modulo) * * `['^', value1, value2]` returns the value of `value1` raised to the `value2` power + * * `['abs', value1]` returns the absolute value of `value1` * * * Transform operators: * * `['case', condition1, output1, ...conditionN, outputN, fallback]` selects the first output whose corresponding @@ -551,6 +552,17 @@ Operators['^'] = { }, }; +Operators['abs'] = { + getReturnType: function (args) { + return ValueTypes.NUMBER; + }, + toGlsl: function (context, args) { + assertArgsCount(args, 1); + assertNumbers(args); + return `abs(${expressionToGlsl(context, args[0])})`; + }, +}; + Operators['>'] = { getReturnType: function (args) { return ValueTypes.BOOLEAN; diff --git a/test/browser/spec/ol/style/expressions.test.js b/test/browser/spec/ol/style/expressions.test.js index caabe63156..fcf1f9e004 100644 --- a/test/browser/spec/ol/style/expressions.test.js +++ b/test/browser/spec/ol/style/expressions.test.js @@ -235,6 +235,12 @@ describe('ol.style.expressions', function () { expect(expressionToGlsl(context, ['^', ['%', ['time'], 10], 2])).to.eql( 'pow(mod(u_time, 10.0), 2.0)' ); + expect( + expressionToGlsl(context, [ + 'abs', + ['-', ['get', 'attr3'], ['get', 'attr2']], + ]) + ).to.eql('abs((a_attr3 - a_attr2))'); expect(expressionToGlsl(context, ['>', 10, ['get', 'attr4']])).to.eql( '(10.0 > a_attr4)' );