排序(十六进制)的颜色,以配合彩虹 [英] Sort (hex) colors to match rainbow

查看:981
本文介绍了排序(十六进制)的颜色,以配合彩虹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个颜色列表重新psented十六进制$ P $ - 我需要梳理他们的颜色顺序匹配的彩虹。 - 我可以硬codea的排序顺序 - 但我觉得有一个更清洁的方法

I have a list of colors represented in hex - I need to sort them to match the order of colors in a rainbow. - I could hardcode a sort order - but I feel there's a cleaner way.

推荐答案

下面是一个函数,考虑到十六进制RGB颜色规范,返回其HSV颜色:

Here's a function that, given a color specification in hex RGB, returns its HSV color:

import colorsys

def get_hsv(hexrgb):
    hexrgb = hexrgb.lstrip("#")   # in case you have Web color specs
    r, g, b = (int(hexrgb[i:i+2], 16) / 255.0 for i in xrange(0,5,2))
    return colorsys.rgb_to_hsv(r, g, b)

现在你可以使用这个由色相您的RGB十六进制颜色列表进行排序:

Now you can use this to sort your list of RGB hex colors by hue:

color_list = ["000050", "005000", "500000"]  # GBR
color_list.sort(key=get_hsv)
print color_list

通过使用整个HSV元组排序,可以确保在一个一致的地方,对具有无色调的颜色(即灰度级)排序,而且颜色具有相同的色调,但不同的饱和度/值排序,以便相对于他们更一致的 - 饱和/值对应。

By sorting using the entire HSV tuple, you ensure that colors that have no hue (i.e. grayscales) sort in a consistent place, and that colors with the same hue but different saturations/values sort in a consistent order relative to their more-saturated/valued counterparts.

您仍然有一些乱七八糟的,如果颜色差异很大,饱和度(亮度)或值(亮度),但没有得到解决的。

You will still have something of a mess if colors vary widely by saturation (intensity) or value (brightness), but there's no getting around that.

这篇关于排序(十六进制)的颜色,以配合彩虹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