用python计算类似的颜色 [英] Calculating the analogous color with python

查看:594
本文介绍了用python计算类似的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有RGB值: 255,165,0 ,可以做什么来计算 218,255的类似颜色,0 255,37,0 ,但是仍然应用任何RGB颜色?

If I had RGB values: 255, 165, 0, what could be done to calculate the analogous color(s) of 218, 255, 0 and 255, 37, 0, but still applying for any RGB color?

例如:

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

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

For simplicity, an analogous color can be viewed as this, green being the input color, then the blue green and yellow green being the output:

推荐答案

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

Converting from RGB to HSL and rotating +/- 30 degrees might be indeed what you want, but you will not get the color wheel showed. Obtaining, respectively, 12 and 128 colors, starting with pure red (at top), this is what you will get:

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

And here is sample code to produce the analogous colors:

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色彩空间为简单(它代表你可能在任何典型的学校的艺术课程中学习的颜色混合)。使用它,我们马上获得以下轮子:

The other color wheel was obtained by considering a subtractive color system. For that, let us consider the RYB color space for simplicity (it represents the color mixing you probably learned in your art classes in any typical school). By using it, we immediately obtain the following wheels:

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

To obtain these analogous colors, we consider a color in RGB to directly represent a color in RYB, and then convert from RYB to RGB. For example, suppose you have a triple (255, 128, 0) in RGB. Call that triple a RYB triple and convert to RGB to obtain (255, 64, 0). This RYB -> RGB conversion is not unique in the sense that there might be multiple definitions for it, I've used the one from "Paint Inspired Color Compositing" by Gosset and Chen. Here is the code to perform the conversion:

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计算类似的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