我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用colorsys.rgb_to_yiq()。
def background_color(self): try: hex_color = self.info['background_color'] except KeyError: return TingApp.default_background_color try: color = _hex_color_to_tuple(hex_color) except: logging.exception('Failed to parse hex color, using default') return TingApp.default_background_color # colorsys works with colors between 0 and 1 fractional_color = _color_multiply(color, 1/255.0) y, i, q = colorsys.rgb_to_yiq(*fractional_color) if y > 0.6: y = 0.6 fractional_color = colorsys.yiq_to_rgb(y, i, q) color = _color_multiply(fractional_color, 255) logging.warning( 'Background color was too bright (white text must be visible on top of this ' 'color), color "%s" was darkened to "%s"' % (hex_color, _tuple_to_hex_color(color))) return color
def test_yiq_values(self): values = [ # rgb, yiq ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)), # black ((0.0, 0.0, 1.0), (0.11, -0.3217, 0.3121)), # blue ((0.0, 1.0, 0.0), (0.59, -0.2773, -0.5251)), # green ((0.0, 1.0, 1.0), (0.7, -0.599, -0.213)), # cyan ((1.0, 0.0, 0.0), (0.3, 0.599, 0.213)), # red ((1.0, 0.0, 1.0), (0.41, 0.2773, 0.5251)), # purple ((1.0, 1.0, 0.0), (0.89, 0.3217, -0.3121)), # yellow ((1.0, 1.0, 1.0), (1.0, 0.0, 0.0)), # white ((0.5, 0.5, 0.5), (0.5, 0.0, 0.0)), # grey ] for (rgb, yiq) in values: self.assertTripleEqual(yiq, colorsys.rgb_to_yiq(*rgb)) self.assertTripleEqual(rgb, colorsys.yiq_to_rgb(*yiq))
def yiq(self): """ Returns a 3-tuple of (y, i, q) float values; y values can be between 0.0 and 1.0, whilst i and q values can be between -1.0 and 1.0. """ return colorsys.rgb_to_yiq(self.red, self.green, self.blue)
def test_yiq_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.yiq_to_rgb(*colorsys.rgb_to_yiq(*rgb)) )
def darken_colors(self, data): def change_color(color): rgb = self._html_color_to_rgb(color) if not rgb: return color yiq = colorsys.rgb_to_yiq(*(x / 255.0 for x in rgb)) if yiq[0] > self.MIN_Y_TO_DARKEN: rgb = self._change_brightness(color, float(1 - (yiq[0] - self.MIN_Y_TO_DARKEN))) return self._rgb_to_html_color(*rgb) return color if 'combined_colors' in data: data['combined_colors'] = [change_color(color) for color in data['combined_colors']]
def RGBToYIQ(red, green, blue): return colorsys.rgb_to_yiq( float(red) / 0xff, float(green) / 0xff, float(blue) / 0xff)
def get_random_pastel_color(tone=None, black_list: list=None) -> namedtuple: """Get a random dark or light color as string, useful for CSS styling.""" light_colors_tuple = ( 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'cornsilk', 'floralwhite', 'ghostwhite', 'grey', 'honeydew', 'ivory', 'lavender', 'lavenderblush', 'lemonchiffon', 'lightcyan', 'lightgoldenrodyellow', 'lightgrey', 'lightpink', 'lightskyblue', 'lightyellow', 'linen', 'mint', 'mintcream', 'oldlace', 'papayawhip', 'peachpuff', 'seashell', 'skyblue', 'snow', 'thistle', 'white') dark_colors_tuple = ( 'brown', 'chocolate', 'crimson', 'darkblue', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkolivegreen', 'darkorange', 'darkred', 'darkslateblue', 'darkslategray', 'dimgray', 'dodgerblue', 'firebrick', 'forestgreen', 'indigo', 'maroon', 'mediumblue', 'midnightblue', 'navy', 'olive', 'olivedrab', 'royalblue', 'saddlebrown', 'seagreen', 'sienna', 'slategray', 'teal') if tone.lower() == "light": colors_tuple = light_colors_tuple elif tone.lower() == "dark": colors_tuple = dark_colors_tuple else: colors_tuple = light_colors_tuple + dark_colors_tuple if black_list: colors_tuple = tuple(set(colors_tuple).difference(set(black_list))) color = choice(colors_tuple) hexa = NAMED2HEX[color] rgb = hex2rgb(hexa) hls = rgb_to_hls(rgb.red, rgb.green, rgb.blue) hsv = rgb_to_hsv(rgb.red, rgb.green, rgb.blue) yiq = rgb_to_yiq(rgb.red, rgb.green, rgb.blue) hls = namedtuple("HLS", "h l s")( # Round bcause default precision is huge round(hls[0], 2), round(hls[1], 2), round(hls[2], 2)) hsv = namedtuple("HSV", "h s v")( round(hsv[0], 2), round(hsv[1], 2), round(hsv[2], 2)) yiq = namedtuple("YIQ", "y i q")( round(yiq[0], 2), round(yiq[1], 2), round(yiq[2], 2)) per = lambda value: int(value * 100 / 255) # To Percentage, 0~255 > 0~100% return namedtuple("PastelColor", "name hex rgb hls hsv yiq css css_prcnt")( color, hexa, rgb, hls, hsv, yiq, f"rgb({rgb.red},{rgb.green},{rgb.blue})", # rgb(int, int, int) f"rgb({per(rgb.red)}%,{per(rgb.green)}%,{per(rgb.blue)}%)") # rgb(%,%)