Sort (hex) colors to match rainbow
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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. 回答1: 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) Now you can use this to sort your list of RGB hex colors by hue: color_list =