没有简单的物理模型可以做到这一点,画家的颜料与光线有非常复杂的相互作用。幸运的是,我们有计算机,它们不受建模物理世界的限制,我们可以让它们做任何我们想要的任意事情!
第一步是创建一个颜色轮,具有所需的色相分布,红、黄和蓝以120度为增量。网上有很多例子。我在这里创建了一个只有完全饱和颜色的,以便用来生成全RGB色域的颜色轮。轮上的颜色是完全任意的;我将橙色(60°)设置为(255,160,0),因为红色和黄色之间的中点太红了,我将纯蓝(0,0,255)移动到250°而不是240°,以使240°的蓝色看起来更好。
记住我童年时的实验,当你将红色、黄色和蓝色的等量混合在一起时,你会得到一个不确定的褐灰色。我选择了一个合适的颜色,在颜色轮的中心可以看到它;在代码中,我喜欢称它为“泥土”。
为了得到每种可能的颜色,你需要用红、黄、蓝、白和黑来混合。例如,你可以通过混合红色和白色得到粉色,通过将橙色(黄+红)与黑色混合得到棕色。
转换是以比例为基础的,而不是绝对数值。就像真正的颜料一样,混合1份红色和1份黄色与混合100份红色和100份黄色没有区别。
代码是用Python呈现的,但转换到其他语言不应该很难。最棘手的部分是添加红色、黄色和蓝色以创建色相角度。我使用向量加法,并使用
atan2
将其转换回角度。几乎所有其他操作都是用线性插值(lerp)完成的。
from math import degrees, radians, atan2, sin, cos
red = (255, 0, 0)
orange = (255, 160, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)
cyan = (0, 255, 255)
blue = (0, 0, 255)
magenta = (255, 0, 255)
white = (255, 255, 255)
black = (0, 0, 0)
mud = (94, 81, 74)
colorwheel = [(0, red), (60, orange), (120, yellow), (180, green),
(215, cyan), (250, blue), (330, magenta), (360, red)]
red_x, red_y = cos(radians(0)), sin(radians(0))
yellow_x, yellow_y = cos(radians(120)), sin(radians(120))
blue_x, blue_y = cos(radians(240)), sin(radians(240))
def lerp(left, right, left_part, total):
if total == 0:
return left
ratio = float(left_part) / total
return [l * ratio + r * (1.0 - ratio) for l,r in zip(left, right)]
def hue_to_rgb(deg):
deg = deg % 360
previous_angle, previous_color = colorwheel[0]
for angle, color in colorwheel:
if deg <= angle:
return lerp(previous_color, color, angle - deg, angle - previous_angle)
previous_angle = angle
previous_color = color
def int_rgb(rgb):
return tuple(int(c * 255.99 / 255) for c in rgb)
def rybwk_to_rgb(r, y, b, w, k):
if r == 0 and y == 0 and b == 0:
rgb = white
else:
hue = degrees(atan2(r * red_y + y * yellow_y + b * blue_y,
r * red_x + y * yellow_x + b * blue_x))
rgb = hue_to_rgb(hue)
rgb = lerp(mud, rgb, min(r, y, b), max(r, y, b))
gray = lerp(white, black, w, w+k)
rgb = lerp(rgb, gray, r+y+b, r+y+b+w+k)
return int_rgb(rgb)