Python colorsys.rgb_to_hsv() Examples

The following are 30 code examples of colorsys.rgb_to_hsv(). 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: board.py    From eduActiv8 with GNU General Public License v3.0 6 votes vote down vote up
def set_outline(self, color=(255, 0, 0), width=2):
        'enables the draw_outline and sets line color and width'
        self.perm_outline = True
        if color == 0 and hasattr(self, "door_outline") is False:  # if color is 0 calculate colour from base colour
            # convert to hsv
            c = self.color
            h, s, v = ex.rgb_to_hsv(c[0], c[1], c[2])
            outline_color = ex.hsv_to_rgb(h, s + 50, v - 50)
            self.perm_outline_color = outline_color
        elif color == 1:
            c = self.color
            h, s, v = ex.rgb_to_hsv(c[0], c[1], c[2])
            outline_color = ex.hsv_to_rgb(h, s + 20, v - 20)
            self.perm_outline_color = outline_color
        elif hasattr(self, "door_outline") is False:
            self.perm_outline_color = color

        self.perm_outline_width = width
        self.init_pow = width 
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: 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 #5
Source File: update_colors.py    From wasabi2d with GNU Lesser General Public License v3.0 6 votes vote down vote up
def hue_name(color):
    arr = np.array(color)
    h, s, v = colorsys.rgb_to_hsv(*arr[:3] / 255)
    if s == 0:
        return 'Greys'

    if h < 0.02:
        return 'Reds'
    elif h < 0.11:
        return "Oranges"
    elif h < 0.2:
        return "Yellows"
    elif h < 0.45:
        return "Greens"
    elif h < 0.52:
        return "Turquoises"
    elif h < 0.7:
        return "Blues"
    else:
        return "Purples" 
Example #6
Source File: image_ops_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testBatch(self):
    # Build an arbitrary RGB image
    np.random.seed(7)
    batch_size = 5
    shape = (batch_size, 2, 7, 3)

    for nptype in [np.float32, np.float64]:
      inp = np.random.rand(*shape).astype(nptype)

      # Convert to HSV and back, as a batch and individually
      with self.test_session(use_gpu=True) as sess:
        batch0 = constant_op.constant(inp)
        batch1 = image_ops.rgb_to_hsv(batch0)
        batch2 = image_ops.hsv_to_rgb(batch1)
        split0 = array_ops.unpack(batch0)
        split1 = list(map(image_ops.rgb_to_hsv, split0))
        split2 = list(map(image_ops.hsv_to_rgb, split1))
        join1 = array_ops.pack(split1)
        join2 = array_ops.pack(split2)
        batch1, batch2, join1, join2 = sess.run([batch1, batch2, join1, join2])

      # Verify that processing batch elements together is the same as separate
      self.assertAllClose(batch1, join1)
      self.assertAllClose(batch2, join2)
      self.assertAllClose(batch2, inp) 
Example #7
Source File: image_ops_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def _adjustHueNp(self, x_np, delta_h):
    self.assertEqual(x_np.shape[-1], 3)
    x_v = x_np.reshape([-1, 3])
    y_v = np.ndarray(x_v.shape, dtype=x_v.dtype)
    channel_count = x_v.shape[0]
    for i in xrange(channel_count):
      r = x_v[i][0]
      g = x_v[i][1]
      b = x_v[i][2]
      h, s, v = colorsys.rgb_to_hsv(r, g, b)
      h += delta_h
      h = math.fmod(h + 10.0, 1.0)
      r, g, b = colorsys.hsv_to_rgb(h, s, v)
      y_v[i][0] = r
      y_v[i][1] = g
      y_v[i][2] = b
    return y_v.reshape(x_np.shape) 
Example #8
Source File: test_colorsys.py    From Fluid-Designer with GNU General Public License v3.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 #9
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 #10
Source File: randomizer_window.py    From wwrando with MIT License 6 votes vote down vote up
def get_random_h_and_v_shifts_for_custom_color(self, default_color):
    r, g, b = default_color
    h, s, v = colorsys.rgb_to_hsv(r/255, g/255, b/255)
    h = int(h*360)
    s = int(s*100)
    v = int(v*100)
    
    min_v_shift = -40
    max_v_shift = 40
    if s < 10:
      # For very unsaturated colors, we want to limit the range of value randomization to exclude results that wouldn't change anything anyway.
      # This effectively stops white and black from having a 50% chance to not change at all.
      min_v_shift = max(-40, 0-v)
      max_v_shift = min(40, 100-v)
    
    h_shift = random.randint(0, 359)
    v_shift = random.randint(min_v_shift, max_v_shift)
    
    return (h_shift, v_shift) 
