【问题标题】:Find colour based on brightness in python [closed]在python中根据亮度查找颜色[关闭]
【发布时间】:2014-07-27 08:49:27
【问题描述】:

给定一定的亮度(例如从 0 到 255),是否有任何算法可以从中导出 RGB 颜色?

这适用于 0 为黑色、255 为白色且所有其他颜色位于它们之间的情况。

【问题讨论】:

  • 请更清楚地解释您的问题?你到底想要什么?
  • 我想要一些光谱,我猜——所以任何介于 0 到 255 之间的数字都被分配了一种颜色——颜色越深越小,颜色越浅。
  • 我为迟到的评论道歉。
  • 这不是关于 python 的,请阅读颜色空间:en.wikipedia.org/wiki/Color_space。你想要的可以通过HSL色彩空间来实现:en.wikipedia.org/wiki/HSL_and_HSV

标签: python colors rgb


【解决方案1】:

在 Python 中有标准库模块colorsys。你想要的是HSL colour coordinates;那么你的“亮度”就是亮度。对于每个组件,模块使用的值是 0..1。 [请注意,虽然标准顺序是 HSL,但在 python 中它是 HLS。 ]

因此要获得不同亮度的完全饱和的红色,您可以这样做

import colorsys
hue = 0.0        # 0 is red
saturation = 1.0 # fully saturated

for luminance in range(0, 10, 1):
   r, g, b = colorsys.hls_to_rgb(hue, luminance / 10.0, saturation)
   print('{:d}, {:d}, {:d}'.format(int(r * 255), int(g * 255), int(b * 255)))

导致

0, 0, 0
51, 0, 0
102, 0, 0
153, 0, 0
204, 0, 0
255, 0, 0
255, 50, 50
255, 101, 101
255, 153, 153
254, 204, 204
255, 255, 255

同样,在整个光谱中获得单一亮度的颜色,保持亮度和饱和度不变,并在 0 到 1 之间改变色调。

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2014-12-12
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多