diff --git a/lib/server.js b/lib/server.js index 3f1955732..198418f6b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1639,18 +1639,18 @@ class WebServer { is_rgb_string: true, getParser: (entity, attr, state) => { state = state || {val: '#000000'}; - let str = state.val || '#000000'; - if (str.charAt(0) === '#') { + let str = (state.val || '#000000').toString(); + if (str[0] === '#') { str = str.substring(1); } - if(!/^[\da-fA-F]{6}/.test(str)) { + if (!/^[\da-fA-F]{6}/.test(str)) { this.log.error('Malformed rgb string ' + str + ' expecting six hex digits.'); return; } - //from rgb to hsv: - const r = parseInt(str.substr(0, 2), 16) * 100/255; - const g = parseInt(str.substr(2, 2), 16) * 100/255; - const b = parseInt(str.substr(4, 2), 16) * 100/255; + // from rgb to hsv: + const r = parseInt(str.substr(0, 2), 16) * 100 / 255; + const g = parseInt(str.substr(2, 2), 16) * 100 / 255; + const b = parseInt(str.substr(4, 2), 16) * 100 / 255; const hsv = this._RGB2HSV(r, g, b); entity.attributes.hs_color = [hsv[0], hsv[1]]; attr.value = hsv[2]; //store value in attribute. @@ -1673,10 +1673,11 @@ class WebServer { return; } - const redState = control.states.find(s => s.id && s.name === 'RED'); + const redState = control.states.find(s => s.id && s.name === 'RED'); const greenState = control.states.find(s => s.id && s.name === 'GREEN'); - const blueState = control.states.find(s => s.id && s.name === 'BLUE'); + const blueState = control.states.find(s => s.id && s.name === 'BLUE'); const whiteState = control.states.find(s => s.id && s.name === 'WHITE'); + if (redState && redState.id && greenState && greenState.id && blueState && blueState.id) { //create main attribute for red: const attribute = { @@ -1687,7 +1688,7 @@ class WebServer { max: objects[redState.id].common.max || 100, getParser: (entity, attr, state) => { state = state || {val: 0}; - const r = state.val / attr.max * 255; //ok, we get red. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS. + const r = state.val / attr.max * 255; // ok, we get red. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS. const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value || 100]; const rgb = this._HSV2RGB(hsv[0], hsv[1], hsv[2]); const hsv_new = this._RGB2HSV(r, rgb.g, rgb.b); @@ -1703,7 +1704,7 @@ class WebServer { max: objects[greenState.id].common.max || 100, getParser: (entity, attr, state) => { state = state || {val: 0}; - const g = state.val / attr.max * 255; //ok, we get green. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS. + const g = state.val / attr.max * 255; // ok, we get green. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS. const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value || 100]; const rgb = this._HSV2RGB(hsv[0], hsv[1], hsv[2]); const hsv_new = this._RGB2HSV(rgb.r, g, rgb.b); @@ -1719,8 +1720,8 @@ class WebServer { max: objects[blueState.id].common.max || 100, getParser: (entity, attr, state) => { state = state || {val: 0}; - const b = state.val / attr.max * 255; //ok, we get blue. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS. - const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value ||100]; + const b = state.val / attr.max * 255; // ok, we get blue. Now do kind of a hack, calculate RGB from current setting and change r and recaluclate HS. + const hsv = [entity.attributes.hs_color[0], entity.attributes.hs_color[1], attribute.value || 100]; const rgb = this._HSV2RGB(hsv[0], hsv[1], hsv[2]); const hsv_new = this._RGB2HSV(rgb.r, rgb.g, b); entity.attributes.hs_color = [hsv_new[0], hsv_new[1]];