Example #11
Source File: tileGAN_client.py    From tileGAN with GNU General Public License v3.0 6 votes vote down vote up
def createLatentPicker(self, widgetLayout):
		def clearLayout(layout):
			while layout.count():
				child = layout.takeAt(0)
				if child.widget():
					child.widget().deleteLater()

		clearLayout(widgetLayout)

		latent_images = np.asarray(tf_manager.getLatentImages()._getvalue())

		latent_colors = np.asarray(tf_manager.getDominantClusterColors()._getvalue())
		latent_hues = [ colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[0] for r, g, b in latent_colors ]
		sorting = np.argsort(latent_hues)

		# create latent picker label widgets
		self.latentLabels = []
		for i in range(len(latent_images)):
			cluster = sorting[i]
			label = LatentLabel(self, cluster, latent_images[cluster], tuple(latent_colors[cluster]))
			label.latentDragged.connect(self.viewer.dragStarted)
			label.activated.connect(self.activateLabel)
			self.latentLabels.append(label)
			self.viewer.mouseDrop.connect(label.dropFinished)
			widgetLayout.addWidget(label) 
Example #12
Source File: test_colorsys.py    From gcblue with BSD 3-Clause "New" or "Revised" 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 #13
Source File: mote-api.py    From mote with MIT License 6 votes vote down vote up
def get_state(channel):
    global status
    for chan in range(1, 5):
        for pixel in range(16):
            if mote.get_pixel(chan, pixel) != (0, 0, 0):
                status['state'][chan] = 1
            else:
                status['state'][chan] = 0
        r, g, b = mote.get_pixel(chan, 0)[0:3]
        h, s, v = rgb_to_hsv(r, g, b)[2]
        status['colour'][chan] = [r, g, b]
        status['brightness'][chan] = v
    if channel == 'all':
        return jsonify(status)
    else:
        channel_status = {}
        for k in status:
            channel_status[k] = {int(channel): status[k][int(channel)]}
        return jsonify(channel_status)

## Sets all channels, or a given channel, "on" or "off" 
Example #14
Source File: test_colorsys.py    From ironpython3 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 #15
Source File: color-renderer.py    From hawthorne with GNU Lesser General Public License v3.0 6 votes vote down vote up
def step(target, repetitions=1):
  _, rgba = target.split(' = ')

  rgba = re.findall(r'rgba\((\d+), (\d+), (\d+), ([\d\.]+)(?:.*)?\)', rgba)[0]
  rgba = [float(x) for x in rgba]
  r, g, b, a = rgba

  lum = math.sqrt(.241 * r + .691 * g + .068 * b)

  h, s, v = colorsys.rgb_to_hsv(r, g, b)

  h2 = int(h * repetitions)
  # lum2 = int(lum * repetitions)
  v2 = int(v * repetitions)

  if h2 % 2 == 1:
    v2 = repetitions - v2
    lum = repetitions - lum

  return (h2, lum, v2) 
Example #16
Source File: rgba.py    From ColorHelper with MIT License 5 votes vote down vote up
def tohsv(self):
        """Convert to `HSV` color format."""

        return rgb_to_hsv(self.r * RGB_CHANNEL_SCALE, self.g * RGB_CHANNEL_SCALE, self.b * RGB_CHANNEL_SCALE) 
Example #17
Source File: privacy.py    From deda with GNU General Public License v3.0 5 votes vote down vote up
def _selectColour(self,colour):
        edgeHue = int(rgb_to_hsv(*colour)[0]*180)
        TOLERANCE = 30
        edgeHue1 = (edgeHue-TOLERANCE)%181
        edgeHue2 = (edgeHue+TOLERANCE)%181
        hsv = cv2.cvtColor(self.im, cv2.COLOR_BGR2HSV)
        if edgeHue2 < edgeHue1: 
            raise Exception("Implementation change necessary: "
                +"Trying to select range that includes 180..0.")
        im = cv2.inRange(hsv, (edgeHue1,100,100),(edgeHue2,255,255))
        return im 
Example #18
Source File: rgba.py    From ColorHelper with MIT License 5 votes vote down vote up
def blend(self, color, percent, alpha=False, color_space=CS_RGB):
        """Blend color."""

        r, g, b, a = self._split_channels(color)
        factor = clamp(clamp(float(percent), 0.0, 100.0) * SCALE_PERCENT, 0.0, 1.0)

        if color_space == CS_RGB:
            self.r = rgb_blend_channel(self.r, r, factor)
            self.g = rgb_blend_channel(self.g, g, factor)
            self.b = rgb_blend_channel(self.b, b, factor)
        elif color_space == CS_HSL:
            orig_h, orig_l, orig_s = self.tohls()
            blend_h, blend_l, blend_s = rgb_to_hls(r * RGB_CHANNEL_SCALE, g * RGB_CHANNEL_SCALE, b * RGB_CHANNEL_SCALE)
            h = hue_blend_channel(orig_h, blend_h, factor)
            l = percent_blend_channel(orig_l, blend_l, factor)
            s = percent_blend_channel(orig_s, blend_s, factor)
            self.fromhls(h, l, s)
        elif color_space == CS_HWB:
            orig_h, orig_w, orig_b = self.tohwb()
            blend_h, s, v = rgb_to_hsv(r * RGB_CHANNEL_SCALE, g * RGB_CHANNEL_SCALE, b * RGB_CHANNEL_SCALE)
            blend_w = (1.0 - s) * v
            blend_b = 1.0 - v
            h = hue_blend_channel(orig_h, blend_h, factor)
            w = percent_blend_channel(orig_w, blend_w, factor)
            b = percent_blend_channel(orig_b, blend_b, factor)
            self.fromhwb(h, w, b)
        else:
            raise ValueError('Invalid color space value: {}'.format(str(color_space)))

        if alpha:
            self.a = rgb_blend_channel(self.a, a, factor) 
