def parse_color_string(color_spec: str) -> str: """Handle the SVG-specific color strings and convert them to HTML #rrggbb format. Parameters ---------- color_spec : :class:`str` String in any web-compatible format Returns ------- :class:`str` Hexadecimal color string in the format `#rrggbb` """ if color_spec[0:3] == "rgb": # these are only in integer form, but the Colour module wants them in floats. splits = color_spec[4:-1].split(",") if splits[0][-1] == "%": # if the last character of the first number is a percentage, # then interpret the number as a percentage parsed_rgbs = [float(i[:-1]) / 100.0 for i in splits] else: parsed_rgbs = [int(i) / 255.0 for i in splits] hex_color = rgb_to_hex(parsed_rgbs) elif color_spec[0] == "#": # its OK, parse as hex color standard. hex_color = color_spec else: # attempt to convert color names like "red" to hex color hex_color = web2hex(color_spec, force_long=True) return hex_color
def parse_color(color): color = color.strip() if color[0:3] == "rgb": splits = color[4:-1].strip().split(",") if splits[0].strip()[-1] == "%": parsed_rgbs = [float(i.strip()[:-1]) / 100.0 for i in splits] else: parsed_rgbs = [int(i) / 255.0 for i in splits] return rgb_to_hex(parsed_rgbs) else: return web2hex(color)
def web2hex(web, force_long=False): web = web.upper() if web in PALETTE: return COLOR_MAP[web] else: return colour.web2hex(web, force_long)
def get_colour(html_colour: str) -> SassColor: """Get a SASS colour object from an HTML colour string.""" rgb = web2hex(html_colour, force_long=True)[1:] r, g, b = int(rgb[0:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16) return SassColor(r, g, b, 255)