Python colorsys.hsv_to_rgb() Examples

The following are 30 code examples of colorsys.hsv_to_rgb(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module colorsys , or try the search function .
Example #1
Source File: mote-api.py    From mote with MIT License 7 votes vote down vote up
def set_brightness(channel, br):
    global status
    if channel == 'all':
        for ch in status['colour']:
            c = status['colour'][ch]
            r, g, b = c
            h, s, v = rgb_to_hsv(r, g, b)
            v = int(br) / 100.0
            r, g, b = [int(c * 255) for c in hsv_to_rgb(h, s, v)]
            status['colour'][ch] = [r, g, b]
        if not all(status['state'].values()) == 0:
            mote_on(status)
    else:
        c = status['colour'][int(channel)]
        r, g, b = c
        h, s, v = rgb_to_hsv(r, g, b)
        v = int(br) / 100.0
        r, g, b = [int(c * 255) for c in hsv_to_rgb(h, s, v)]
        status['colour'][int(channel)] = [r, g, b]
        if not status['state'][int(channel)] == 0:
            mote_on(status)
    return jsonify(status)

## Returns the current API version to the requester 
Example #2
Source File: __init__.py    From blender-scripting with MIT License 6 votes vote down vote up
def rainbowLights(r=5, n=100, freq=2, energy=0.1):
    for i in range(n):
        t = float(i)/float(n)
        pos = (r*sin(tau*t), r*cos(tau*t), r*sin(freq*tau*t))

        # Create lamp
        bpy.ops.object.add(type='LAMP', location=pos)
        obj = bpy.context.object
        obj.data.type = 'POINT'

        # Apply gamma correction for Blender
        color = tuple(pow(c, 2.2) for c in colorsys.hsv_to_rgb(t, 0.6, 1))

        # Set HSV color and lamp energy
        obj.data.color = color
        obj.data.energy = energy 
Example #3
Source File: test_colorsys.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_hsv_values(self):
        values = [
            # rgb, hsv
            ((0.0, 0.0, 0.0), (  0  , 0.0, 0.0)), # black
            ((0.0, 0.0, 1.0), (4./6., 1.0, 1.0)), # blue
            ((0.0, 1.0, 0.0), (2./6., 1.0, 1.0)), # green
            ((0.0, 1.0, 1.0), (3./6., 1.0, 1.0)), # cyan
            ((1.0, 0.0, 0.0), (  0  , 1.0, 1.0)), # red
            ((1.0, 0.0, 1.0), (5./6., 1.0, 1.0)), # purple
            ((1.0, 1.0, 0.0), (1./6., 1.0, 1.0)), # yellow
            ((1.0, 1.0, 1.0), (  0  , 0.0, 1.0)), # white
            ((0.5, 0.5, 0.5), (  0  , 0.0, 0.5)), # grey
        ]
        for (rgb, hsv) in values:
            self.assertTripleEqual(hsv, colorsys.rgb_to_hsv(*rgb))
            self.assertTripleEqual(rgb, colorsys.hsv_to_rgb(*hsv)) 
Example #4
Source File: yolo.py    From deep_sort_yolov3 with GNU General Public License v3.0 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example #5
Source File: test_colorsys.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_hsv_values(self):
        values = [
            # rgb, hsv
            ((0.0, 0.0, 0.0), (  0  , 0.0, 0.0)), # black
            ((0.0, 0.0, 1.0), (4./6., 1.0, 1.0)), # blue
            ((0.0, 1.0, 0.0), (2./6., 1.0, 1.0)), # green
            ((0.0, 1.0, 1.0), (3./6., 1.0, 1.0)), # cyan
            ((1.0, 0.0, 0.0), (  0  , 1.0, 1.0)), # red
            ((1.0, 0.0, 1.0), (5./6., 1.0, 1.0)), # purple
            ((1.0, 1.0, 0.0), (1./6., 1.0, 1.0)), # yellow
            ((1.0, 1.0, 1.0), (  0  , 0.0, 1.0)), # white
            ((0.5, 0.5, 0.5), (  0  , 0.0, 0.5)), # grey
        ]
        for (rgb, hsv) in values:
            self.assertTripleEqual(hsv, colorsys.rgb_to_hsv(*rgb))
            self.assertTripleEqual(rgb, colorsys.hsv_to_rgb(*hsv)) 
Example #6
Source File: yolo.py    From multi-object-tracking with GNU General Public License v3.0 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example #7
Source File: yolo.py    From YOLO-3D-Box with MIT License 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example #8
Source File: yolo.py    From Vehicle-Detection-and-Tracking-Usig-YOLO-and-Deep-Sort-with-Keras-and-Tensorflow with MIT License 6 votes vote down vote up
def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'

        self.yolo_model = load_model(model_path, compile=False)
        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        random.seed(10101)  # Fixed seed for consistent colors across runs.
        random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.
        random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        return boxes, scores, classes 
Example #9
Source File: test_colorsys.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_hsv_values(self):
        values = [
            # rgb, hsv
            ((0.0, 0.0, 0.0), (  0  , 0.0, 0.0)), # black
            ((0.0, 0.0, 1.0), (4./6., 1.0, 1.0)), # blue
            ((0.0, 1.0, 0.0), (2./6., 1.0, 1.0)), # green
            ((0.0, 1.0, 1.0), (3./6., 1.0, 1.0)), # cyan
            ((1.0, 0.0, 0.0), (  0  , 1.0, 1.0)), # red
            ((1.0, 0.0, 1.0), (5./6., 1.0, 1.0)), # purple
            ((1.0, 1.0, 0.0), (1./6., 1.0, 1.0)), # yellow
            ((1.0, 1.0, 1.0), (  0  , 0.0, 1.0)), # white
            ((0.5, 0.5, 0.5), (  0  , 0.0, 0.5)), # grey
        ]
        for (rgb, hsv) in values:
            self.assertTripleEqual(hsv, colorsys.rgb_to_hsv(*rgb))
            self.assertTripleEqual(rgb, colorsys.hsv_to_rgb(*hsv)) 
Example #10
Source File: FamilyTree.py    From addons-source with GNU General Public License v2.0 6 votes vote down vote up
def __set_fill_color(self, style_name, number, count):

        if self.shuffle_colors:
            number = int(number * (count + 1) / int(pow(count, 0.5))) % count
        (r, g, b) = colorsys.hsv_to_rgb((number + 1) / count, .20, 1.0)
        (r, g, b) = int(255 * r), int(255 * g), int(255 * b)
        style_sheet = self.doc.get_style_sheet()
        draw_style = style_sheet.get_draw_style(style_name)
        draw_style.set_fill_color((r, g, b))
        style_sheet.add_draw_style(style_name, draw_style)
        self.doc.set_style_sheet(style_sheet)


#------------------------------------------------------------------------
#
# FamilyTreeOptions
#
#------------------------------------------------------------------------ 
Example #11
Source File: visualization_utils.py    From MOTSFusion with MIT License 6 votes vote down vote up
def generate_colors():
    """
    Generate random colors.
    To get visually distinct colors, generate them in HSV space then
    convert to RGB.
    """
    import colorsys
    N = 30
    brightness = 0.7
    hsv = [(i / N, 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    perm = [15, 13, 25, 12, 19, 8, 22, 24, 29, 17, 28, 20, 2, 27, 11, 26, 21, 4, 3, 18, 9, 5, 14, 1, 16, 0, 23, 7,
            6, 10]
    colors = [colors[idx] for idx in perm]
    del colors[::2]
    return colors 
Example #12
Source File: visualize_mots.py    From MOTSFusion with MIT License 6 votes vote down vote up
def generate_colors():
    """
    Generate random colors.
    To get visually distinct colors, generate them in HSV space then
    convert to RGB.
    """
    N = 30
    brightness = 0.7
    hsv = [(i / N, 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    perm = [15, 13, 25, 12, 19, 8, 22, 24, 29, 17, 28, 20, 2, 27, 11, 26, 21, 4, 3, 18, 9, 5, 14, 1, 16, 0, 23, 7, 6,
            10]
    colors = [colors[idx] for idx in perm]
    return colors


# from https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/visualize.py 
Example #13
Source File: merge_KITTI_masks.py    From MOTSFusion with MIT License 6 votes vote down vote up
def generate_colors():
  """
  Generate random colors.
  To get visually distinct colors, generate them in HSV space then
  convert to RGB.
  """
  N = 30
  brightness = 0.7
  hsv = [(i / N, 1, brightness) for i in range(N)]
  colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
  perm = [15, 13, 25, 12, 19, 8, 22, 24, 29, 17, 28, 20, 2, 27, 11, 26, 21, 4, 3, 18, 9, 5, 14, 1, 16, 0, 23, 7, 6, 10]
  colors = [colors[idx] for idx in perm]
  return colors


# from https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/visualize.py 
Example #14
Source File: visualization.py    From deep_sort with GNU General Public License v3.0 6 votes vote down vote up
def create_unique_color_float(tag, hue_step=0.41):
    """Create a unique RGB color code for a given track id (tag).

    The color code is generated in HSV color space by moving along the
    hue angle and gradually changing the saturation.

    Parameters
    ----------
    tag : int
        The unique target identifying tag.
    hue_step : float
        Difference between two neighboring color codes in HSV space (more
        specifically, the distance in hue channel).

    Returns
    -------
    (float, float, float)
        RGB color code in range [0, 1]

    """
    h, v = (tag * hue_step) % 1, 1. - (int(tag * hue_step) % 4) / 5.
    r, g, b = colorsys.hsv_to_rgb(h, 1., v)
    return r, g, b 
Example #15
Source File: utils.py    From pixelworld with MIT License 5 votes vote down vote up
def generate_color_palette(n):
    """generate a color palette
    
    Parameters
    ----------
    n : int
        the number of colors needed
    
    Returns
    -------
    pal : ndarray
        a num_colors x 3 array of RGB colors
    """
    #number of colors needed on top of the base palette defined above
    n_needed = n - len(base_lut)
    
    if n_needed <= 0:  # already have what we need
        return base_lut[0:n,:]
    
    #sort the hues of the base palette
    sorted_hues = sorted(base_lut_hsv[:,0])
    
    #find maximally distinct hues until we have enough colors
    hues = []
    while n_needed > 0:
        #difference between adjacent hues
        df = [sorted_hues[i+1] - sorted_hues[i] for i in xrange(len(sorted_hues)-1)]
        
        #find the consecutive hues that have the maximum distance between them
        mx = max(df)
        idx = df.index(mx)
        
        #new hue is the mid-point between those hues
        new_hue = (sorted_hues[idx] + sorted_hues[idx+1])/2
        sorted_hues.insert(idx+1, new_hue)
        hues.append(new_hue)
        
        n_needed -= 1
    
    #append the generated colors to the base palette
    return np.vstack((base_lut, np.array([colorsys.hsv_to_rgb(hue, 1, 1) for hue in hues]))) 
Example #16
Source File: test_colorsys.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_hsv_roundtrip(self):
        for r in frange(0.0, 1.0, 0.2):
            for g in frange(0.0, 1.0, 0.2):
                for b in frange(0.0, 1.0, 0.2):
                    rgb = (r, g, b)
                    self.assertTripleEqual(
                        rgb,
                        colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb))
                    ) 
Example #17
Source File: functions.py    From ttkwidgets with GNU General Public License v3.0 5 votes vote down vote up
def hue2col(h):
    """Return the color in RGB format corresponding to (h, 100, 100) in HSV."""
    if h < 0 or h > 360:
        raise ValueError("Hue should be between 0 and 360")
    else:
        return hsv_to_rgb(h, 100, 100)


# --- Fake transparent image creation with PIL 
Example #18
Source File: functions.py    From ttkwidgets with GNU General Public License v3.0 5 votes vote down vote up
def hsv_to_rgb(h, s, v):
    """Convert HSV color to RGB."""
    r, g, b = colorsys.hsv_to_rgb(h / 360., s / 100., v / 100.)
    return round2(r * 255), round2(g * 255), round2(b * 255) 
Example #19
Source File: RenderManager.py    From EM-uNetPi with MIT License 5 votes vote down vote up
def ConvRgb(self, color, saturation, brightness):
        rgb = colorsys.hsv_to_rgb(
            color, saturation,
            brightness)  # rgb (color, light, light),,,poi? 0 < range < 1
        r = int(rgb[0] * ((1 << self.fb.red.length) - 1))
        g = int(rgb[1] * ((1 << self.fb.green.length) - 1))
        b = int(rgb[2] * ((1 << self.fb.blue.length) - 1))
        return self.fb.rgb(r, g, b) 
Example #20
Source File: visualize.py    From paper.io.sessdsa with GNU General Public License v3.0 5 votes vote down vote up
def gen_color_text(h, s, v):
        '''
        hsv to rgb text
        params:
            h, s, v
        
        return:
            str : #xxxxxx
        '''
        raw = hsv_to_rgb(h, s, v)
        return '#%02x%02x%02x' % tuple(map(lambda x: int(x * 255), raw))


# 调用函数 
Example #21
Source File: utils.py    From PSPNet-Keras-tensorflow with MIT License 5 votes vote down vote up
def to_color(category):
    """Map each category color a good distance away from each other on the HSV color space."""
    v = (category - 1) * (137.5 / 360)
    return colorsys.hsv_to_rgb(v, 1, 1) 
Example #22
Source File: imagetools.py    From MangoByte with MIT License 5 votes vote down vote up
def hsv_to_rgb(hsv):
	hsv = tuple(map(lambda v: v / 255.0, hsv))
	rgb = colorsys.hsv_to_rgb(*hsv)
	return tuple(map(lambda v: int(v * 255), rgb)) 
Example #23
Source File: imagetools.py    From MangoByte with MIT License 5 votes vote down vote up
def colorize_single(converter, pixel_color):
	old = min(converter, key=lambda c: color_diff(c, pixel_color))
	new = converter[old]

	return Color(hsv_to_rgb((
		new.h,
		pixel_color.s,
		pixel_color.v
	)))


# takes in 2 image filenames and spits out a third colorized one
# the colors from the first one are used to fill in the shape of the second one 
Example #24
Source File: wm_legend.py    From wafer_map with GNU General Public License v3.0 5 votes vote down vote up
def create_colors(n):
        """
        Create the colors based on how many legend items there are (n).

        The idea is to start off with one color, assign it to the 1st legend
        value, then find that color's complement and assign it to the 2nd
        legend value. Then, move around the color wheel by some degree,
        probably like so:

          <insert math>

        We are limited to only using 1/2 of the circle
        because we use the other half for the complements.

        1.  Split the circle into n parts.
        2.  reorganize into alternations
            1 2 3 4 5 6 7 8  -->
            1 3 5 7 2 4 6 8
        """
        spacing = 360 / n
        colors = []
        for val in wm_utils.frange(0, 360, spacing):
            hsl = (val/360, 1, 0.75)
            colors.append(colorsys.hsv_to_rgb(*hsl))

        # convert from 0-1 to 0-255 and return
        colors = [tuple(int(i*255) for i in color) for color in colors]

        # Alternate colors across the circle
        colors = colors[::2] + colors[1::2]
        return colors 
Example #25
Source File: glory_of_mankind.py    From paper.io.sessdsa with GNU General Public License v3.0 5 votes vote down vote up
def gen_color_text(h, s, v):
        '''
        hsv to rgb text
        params:
            h, s, v
        
        return:
            str : #xxxxxx
        '''
        raw = hsv_to_rgb(h, s, v)
        return '#%02x%02x%02x' % tuple(map(lambda x: int(x * 255), raw))


# 自定义类 
Example #26
Source File: solo.py    From paper.io.sessdsa with GNU General Public License v3.0 5 votes vote down vote up
def gen_color_text(h, s, v):
        '''
        hsv to rgb text
        params:
            h, s, v
        
        return:
            str : #xxxxxx
        '''
        raw = hsv_to_rgb(h, s, v)
        return '#%02x%02x%02x' % tuple(map(lambda x: int(x * 255), raw))


# 调用函数 
Example #27
Source File: 6.glory_of_mankind.py    From paper.io.sessdsa with GNU General Public License v3.0 5 votes vote down vote up
def gen_color_text(h, s, v):
        '''
        hsv to rgb text
        params:
            h, s, v
        
        return:
            str : #xxxxxx
        '''
        raw = hsv_to_rgb(h, s, v)
        return '#%02x%02x%02x' % tuple(map(lambda x: int(x * 255), raw))


# 自定义类 
Example #28
Source File: 5.visualize.py    From paper.io.sessdsa with GNU General Public License v3.0 5 votes vote down vote up
def gen_color_text(h, s, v):
        '''
        hsv to rgb text
        params:
            h, s, v
        
        return:
            str : #xxxxxx
        '''
        raw = hsv_to_rgb(h, s, v)
        return '#%02x%02x%02x' % tuple(map(lambda x: int(x * 255), raw))


# 调用函数 
Example #29
Source File: nviz.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def random_color(self, alpha=0.2):
        """ Return random color """
        from colorsys import hsv_to_rgb
        HSV = [[random.random(), 0.5, 0.5]]
        RGBA = [x + (alpha,) for x in [hsv_to_rgb(*x) for x in HSV]]
        RGBA = [[int(y * 255) for y in x] for x in RGBA]
        HEX = ["#{:02x}{:02x}{:02x}{:02x}".format(
            r, g, b, a) for r, g, b, a in RGBA]
        return HEX[0] 
Example #30
Source File: color.py    From pyx with GNU General Public License v2.0 5 votes vote down vote up
def rgb(self):
        r, g, b = colorsys.hsv_to_rgb(self.h, self.s, self.b)
        return rgb(r, g, b)