def create_light_icon(lid, light_data): """Creates a 1x1 PNG icon of light's RGB color and saves it to the local dir. """ # Create a color converter & helper converter = colors.Converter() # Set color based on the type of light # See: http://www.developers.meethue.com/documentation/supported-lights if light_data['state'].get('xy'): rgb_value = converter.xy_to_rgb(light_data['state']['xy'][0], light_data['state']['xy'][1]) elif light_data['state'].get('bri'): rgb_value = colorsys.hsv_to_rgb(0, 0, float(light_data['state']['bri']) / 255) rgb_value = tuple([255 * x for x in rgb_value]) else: rgb_value = (255, 255, 255) if light_data['state']['on'] else (0, 0, 0) f = open('icons/%s.png' % lid, 'wb') w = png.Writer(1, 1) w.write(f, [rgb_value]) f.close()
def get_items(self, query, id, type, resource): control = query.split(':') is_on = (type == self.LIGHT_TYPE and resource['state']['on']) name = resource['name'] if resource else 'All lights' if type == self.LIGHT_TYPE: self.icon = ('%s.png' % id) if is_on else 'off.png' if len(control) is 1: self.partial_query = control[0] if type == self.GROUP_TYPE or is_on: self._add_item( title='Turn %s off' % name, icon='%s.png' % id if type == self.LIGHT_TYPE else 'off.png', arg='%s:%s:off' % (type, id), autocomplete='%s:%s:off' % (type, id), valid=True) if type == self.GROUP_TYPE or not is_on: self._add_item( 'light_on', icon='on.png', title='Turn %s on' % name, arg='%s:%s:on' % (type, id), valid=True) if type == self.GROUP_TYPE or is_on: if type == self.GROUP_TYPE: self._add_item( 'shuffle', arg='%s:%s:shuffle' % (type, id), valid=True) if type == self.GROUP_TYPE: # maybe settings scenes should be disabled for "All lights" # if the bridge is deconz self._add_item( 'set_scene', autocomplete='groups:%s:set:' % id) # Sadly the Hue app will only show scenes that *IT* created, # meaning scenes saved by this workflow do not show up in the # Hue app. So to keep scene CRUD more simple, this has been # disabled. # if type == self.GROUP_TYPE: # self._add_item('save_scene', autocomplete='groups:%s:save:' % id) if type == self.GROUP_TYPE or (type == self.LIGHT_TYPE and resource['state'].get('xy')): self._add_item( 'set_color', subtitle='', autocomplete='%s:%s:color:' % (type, id)) self._add_item( 'set_brightness', subtitle='', autocomplete='%s:%s:bri:' % (type, id)) if type == self.GROUP_TYPE: self._add_item('set_harmony', autocomplete='groups:%s:harmony:' % id) if type == self.GROUP_TYPE or resource['state'].get('effect') is not None: self._add_item( 'set_effect', subtitle='', autocomplete='%s:%s:effect:' % (type, id)) self._add_item('set_reminder', autocomplete='%s:%s:reminder:' % (type, id)) self._add_item('rename', autocomplete='%s:%s:rename:' % (type, id)) elif len(control) >= 2: function = control[0] value = control[1] if function == 'set': self.icon = 'scene.png' self.partial_query = value scenes = utils.get_scenes(id) items = sorted(scenes.items(), key=lambda (k, v): v.get('lastupdated')) for sid, scene in items: self._add_item( title=scene['name'], arg='groups:%s:set:%s' % (id, sid), valid=True) # if function == 'save': # self._add_item( # icon='scene.png', # title='Save current state as %s' % (value or '…'), # valid=True, # arg='groups:%s:save:%s' % (id, value)) if function == 'color': self.icon = 'color.png' converter = colors.Converter() if type == self.GROUP_TYPE: current_hex = 'ffffff' else: current_hex = converter.xy_to_hex( resource['state']['xy'][0], resource['state']['xy'][1], resource['state']['bri']) self._add_item( 'set_color', valid=utils.is_valid_color(value), arg='%s:%s:color:%s' % (type, id, value)) self._add_item( 'random_color', valid=True, arg='%s:%s:color:random' % (type, id)) self._add_item( 'color_picker', valid=True, arg='colorpicker %s:%s:color:<color>' % (type, id)) elif function == 'bri': self._add_item( 'set_brightness', title='Set brightness to %s' % (value + '%' if value else '…'), valid=True if value else False, arg='%s:%s:bri:%s' % (type, id, value)) elif function == 'effect': self.icon = 'effect.png' self.partial_query = value self._add_item( 'effect_none', valid=True, arg='%s:%s:effect:none' % (type, id)) self._add_item( 'color_loop', valid=True, arg='%s:%s:effect:colorloop' % (type, id)) elif function == 'reminder': self.icon = 'reminder.png' def reminder_title(suffix): return 'Blink {name} in {time} {suffix}'.format( name=name, time=(int_value or '…'), suffix=suffix) try: int_value = int(value) except ValueError: int_value = 0 self._add_item( title=reminder_title('minutes'), subtitle='', valid=True if int_value else False, arg='%s:%s:reminder:%s' % (type, id, int_value * 60)) self._add_item( title=reminder_title('hours'), subtitle='', valid=True if int_value else False, arg='%s:%s:reminder:%s' % (type, id, int_value * 60 * 60)) elif function == 'rename': self._add_item( 'rename', title='Rename to %s' % value, valid=True, arg='%s:%s:rename:%s' % (type, id, value)) elif function == 'harmony': mode = value if mode: root_color = control[2] is_valid_color = utils.is_valid_color(root_color) self._add_item( 'set_color', title='Set harmony root color…', icon='%s.png' % mode, valid=is_valid_color, arg='groups:%s:harmony:%s:%s' % (id, mode, root_color)) self._add_item( 'color_picker', icon='%s.png' % mode, valid=True, arg='colorpicker groups:%s:harmony:%s:<color>' % (id, mode)) else: self._add_item( title='Analogous', subtitle='Colors that are adjacent to each other. Recommended!', icon='analogous.png', autocomplete='groups:%s:harmony:analogous:' % id) self._add_item( title='Complementary', subtitle='Colors that are opposite each other.', icon='complementary.png', autocomplete='groups:%s:harmony:complementary:' % id) self._add_item( title='Triad', subtitle='Colors that are evenly spaced by thirds.', icon='triad.png', autocomplete='groups:%s:harmony:triad:' % id) self._add_item( title='Tetrad', subtitle='Colors that are evenly spaced by quarters.', icon='tetrad.png', autocomplete='groups:%s:harmony:tetrad:' % id) self._add_item( title='Split Complementary', subtitle='Colors that are opposite and adjacent.', icon='split_complementary.png', autocomplete='groups:%s:harmony:split_complementary:' % id) self._filter_items() return self.items
def _get_random_xy_color(self, gamut): random_color = colorsys.hsv_to_rgb(random.random(), 1, 1) random_color = tuple([255 * x for x in random_color]) return colors.Converter(gamut).rgb_to_xy(*random_color)
def _get_xy_color(self, color, gamut): """Validate and convert hex color to XY space.""" return colors.Converter(gamut).hex_to_xy(utils.get_color_value(color))