Example #1
0
    def rgb(self, red, green=None, blue=None, transitiontime=5):
        if isinstance(red, basestring):
            # assume a hex string is passed
            rstring = red
            red = int(rstring[1:3], 16)
            green = int(rstring[3:5], 16)
            blue = int(rstring[5:], 16)

        print red, green, blue

        # We need to convert the RGB value to Yxy.
        redScale = float(red) / 255.0
        greenScale = float(green) / 255.0
        blueScale = float(blue) / 255.0
        colormodels.init(
            phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
            phosphor_green=colormodels.xyz_color(0.4091, 0.518),
            phosphor_blue=colormodels.xyz_color(0.167, 0.04))
        # logger.debug(redScale, greenScale, blueScale)
        xyz = colormodels.irgb_color(red, green, blue)
        # logger.debug(xyz)
        xyz = colormodels.xyz_from_rgb(xyz)
        # logger.debug(xyz)
        xyz = colormodels.xyz_normalize(xyz)
        # logger.debug(xyz)

        return self.set_state(
            {"xy": [xyz[0], xyz[1]], "transitiontime": transitiontime})
Example #2
0
    def rgb(self, red, green=None, blue=None, transitiontime=5):
        if isinstance(red, basestring):
            # assume a hex string is passed
            rstring = red
            red = int(rstring[1:3], 16)
            green = int(rstring[3:5], 16)
            blue = int(rstring[5:], 16)

        print red, green, blue

        # We need to convert the RGB value to Yxy.
        redScale = float(red) / 255.0
        greenScale = float(green) / 255.0
        blueScale = float(blue) / 255.0
        colormodels.init(phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
                         phosphor_green=colormodels.xyz_color(0.4091, 0.518),
                         phosphor_blue=colormodels.xyz_color(0.167, 0.04))
        logger.debug(redScale, greenScale, blueScale)
        xyz = colormodels.irgb_color(red, green, blue)
        logger.debug(xyz)
        xyz = colormodels.xyz_from_rgb(xyz)
        logger.debug(xyz)
        xyz = colormodels.xyz_normalize(xyz)
        logger.debug(xyz)

        return self.set_state({
            "xy": [xyz[0], xyz[1]],
            "transitiontime": transitiontime
        })
Example #3
0
    def rgb(self, red, green=None, blue=None, transitiontime=5):
        if isinstance(red, basestring):
            if not red.startswith('#'):
                raise ArgumentError('rgb strings must start with "#"')
            # assume a hex string is passed
            rstring = red
            red = int(rstring[1:3], 16)
            green = int(rstring[3:5], 16)
            blue = int(rstring[5:], 16)

        logger.debug("%d %d %d"%(red, green, blue))

        # We need to convert the RGB value to Yxy.
        redScale = float(red) / 255.0
        greenScale = float(green) / 255.0
        blueScale = float(blue) / 255.0
        logger.debug("%d %d %d"%(redScale, greenScale, blueScale))
        xyz = colormodels.irgb_color(red, green, blue)
        logger.debug(xyz)
        xyz = colormodels.xyz_from_rgb(xyz)
        logger.debug(xyz)
        xyz = colormodels.xyz_normalize(xyz)
        logger.debug(xyz)

        return self.set_state(
            {"xy": [xyz[0], xyz[1]], "transitiontime": transitiontime})
