gpt4 book ai didi

python - 用python计算相似色

转载 作者:太空狗 更新时间:2023-10-30 00:45:52 24 4
gpt4 key购买 nike

如果我有 RGB 值:255, 165, 0,如何计算 218, 255, 0 的类似颜色>255, 37, 0,但仍然适用于任何 RGB 颜色?

例如:

>>> to_analogous(0, 218, 255)
[(0, 255, 165),(0, 90, 255)]

编辑:为简单起见,可以将类似的颜色视为这样,绿色是输入颜色,然后是蓝绿色和黄绿色是输出:

(来源:tigercolor.com)

最佳答案

从 RGB 转换为 HSL 并旋转 +/- 30 度可能确实是您想要的,但不会显示色轮。分别获得 12 和 128 种颜色,从纯红色(顶部)开始,这就是您将获得的:

enter image description here enter image description here

这里是生成类似颜色的示例代码:

import colorsys

DEG30 = 30/360.
def adjacent_colors((r, g, b), d=DEG30): # Assumption: r, g, b in [0, 255]
r, g, b = map(lambda x: x/255., [r, g, b]) # Convert to [0, 1]
h, l, s = colorsys.rgb_to_hls(r, g, b) # RGB -> HLS
h = [(h+d) % 1 for d in (-d, d)] # Rotation by d
adjacent = [map(lambda x: int(round(x*255)), colorsys.hls_to_rgb(hi, l, s))
for hi in h] # H'LS -> new RGB
return adjacent

另一个色轮是通过考虑减色系统获得的。为此,让我们为简单起见考虑 RYB 色彩空间(它代表您可能在任何典型学校的艺术课上学到的色彩混合)。通过使用它,我们立即获得以下轮子:

enter image description here enter image description here

为了得到这些相似的颜色,我们考虑用RGB中的一种颜色直接表示RYB中的一种颜色,然后从RYB转换为RGB。例如,假设您有一个 RGB 的三元组 (255, 128, 0)。将该三元组称为 RYB 三元组并转换为 RGB 以获得 (255, 64, 0)。这种 RYB -> RGB 转换并不是唯一的,因为它可能有多个定义,我使用了 Gosset 和 Chen 的“Paint Inspired Color Compositing”中的那个。下面是执行转换的代码:

def _cubic(t, a, b):
weight = t * t * (3 - 2*t)
return a + weight * (b - a)

def ryb_to_rgb(r, y, b): # Assumption: r, y, b in [0, 1]
# red
x0, x1 = _cubic(b, 1.0, 0.163), _cubic(b, 1.0, 0.0)
x2, x3 = _cubic(b, 1.0, 0.5), _cubic(b, 1.0, 0.2)
y0, y1 = _cubic(y, x0, x1), _cubic(y, x2, x3)
red = _cubic(r, y0, y1)

# green
x0, x1 = _cubic(b, 1.0, 0.373), _cubic(b, 1.0, 0.66)
x2, x3 = _cubic(b, 0., 0.), _cubic(b, 0.5, 0.094)
y0, y1 = _cubic(y, x0, x1), _cubic(y, x2, x3)
green = _cubic(r, y0, y1)

# blue
x0, x1 = _cubic(b, 1.0, 0.6), _cubic(b, 0.0, 0.2)
x2, x3 = _cubic(b, 0.0, 0.5), _cubic(b, 0.0, 0.0)
y0, y1 = _cubic(y, x0, x1), _cubic(y, x2, x3)
blue = _cubic(r, y0, y1)

return (red, green, blue)

关于python - 用python计算相似色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14095849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com