Python webcolors.name_to_rgb() Examples

The following are 6 code examples of webcolors.name_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 webcolors , or try the search function .
Example #1
Source File: syntrax.py    From syntrax with MIT License 6 votes vote down vote up
def convert_color(c):
  rgb = c

  # Check for hex string
  try:
    rgb = hex_to_rgb(rgb)
  except (TypeError, ValueError):
    pass

  # Check for named color
  if have_webcolors:
    try:
      rgb = webcolors.name_to_rgb(rgb)
    except AttributeError:
      pass

  # Restrict to valid range
  rgb = tuple(0 if c < 0 else 255 if c > 255 else c for c in rgb)
  return rgb 
Example #2
Source File: color.py    From airport with Apache License 2.0 5 votes vote down vote up
def Color(name):
  """Return BGR value for given color name"""
  return webcolors.name_to_rgb(name)[::-1] 
Example #3
Source File: __main__.py    From flux_led with GNU Lesser General Public License v3.0 5 votes vote down vote up
def color_object_to_tuple(color):
        global webcolors_available

        # see if it's already a color tuple
        if type(color) is tuple and len(color) in [3, 4, 5]:
            return color

        # can't convert non-string
        if type(color) is not str:
            return None
        color = color.strip()

        if webcolors_available:
            # try to convert from an english name
            try:
                return webcolors.name_to_rgb(color)
            except ValueError:
                pass
            except:
                pass

            # try to convert an web hex code
            try:
                return webcolors.hex_to_rgb(webcolors.normalize_hex(color))
            except ValueError:
                pass
            except:
                pass

        # try to convert a string RGB tuple
        try:
            val = ast.literal_eval(color)
            if type(val) is not tuple or len(val) not in [3, 4, 5]:
                raise Exception
            return val
        except:
            pass
        return None 
Example #4
Source File: images.py    From blockdiag with Apache License 2.0 5 votes vote down vote up
def color_to_rgb(color):
    import webcolors
    if color == 'none' or isinstance(color, (list, tuple)):
        rgb = color
    elif re.match('#', color):
        rgb = webcolors.hex_to_rgb(color)
    else:
        rgb = webcolors.name_to_rgb(color)

    return rgb 
Example #5
Source File: flux_led.py    From homebridge-magichome with MIT License 5 votes vote down vote up
def color_object_to_tuple(color):
		global webcolors_available

		# see if it's already a color tuple
		if type(color) is tuple and len(color) == 3:
			return color

		# can't convert non-string
		if type(color) is not str:
			return None
		color = color.strip()

		if webcolors_available:
			# try to convert from an english name
			try:
				return webcolors.name_to_rgb(color)
			except ValueError:
				pass
			except:
				pass

			# try to convert an web hex code
			try:
				return webcolors.hex_to_rgb(webcolors.normalize_hex(color))
			except ValueError:
				pass
			except:
				pass

		# try to convert a string RGB tuple
		try:
			val = ast.literal_eval(color)
			if type(val) is not tuple or len(val) != 3:
				raise Exception
			return val
		except:
			pass
		return None 
Example #6
Source File: HectorController.py    From Hector9000 with MIT License 5 votes vote down vote up
def _do_dose_drink(self, msg):
        debug("start dosing drink")
        id = int(msg.payload)
        drink = drinks.available_drinks[id - 1]
        # Return ID of drink to identify that drink creation starts
        self.client.publish(self.get_returnTopic(msg.topic), msg.payload)
        progress = 0
        self.client.publish(self.get_progressTopic(msg.topic), progress)
        steps = 100 / len(drink["recipe"])
        if self.LED:
            if "color" in drink.keys():
                self.hector.dosedrink(color=webcolors.name_to_rgb(drink["color"]))
            else:
                self.hector.dosedrink()
        if self.client.want_write():
            self.client.loop_write()
        self.hector.light_on()
        self.hector.arm_out()
        debug("dose drink preparation complete")
        for step in drink["recipe"]:
            debug("dosing progress: " + str(progress))
            if step[0] == "ingr":
                pump = drinks.available_ingredients.index(step[1])
                self.hector.valve_dose(index=int(pump), amount=int(step[2]), cback=self.dose_callback, progress=(progress, steps), topic="Hector9000/doseDrink/progress")
                self.client.publish(self.get_progressTopic(msg.topic), progress + steps)
                if self.client.want_write():
                    self.client.loop_write()
            progress = progress + steps
        debug("dosing drink finished")
        if self.LED: self.hector.drinkfinish()
        time.sleep(1)
        self.hector.arm_in()
        self.hector.light_off()
        self.hector.ping(3, 0)
        debug("reset hardware")
        self.client.publish(self.get_progressTopic(msg.topic), "end", qos=1)
        while not self.client.want_write():
            pass
        self.client.loop_write()