Example #4
0
    def color_adj(self, light_id):
        if light_id == 1:    
            rgba = self.color_picker1.wheel.color
        elif light_id == 2:
            rgba = self.color_picker2.wheel.color
        elif light_id == 3:
            rgba = self.color_picker2.wheel.color
        red = rgba[0]
        green = rgba[1]
        blue = rgba[2]

        colormodels.init(
            phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
            phosphor_green=colormodels.xyz_color(0.4091, 0.518),
            phosphor_blue=colormodels.xyz_color(0.167, 0.04))
        xyz = colormodels.irgb_color(red, green, blue)
        xyz = colormodels.xyz_from_rgb(xyz)
        xyz = colormodels.xyz_normalize(xyz)
        #print xyz, '\n'
        xy = [xyz[0], xyz[1]]
        huehub = 'http://' + ip + '/api/'+ myhash + "/lights/" + str(light_id)
        reply = requests.get(huehub)
        a=json.loads(reply.text)
        #print bri_val
        payload = json.dumps({"xy":xy})
        sethuehub = huehub + "/state"
        reply = requests.put(sethuehub, data=payload)

        #Update hue data printout
        reply= requests.get(huehub)
        a=json.loads(reply.text)

        if light_id == 1:    
            self.hue_label1.text = 'Hue :'+str(a['state']['hue'])
            self.bri_label1.text = 'Brightness :'+str(a['state']['bri'])
            self.sat_label1.text = 'Saturation :'+str(a['state']['sat'])
            self.ct_label1.text = 'Colour Temp :'+str(a['state']['ct'])
            self.xy_label1.text = '(x, y) :'+str(a['state']['xy'])
        elif light_id == 2:
            self.hue_label2.text = 'Hue :'+str(a['state']['hue'])
            self.bri_label2.text = 'Brightness :'+str(a['state']['bri'])
            self.sat_label2.text = 'Saturation :'+str(a['state']['sat'])
            self.ct_label2.text = 'Colour Temp :'+str(a['state']['ct'])
            self.xy_label2.text = '(x, y) :'+str(a['state']['xy'])
        elif light_id == 3:
            self.hue_label3.text = 'Hue :'+str(a['state']['hue'])
            self.bri_label3.text = 'Brightness :'+str(a['state']['bri'])
            self.sat_label3.text = 'Saturation :'+str(a['state']['sat'])
            self.ct_label3.text = 'Colour Temp :'+str(a['state']['ct'])
            self.xy_label3.text = '(x, y) :'+str(a['state']['xy'])
Example #5
0
    def _rgb_transfer(self, red, green=None, blue=None):
        if isinstance(red, basestring):
            rstring = red
            red = int(rstring[1:3], 16)
            green = int(rstring[3:5], 16)
            blue = int(rstring[5:], 16)

        colormodels.init(
            phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
            phosphor_green=colormodels.xyz_color(0.4091, 0.518),
            phosphor_blue=colormodels.xyz_color(0.167, 0.04))
        xyz = colormodels.irgb_color(red, green, blue)
        xyz = colormodels.xyz_from_rgb(xyz)
        xyz = colormodels.xyz_normalize(xyz)
        d = dict(xy=[xyz[0], xyz[1]],
                transitiontime=self._conf.default_transitiontime)
        return self.set_state(d)
    def colour_select(self, light_id):

        try:
            (rgb, hx) = tkColorChooser.askcolor()
            if (rgb, hx) == (None, None):
                warnings.warn('ColorChooser Error: No RGB colour selected.')
    
            else:
                print '(rgb, hx):', rgb, hx
                red = rgb[0]
                green = rgb[1]
                blue = rgb[2]

                redScale = float(red) / 255.0 #rgb goes from 0-255, this changes it into the 0-1.0 that xy uses
                greenScale = float(green) / 255.0
                blueScale = float(blue) / 255.0
                # Initialization function for conversion between CIE XYZ and linear RGB spaces
                colormodels.init(
                    phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
                    phosphor_green=colormodels.xyz_color(0.4091, 0.518),
                    phosphor_blue=colormodels.xyz_color(0.167, 0.04))
                xyz = colormodels.irgb_color(red, green, blue)
                xyz = colormodels.xyz_from_rgb(xyz)
                xyz = colormodels.xyz_normalize(xyz)
                print xyz, '\n'
                xy = [xyz[0], xyz[1]]
                

                #print 'rgb: ', red, green, blue
                #global huehub
                huehub = "http://192.168.0.100/api/"+ myhash + "/lights/" + str(light_id)
                reply = requests.get(huehub)
                a=json.loads(reply.text)
                #print bri_val
                payload = json.dumps({"xy":xy})
                sethuehub = huehub + "/state"
                reply = requests.put(sethuehub, data=payload)
        except TypeError, e:
            print 'Error closing ColorChooser: variable referenced before assignment\n', e, '\n'
            pass
