用python计算类似的颜色

Hai*_*irr 5 python colors

如果我有RGB值:255, 165, 0,可以做些什么来计算的类似色(S)218, 255, 0255, 37, 0,但仍然申请任何RGB颜色?

例如:

>>> to_analogous(0, 218, 255)
[(0, 255, 165),(0, 90, 255)]
Run Code Online (Sandbox Code Playgroud)

编辑:为简单起见,类似的颜色可以看作是这样,绿色是输入颜色,然后蓝绿色和黄绿色是输出:http:
//www.tigercolor.com/Images/Analogous.gif

mmg*_*mgp 9

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

在此输入图像描述 在此输入图像描述

以下是生成类似颜色的示例代码:

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
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述 在此输入图像描述

为了获得这些类似的颜色,我们认为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)
Run Code Online (Sandbox Code Playgroud)