From bae7ce44f9dfc067c22d01ca49610bd57b3f557c Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 20 Nov 2018 09:37:43 +0100 Subject: [PATCH] meter: allow setting gradient as an object with numeric breakpoints --- CHANGELOG.md | 1 + src/client/app/widgets/plots/meter.js | 8 ++++++- src/client/app/widgets/sliders/fader.js | 32 +++++++++++++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f871f417..447620c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - widgets - formulas: added `indexOf` function - **deprecated** `multifader` / `multipush` / `multitoggle` in favor of the generic `matrix` widget + - meter: `gradient` can be set as an object to define uneven colors breakpoints - mobile - button for preventing device sleep diff --git a/src/client/app/widgets/plots/meter.js b/src/client/app/widgets/plots/meter.js index e69e5897..c2600635 100644 --- a/src/client/app/widgets/plots/meter.js +++ b/src/client/app/widgets/plots/meter.js @@ -17,7 +17,13 @@ module.exports = class Meter extends Fader { horizontal: {type: 'boolean', value: false, help: 'See fader\'s `horizontal`'}, pips: {type: 'boolean', value: false, help: 'See fader\'s `pips`'}, dashed: {type: 'boolean', value: false, help: 'See fader\'s `dashed`'}, - gradient: {type: 'array', value: [], help: 'When set, the meter\'s gauge will be filled with a linear color gradient : each item must be a CSS color string. Example: `[\'blue\', \'red\']`'}, + gradient: {type: 'array|object', value: [], help: [ + 'When set, the meter\'s gauge will be filled with a linear color gradient', + '- each item must be a CSS color string.', + '- as an `object`: each key must be a number between 0 and 1', + '- each item must be a CSS color string.', + 'Examples: `[\'blue\', \'red\']`, {\'0\': \'blue\', \'0.9\': \'blue\', \'1\': \'red\'} ' + ]}, }, ['target', 'precision', 'bypass'], { diff --git a/src/client/app/widgets/sliders/fader.js b/src/client/app/widgets/sliders/fader.js index 334d482f..575b8d0a 100644 --- a/src/client/app/widgets/sliders/fader.js +++ b/src/client/app/widgets/sliders/fader.js @@ -204,18 +204,40 @@ module.exports = class Fader extends Slider { } if (this.getProp('gradient')) { + var colors = this.getProp('gradient') - if (Array.isArray(colors) && colors.length > 1) { + + if ( + (Array.isArray(colors) && colors.length > 1) || + (typeof colors === 'object' && Object.keys(colors).length > 1) + ) { + + var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0) + try { - var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0) - for (var i in colors) { - grad.addColorStop(i / (colors.length - 1), colors[i]) + if (Array.isArray(colors)) { + + for (var i in colors) { + grad.addColorStop(i / (colors.length - 1), colors[i]) + } + this.colors.gradient = grad + + } else { + + for (var k in colors) { + if (!isNaN(k)) { + grad.addColorStop(clip(k, [0, 1]), colors[k]) + } + } + this.colors.gradient = grad + } - this.colors.gradient = grad } catch(err) { this.errors.gradient = err } + } + }