Example #19
Source File: test_colorsys.py    From gcblue with BSD 3-Clause "New" or "Revised" 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 #20
Source File: sort-colors-list.py    From rubiks-color-resolver with MIT License 5 votes vote down vote up
def step2(r, g, b, repetitions=1):
    lum = math.sqrt(0.241 * r + 0.691 * g + 0.068 * b)

    h, s, v = colorsys.rgb_to_hsv(r, g, b)

    h2 = int(h * repetitions)
    # lum2 = int(lum * repetitions)
    v2 = int(v * repetitions)

    if h2 % 2 == 1:
        v2 = repetitions - v2
        lum = repetitions - lum

    return (h2, lum, v2) 
Example #21
Source File: model.py    From photonix with GNU Affero General Public License v3.0 5 votes vote down vote up
def color_distance(self, a, b):
        # Colors are list of 3 floats (RGB) from 0.0 to 1.0
        a_h, a_s, a_v = rgb_to_hsv(a[0] / 255, a[1] / 255, a[2] / 255)
        b_h, b_s, b_v = rgb_to_hsv(b[0] / 255, b[1] / 255, b[2] / 255)
        diff_h = 1 - abs(a_h - b_h)
        diff_s = 1 - abs(a_s - b_s)
        diff_v = 1 - abs(a_v - b_v)
        score = diff_h * diff_s * diff_v
        return score 
Example #22
Source File: update_colors.py    From wasabi2d with GNU Lesser General Public License v3.0 5 votes vote down vote up
def color_key(color_pair):
    """Generate a sort key for the given colour."""
    name, c = color_pair
    arr = np.array(c)
    h, s, v = colorsys.rgb_to_hsv(*arr[:3] / 255)
    return (s == 0, h, v, s) 
Example #23
Source File: main.py    From launcher with GNU General Public License v2.0 5 votes vote down vote up
def on_color(self, instance, value):
        if not self._updating_clr:
            self._updating_clr = True
            self.hsv = rgb_to_hsv(*value[:3])
            self._updating_clr = False 
Example #24
Source File: extras.py    From eduActiv8 with GNU General Public License v3.0 5 votes vote down vote up
def rgb_to_hsv(r, g, b, a=255):
    hsv = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
    hsv255 = [int(each * 255) for each in hsv]
    return hsv255 
Example #25
Source File: main.py    From launcher with GNU General Public License v2.0 5 votes vote down vote up
def on_color(self, instance, value):
        if not self._updating_clr:
            self._updating_clr = True
            self.hsv = rgb_to_hsv(*value[:3])
            self._updating_clr = False 
Example #26
Source File: recipe-577636.py    From code with MIT License 5 votes vote down vote up
def rgb_updated(self, value):
        r = self.r_scale['value']
        g = self.g_scale['value']
        b = self.b_scale['value']
        self.update_rgb(r, g, b)
        h, s, v = colorsys.rgb_to_hsv(r, g, b)
        self.update_hsv(h, s, v)
        self.update_color_area() 
Example #27
Source File: recipe-577638.py    From code with MIT License 5 votes vote down vote up
def rgb_updated(self, value):
        # Update the interface after RBG change.
        r = self.r_scale['value']
        g = self.g_scale['value']
        b = self.b_scale['value']
        self.update_rgb(r, g, b)
        h, s, v = colorsys.rgb_to_hsv(r, g, b)
        self.update_hsv(h, s, v)
        self.update_color_area() 
Example #28
Source File: recipe-577638.py    From code with MIT License 5 votes vote down vote up
def hue(self):
        return colorsys.rgb_to_hsv(self.__rgb[0] / 0xFF,
                                   self.__rgb[1] / 0xFF,
                                   self.__rgb[2] / 0xFF)[0] 
Example #29
Source File: recipe-577638.py    From code with MIT License 5 votes vote down vote up
def saturation(self):
        return colorsys.rgb_to_hsv(self.__rgb[0] / 0xFF,
                                   self.__rgb[1] / 0xFF,
                                   self.__rgb[2] / 0xFF)[1] 
Example #30
Source File: recipe-577638.py    From code with MIT License 5 votes vote down vote up
def value(self):
        return colorsys.rgb_to_hsv(self.__rgb[0] / 0xFF,
                                   self.__rgb[1] / 0xFF,
                                   self.__rgb[2] / 0xFF)[2]