def rgb(self, color):
"""Set the color of the device, as represented by either a hex string or a list of 0-255 RGB values"""
try:
red, green, blue = color
except ValueError:
try:
hexcolor = color
reg_match = re.match("^([A-Fa-f0-9]{6})$", hexcolor)
if reg_match:
red = int(hexcolor[:2], 16)
green = int(hexcolor[2:-2], 16)
blue = int(hexcolor[-2:], 16)
else:
print("Error: Color must be in valid hex format.")
return
except ValueError:
print("Error: Color must have one hex value or three 0-255 values.")
return
if not 0 <= red <= 255:
print("Error: Red value out of range! (0-255)")
return
if not 0 <= green <= 255:
print("Error: Green value out of range! (0-255)")
return
if not 0 <= blue <= 255:
print("Error: Blue value out of range! (0-255)")
return
hsv = colorsys.rgb_to_hsv(red / 255, green / 255, blue / 255)
hue = int(hsv[0] * 360)
saturation = int(hsv[1] * 100)
brightness = int(hsv[2] * 100)
data = {"hue": {"value": hue}, "sat": {"value": saturation}, "brightness": {"value": brightness}}
self.__put("state", data)
###########################################
# Layout methods
###########################################
评论列表
文章目录