async def convert(cls, ctx: typing.Union[AoiContext, SlashContext], arg: str) -> "AoiColor": # noqa C901 orig = arg arg = arg.lower().strip("#x") if arg == "maddiepurple": arg = "a781e7" if arg.startswith("0x"): arg = arg try: clr = webcolors.html5_parse_simple_color( webcolors.name_to_hex(arg)) return cls(clr.red, clr.green, clr.blue) except ValueError: pass if len(arg) == 6: try: clr = webcolors.html5_parse_simple_color(f"#{arg}") return cls(clr.red, clr.green, clr.blue) except ValueError: raise commands.BadColourArgument(orig) elif len(arg) == 3: try: clr = webcolors.html5_parse_simple_color("#" + ''.join(f"{c}{c}" for c in arg)) return cls(clr.red, clr.green, clr.blue) except ValueError: raise commands.BadColourArgument(orig) raise commands.BadColourArgument(orig)
async def convert(cls, ctx: AoiContext, arg: str) -> "AoiColor": orig = arg arg = arg.lower().strip("#x") if arg.startswith("0x"): arg = arg try: clr = webcolors.html5_parse_simple_color( webcolors.name_to_hex(arg)) return cls(clr.red, clr.green, clr.blue) except ValueError: pass if len(arg) == 6: try: clr = webcolors.html5_parse_simple_color(f"#{arg}") return cls(clr.red, clr.green, clr.blue) except ValueError: return cls(0, 0, 0, attempt=orig) elif len(arg) == 3: try: clr = webcolors.html5_parse_simple_color("#" + ''.join(f"{c}{c}" for c in arg)) return cls(clr.red, clr.green, clr.blue) except ValueError: return cls(0, 0, 0, attempt=orig) return cls(0, 0, 0, attempt=orig)
def test_parse_simple_color(self): """ Test implementation of the HTML5 simple color parsing algorithm. """ test_pairs = ((u'#ffffff', (255, 255, 255)), (u'#000080', (0, 0, 128)), (u'#daa520', (218, 165, 32))) for pair in test_pairs: self.assertEqual(pair[1], webcolors.html5_parse_simple_color(pair[0]))
def test_parse_simple_color(self): """ Test implementation of the HTML5 simple color parsing algorithm. """ test_pairs = ((u'#ffffff', (255, 255, 255)), (u'#000080', (0, 0, 128)), (u'#daa520', (218, 165, 32))) for pair in test_pairs: self.assertEqual(pair[1], webcolors.html5_parse_simple_color(pair[0]))
def test_parse_simple_color(self): """ Test implementation of the HTML5 simple color parsing algorithm. """ test_pairs = ((u'#ffffff', (255, 255, 255)), (u'#000080', (0, 0, 128)), (u'#daa520', (218, 165, 32))) for raw, parsed in test_pairs: result = webcolors.html5_parse_simple_color(raw) self.assertTrue(isinstance(result, webcolors.HTML5SimpleColor)) self.assertEqual(parsed, result)
def get_text_colour(self): colours = self.livery and self.livery.colours or self.colours if colours: colours = colours.split() parsed_colours = [ html5_parse_simple_color(colour) for colour in colours ] brightness = sum( get_brightness(colour) for colour in parsed_colours) / len(colours) if brightness < .5: return '#fff'
def test_parse_simple_color(self): """ Test implementation of the HTML5 simple color parsing algorithm. """ test_pairs = ( ("#ffffff", (255, 255, 255)), ("#000080", (0, 0, 128)), ("#daa520", (218, 165, 32)), ) for raw, parsed in test_pairs: result = webcolors.html5_parse_simple_color(raw) assert isinstance(result, webcolors.HTML5SimpleColor) assert parsed == result
def test_parse_simple_color(self): """ Test implementation of the HTML5 simple color parsing algorithm. """ test_pairs = ( (u'#ffffff', (255, 255, 255)), (u'#000080', (0, 0, 128)), (u'#daa520', (218, 165, 32)) ) for raw, parsed in test_pairs: result = webcolors.html5_parse_simple_color(raw) assert isinstance(result, webcolors.HTML5SimpleColor) assert parsed == result
def get_text_colour(colours): if not colours or colours == 'Other': return colours = colours.split() colours = [html5_parse_simple_color(colour) for colour in colours] brightnesses = [get_brightness(colour) for colour in colours] colours_length = len(colours) if colours_length > 2: middle_brightness = sum(brightnesses[1:-1]) outer_brightness = (brightnesses[0] + brightnesses[-1]) brightness = (middle_brightness * 2 + outer_brightness) / ( (colours_length - 2) * 2 + 2) else: brightness = sum(brightnesses) / colours_length if brightness < .5: return '#fff'
def change_color(name): color = request.args.get('color') or request.args.get('hsv') global timer, t_kill, current_color if color: try: # hsv color value e.g. "100,50,100" current_color = hsv_to_rgb([float(x) for x in color.split(',')]) except ValueError: # color names e.g. "yellow" current_color = webcolors.html5_parse_legacy_color(color) else: color = request.args.get('rgb') # rbg color value e.g. "ff00dd" current_color = webcolors.html5_parse_simple_color(u"#" + color) stop_timer() if timer is None: t_kill = threading.Event() timer = RepeatingTimer(t_kill, set_color) timer.start() return color
def change_color(name): color = request.args.get('color') or request.args.get('hsv') global timer, t_kill, current_color if color: try: # hsv color value e.g. "100,50,100" current_color = hsv_to_rgb([float(x) for x in color.split(',')]) except ValueError: # color names e.g. "yellow" current_color = webcolors.html5_parse_legacy_color(color) else: color = request.args.get('rgb') # rbg color value e.g. "ff00dd" current_color = webcolors.html5_parse_simple_color(u"#" + color) stop_timer() if timer is None: t_kill = threading.Event() timer = RepeatingTimer(t_kill, set_color) timer.start() return color