受此页顶部答案的启发,我编写了一个python程序来生成N个不同的HEX颜色。不同的是,最初的作者将使用math.random()生成饱和度和亮度,而我则使用三角函数,我可以保证它总是给出唯一的色调、饱和度和亮度,同时也提供了这样的优势:i可以编程使黄色比蓝色更暗,允许与白色背景和黑色文本(我需要它做的)更好的对比度。
我实际使用的代码也通过RGB将HSL转换为HEX代码,这样我就可以创建网页颜色代码。
我的问题是:-
ps。这实际上不是我使用的代码,但都是从这里开始的。完整的python脚本是可用的这里。
干杯,
亚历克斯
>>> class generate_HSL_colours():
... def __init__( self, N, shift=0, degrees=360 ):
... dict.__init__(self)
... self.N = N
... hues = [ angle for angle in xrange( shift, shift+degrees , degrees / N ) ] # Default hues from 0 --> 360
... self.colours = generate_HSL_colours()
... def generate_HSL_colours(self,angles):
... colours = []
... colour = HSLColour()
... for angle in angles:
... cos_value = math.cos( angle * math.pi / 360 ) ## <== in radians. Degrees == cos( angle/2 ) ; so cos_value goes from 1 to -1, and 0 <= angle < 180.
... ## Could use sin_value too...
... saturation = 90 - (cos_value * 10) ## Saturation from 80 --> 100
... luminance = 50 + (cos_value * 10) ## Lightness from 60 --> 40
... HSLColour.hue = hue
... HSLColour.saturation = saturation
... HSLColour.luminance = luminance
... colours.append( HSLColour )
... return colours
...
... def __iter__(self): ## I put this in to answer a different question (see below).
... for colour in self.colours:
... yield repr(colour.hue, colour.saturation, colour.lightness)
... __iter__函数作为对这里问题的回答
发布于 2011-01-09 22:34:56
https://stackoverflow.com/questions/4642219
复制相似问题