受此页面上的最佳答案的启发,我编写了一个 python 程序来生成 N 种不同的 HEX 颜色。不同之处在于,原作者将使用 math.random() 生成饱和度和亮度,而我使用三角函数,我可以保证它始终会给出独特的色调、饱和度和亮度,同时还提供了我可以编程的优势黄色看起来比蓝色更暗,可以与白色背景和黑色文本形成更好的对比(我需要它)。
我实际使用的代码还通过 RGB 将 HSL 转换为 HEX 代码,这样我就可以创建 Web 颜色代码。
我的问题是:-
- 使用这个模型,我怎么能保证红色不会出现在绿色旁边?
- 生成颜色代码很容易,但我怎样才能轻松看到它们呢?我目前必须将相当大的文件上传到生成 pdf / png / eps 的服务器,然后再再次下载。
- 我可以通过测试来做到这一点,但是有没有人有使用 HSL 模型生成颜色的经验,这些颜色与白色背景的对比度最大化,颜色顶部有黑色文本?蓝调可以使黑色文字真的很难看到,而黄色有时很难在白色背景下看到......
附言。这实际上不是我使用的代码,但这一切都从这里开始。完整的 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__
函数作为问题的答案写在这里