From b267837de9c4606f35e3f4cb646ad0d44e2d2ea3 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 22 Mar 2018 13:33:41 +0100 Subject: [PATCH] rgb: add `range` property (#252) --- CHANGELOG.md | 3 ++- resources/docs/widgets/pads.md | 8 +++++++- src/browser/js/app/widgets/pads/rgb.js | 26 ++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4cf8f3e..e99dd731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ - widgets - matrices: support setting `split` property as a string to allow using children's indexes in their address (`$` is replaced with the index number) - text: `wrap` property doesn't break words by default anymore - + - rgb: added `range` property + ## 0.27.2 - bug fixes diff --git a/resources/docs/widgets/pads.md b/resources/docs/widgets/pads.md index e4a5bedd..67c81b57 100644 --- a/resources/docs/widgets/pads.md +++ b/resources/docs/widgets/pads.md @@ -54,7 +54,7 @@ Pads are multidimensional widgets that output multiple values. ### `doubleTap` - type: `boolean|string` - default: `false` -- usage: set to `true` to make the fader reset to its `default` value when receiving a double tap. `doubleTap` can also be an osc address, which case the widget will just send an osc message (`/ `), +- usage: set to `true` to make the fader reset to its `default` value when receiving a double tap. `doubleTap` can also be an osc address, which case the widget will just send an osc message (`/ `), ### `split` @@ -149,9 +149,15 @@ RGB is a variant of XY, it outputs rgb values between 0 and 255. - default: `false` - usage: by default, dragging the widget will modify it's value starting from its last value. Setting this to true will make it snap directly to the mouse/touch position. +### `range` +- type: `object` +- default: `{"min":0,"max":255}` +- usage: `range` defines the widget's output scale. + ### `split` - type: `boolean|object` - default: `false` - usage: - set to `true` to send separate osc messages for `r` and `g` & `b`. The `address` will be the same as the widget's with `/r`, `/g` or `/b` appended to it - can be set as an object to specify a different `address` : `['/r', '/b', '/b']` +- note: the widget will only respond to its original osc address, not to the splitted version diff --git a/src/browser/js/app/widgets/pads/rgb.js b/src/browser/js/app/widgets/pads/rgb.js index 79f9b3ed..b98d6f93 100644 --- a/src/browser/js/app/widgets/pads/rgb.js +++ b/src/browser/js/app/widgets/pads/rgb.js @@ -33,6 +33,7 @@ module.exports = class Rgb extends Pad { input: true, snap:false, spring:false, + range: {min:0,max:255}, _value: 'value', default: '', @@ -122,7 +123,7 @@ module.exports = class Rgb extends Pad { } - this.setValue([0,0,0]) + this.setValue([this.getProp('range').min, this.getProp('range').min, this.getProp('range').min]) } @@ -139,7 +140,15 @@ module.exports = class Rgb extends Pad { this.update({nohue:!hue}) - var rgb = hsbToRgb(this.hsb) + var rgb = hsbToRgb(this.hsb), + {min, max} = this.getProp('range') + + if (min !== 0 || max !== 255) { + for (var k in rgb) { + rgb[k] = min + max * rgb[k] / 255 + } + } + if (rgb.r != this.value[0] || rgb.g != this.value[1] || rgb.b != this.value[2]) { this.setValue([rgb.r, rgb.g, rgb.b],{send:true,sync:true,dragged:true,nohue:!hue}) } @@ -153,10 +162,19 @@ module.exports = class Rgb extends Pad { if (this.touched && !options.dragged) return this.setValueTouchedQueue = [v, options] for (let i in [0,1,2]) { - v[i] = clip(v[i],[0,255]) + v[i] = clip(v[i],[this.getProp('range').min, this.getProp('range').max]) } - var hsb = rgbToHsb({r:v[0],g:v[1],b:v[2]}) + var rgb = {r:v[0],g:v[1],b:v[2]}, + {min, max} = this.getProp('range') + + if (min !== 0 || max !== 255) { + for (var k in rgb) { + rgb[k] = (rgb[k] - min ) * 255 / max + } + } + + var hsb = rgbToHsb(rgb) if (!options.dragged) { this.pad.setValue([hsb.s, hsb.b], {sync: false, send:false, dragged:false})