Example #7
0
 def test_A(xyz0, tolerance=1.0e-10, verbose=1):
     rgb0 = colormodels.rgb_from_xyz(xyz0)
     xyz1 = colormodels.xyz_from_rgb(rgb0)
     rgb1 = colormodels.rgb_from_xyz(xyz1)
     # check errors
     err_rgb = rgb1 - rgb0
     error_rgb = math.sqrt(numpy.dot(err_rgb, err_rgb))
     err_xyz = xyz1 - xyz0
     error_xyz = math.sqrt(numpy.dot(err_xyz, err_xyz))
     passed = (error_rgb <= tolerance) and (error_xyz <= tolerance)
     if passed:
         status = 'pass'
     else:
         status = 'FAILED'
     msg = 'test_xyz_rgb.test_A() : xyz0 = %s, rgb(xyz0) = %s, xyz(rgb(xyz0)) = %s, rgb(xyz(rgb(xyz0))) = %s, errors = (%g, %g), %s' % (
         str(xyz0), str(rgb0), str(xyz1), str(rgb1), error_rgb, error_xyz,
         status)
     if verbose >= 1:
         print(msg)
     if not passed:
         pass
         raise ValueError(msg)
     return passed
    def rgb(self, red, green, blue, transitiontime=5):
        red = int(red)
        blue = int(blue)
        green = int(green)
        transitiontime = int(transitiontime)

        # We need to convert the RGB value to Yxy.
        redScale = float(red) / 255.0
        greenScale = float(green) / 255.0
        blueScale = float(blue) / 255.0
        colormodels.init(
            phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
            phosphor_green=colormodels.xyz_color(0.4091, 0.518),
            phosphor_blue=colormodels.xyz_color(0.167, 0.04))
        logger.debug(redScale, greenScale, blueScale)
        xyz = colormodels.irgb_color(red, green, blue)
        logger.debug(xyz)
        xyz = colormodels.xyz_from_rgb(xyz)
        logger.debug(xyz)
        xyz = colormodels.xyz_normalize(xyz)
        logger.debug(xyz)

        return self.set_state(
            **{"xy": [xyz[0], xyz[1]], "transitiontime": transitiontime})
Example #9
0
def generate_map2d(hue_quantity, lightness_quantity, hue_cmap,
                   hue_range=None, lightness_range=None,
                   scale_min=0, scale_max=100):
    '''
    Given an existing color map, and two ranges of values, map the first range
    to hue, and the second to lightness by scaling the first component of the
    CIELab expression for the colors representing each hue.

    Parameters
    ----------

    hue_quantity       : array of floats, values to map to hue
    lightness_quantity : array of floats, values to map to lightness
    hue_cmap           : colormap object,
                         gradient of colors defining the range of hues
    hue_range          : list,
                         minimum and maximum of normalization for hue mapping
    lightness_range    : list,
                         minimum and maximum of normalization for hue mapping
    scale_min          : float, minimum perceived lightness (0-100)
    scale_max          : float, maximum perceived lightness (0-100)

    Returns
    -------

    rgb_maps : array of RGB colors, same shape as quantity
    '''

    if hue_range is None:
        hue_range = [np.nanmin(hue_quantity),
                     np.nanmax(hue_quantity)]
    if lightness_range is None:
        lightness_range = [np.nanmin(lightness_quantity),
                           np.nanmax(lightness_quantity)]

    if (scale_min < 0 or scale_min > 100):
        raise Exception('The scale minimum is invalid!')
    if (scale_max < 0 or scale_max > 100):
        raise Exception('The scale maximum is invalid!')
    if (scale_min > scale_max):
        raise Exception('The scale minimum should be less than ' +
                        'or equal to the scale maximum.')

    hue_scale = colors.Normalize(*hue_range)
    hue_map = cm.ScalarMappable(hue_scale, cmap=hue_cmap).get_cmap()
    hues = hue_map(hue_quantity)

    colors_CIElab = np.zeros(np.r_[hues.shape[:-1], 3])
    for index in np.ndindex(hues.shape[:-1]):
        colors_CIElab[index] = colormodels.lab_from_xyz(
            colormodels.xyz_from_rgb(hues[index][:-1]))

    lightness = lightness_quantity / \
        (np.ptp(lightness_range) / (scale_max - scale_min)) - \
        (np.nanmin(lightness_range) - scale_min)
    lightness[lightness < scale_min] = scale_min
    lightness[lightness > scale_max] = scale_max

    colors_CIElab[..., 0] = lightness

    rgb_maps = np.ones(np.r_[hue_quantity.shape, 4])
    for index in np.ndindex(colors_CIElab.shape[:-1]):
        rgb_maps[index][:3] = [
            colormodels.srgb_gamma_invert(c)
            for c in colormodels.rgb_from_xyz(
                colormodels.xyz_from_lab(colors_CIElab[index]))
            ]
    rgb_maps[rgb_maps < 0] = 0
    rgb_maps[rgb_maps > 1] = 1

    return rgb_maps