rgb: use precise floats internally for accurate rgb-hsb conversions

This commit is contained in:
jean-emmanuel 2016-11-03 23:40:19 +01:00
parent d1588e8b39
commit 549e8ea223
2 changed files with 7 additions and 7 deletions

View file

@ -171,13 +171,12 @@ Rgb.prototype.setValue = function(v, options={}){
if (!v || v.length!=3) return
for (i in [0,1,2]) {
v[i] = roundTo(clip(v[i],[0,255]),this.widgetData.precision)
v[i] = clip(v[i],[0,255])
}
var hsb = rgbToHsb({r:v[0],g:v[1],b:v[2]})
if (!options.dragged) {
this.faders.h.setValue(hsb.h, {sync: false, send:false, dragged:false})
this.faders.s.setValue(hsb.s, {sync: false, send:false, dragged:false})
this.faders.b.setValue(hsb.b, {sync: false, send:false, dragged:false})
}
@ -188,19 +187,20 @@ Rgb.prototype.setValue = function(v, options={}){
if (options.send) this.sendValue()
if (options.sync) this.widget.trigger({type:'sync', id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId, options:options})
this.update({nohue:options.nohue})
this.update({dragged:options.dragged, nohue:options.nohue || (v[0]==v[1]&&v[1]==v[2])})
}
Rgb.prototype.update = function(options={}) {
if (!options.nohue) {
if (!options.nohue && !options.dragged) {
var hue = hsbToRgb({h:this.hsb.h,s:100,b:100})
this.hue = `rgb(${hue.r},${hue.g},${hue.b})`
this.hue = `rgb(${Math.round(hue.r)},${Math.round(hue.g)},${Math.round(hue.b)})`
this.wrapper[0].setAttribute('style',`background:${this.hue}`)
this.faders.h.setValue(this.hsb.h, {sync: false, send:false, dragged:false})
}
for (i in this.inputs) [
this.inputs[i].val(this.value[i])
this.inputs[i].val(roundTo(this.value[i],this.widgetData.precision))
]
this.draw()

View file

@ -61,7 +61,7 @@ module.exports = {
else if(h<360) {rgb.r=t1; rgb.g=t2; rgb.b=t1-t3}
else {rgb.r=0; rgb.g=0; rgb.b=0}
}
return {r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b)}
return rgb
},
rgbToHsb: function (rgb) {