Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICP-6885 Fixes error setting color when hue is 0 #3633

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def setColor(value) {
def c = value.hex.findAll(/[0-9a-fA-F]{2}/).collect { Integer.parseInt(it, 16) }
result << zwave.switchColorV3.switchColorSet(red: c[0], green: c[1], blue: c[2], warmWhite: 0, coldWhite: 0)
} else {
def hue = value.hue ?: device.currentValue("hue")
def saturation = value.saturation ?: device.currentValue("saturation")
def hue = (value.hue == null) ? device.currentValue("hue") : value.hue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Color Control capability is defined as requiring hue and saturation so I don't think we need to guard against null inputs. Also other RGB DTHs like https://github.com/SmartThingsCommunity/SmartThingsPublic/blob/master/devicetypes/smartthings/zigbee-rgbw-bulb.src/zigbee-rgbw-bulb.groovy#L214 don't do these checks. I'd say just remove these null checks and let the errors bubble up like @vseven said.

def saturation = (value.saturation == null) ? device.currentValue("saturation") : value.saturation
if(hue == null) hue = 13
if(saturation == null) saturation = 13
def rgb = huesatToRGB(hue, saturation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ def setColor(value) {
def c = value.hex.findAll(/[0-9a-fA-F]{2}/).collect { Integer.parseInt(it, 16) }
result << zwave.switchColorV3.switchColorSet(red:c[0], green:c[1], blue:c[2], warmWhite:0, coldWhite:0)
} else {
def hue = value.hue ?: device.currentValue("hue")
def saturation = value.saturation ?: device.currentValue("saturation")
def hue = (value.hue == null) ? device.currentValue("hue") : value.hue
def saturation = (value.saturation == null) ? device.currentValue("saturation") : value.saturation
if(hue == null) hue = 13
if(saturation == null) saturation = 13
def rgb = huesatToRGB(hue, saturation)
Expand Down
41 changes: 7 additions & 34 deletions devicetypes/smartthings/rgbw-light.src/rgbw-light.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def setColor(value) {
def c = value.hex.findAll(/[0-9a-fA-F]{2}/).collect { Integer.parseInt(it, 16) }
result << zwave.switchColorV3.switchColorSet(red:c[0], green:c[1], blue:c[2], warmWhite:0, coldWhite:0)
} else {
def hue = value.hue ?: device.currentValue("hue")
def saturation = value.saturation ?: device.currentValue("saturation")
def hue = (value.hue == null) ? device.currentValue("hue") : value.hue
def saturation = (value.saturation == null) ? device.currentValue("saturation") : value.saturation
if(hue == null) hue = 13
if(saturation == null) saturation = 13
def rgb = huesatToRGB(hue, saturation)
Expand Down Expand Up @@ -296,39 +296,12 @@ private commands(commands, delay=200) {
}

def rgbToHSV(red, green, blue) {
float r = red / 255f
float g = green / 255f
float b = blue / 255f
float max = [r, g, b].max()
float delta = max - [r, g, b].min()
def hue = 13
def saturation = 0
if (max && delta) {
saturation = 100 * delta / max
if (r == max) {
hue = ((g - b) / delta) * 100 / 6
} else if (g == max) {
hue = (2 + (b - r) / delta) * 100 / 6
} else {
hue = (4 + (r - g) / delta) * 100 / 6
}
}
[hue: hue, saturation: saturation, value: max * 100]
def hex = colorUtil.rgbToHex(red as int, green as int, blue as int)
def hsv = colorUtil.hexToHsv(hex)
return [hue: hsv[0], saturation: hsv[1], value: hsv[2]]
}

def huesatToRGB(hue, sat) {
while(hue >= 100) hue -= 100
int h = (int)(hue / 100 * 6)
float f = hue / 100 * 6 - h
int p = Math.round(255 * (1 - (sat / 100)))
int q = Math.round(255 * (1 - (sat / 100) * f))
int t = Math.round(255 * (1 - (sat / 100) * (1 - f)))
switch (h) {
case 0: return [255, t, p]
case 1: return [q, 255, p]
case 2: return [p, 255, t]
case 3: return [p, q, 255]
case 4: return [t, p, 255]
case 5: return [255, p, q]
}
def color = colorUtil.hsvToHex(Math.round(hue) as int, Math.round(sat) as int)
return colorUtil.hexToRgb(color)
}