get_random_pastel_color.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:anglerfish 作者: juancarlospaco 项目源码 文件源码
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(%,%)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号