生成 n 色彩虹调色板

Bas*_*asj 6 processing rgb colors hsb color-palette

我正在尝试使用(此处可运行代码)生成具有 15 种不同颜色的彩虹:

size(360,100);
colorMode(HSB, 360, 100, 100); // Hue in degrees in [0, 360],
                               // saturation/brightness in [0, 100]
                               // like in Photoshop
noStroke();

for (int i = 0; i < 15; i++)   
{
    fill(i*24, 100, 100);      // 24*15 = 360
    rect(i*24, 0, 25, 100);
}
Run Code Online (Sandbox Code Playgroud)

但它不会产生丰富的 15 种彩虹调色板,而是缺少一些颜色(例如鲜艳的黄色)。

在此输入图像描述

是否有一种众所周知的算法可以产生生动的彩虹调色板?

Kev*_*man 7

要了解发生了什么,请尝试创建一个程序,为 0-360 之间的每个值显示一行:

size(360,100);
colorMode(HSB, 360, 100, 100);                                         
noStroke();
for (int i = 0; i < 360; i++)   
{
    fill(i, 100, 100);
    rect(i, 0, 1, 100);
}
Run Code Online (Sandbox Code Playgroud)

你会看到这个:

颜色渐变

请注意,“鲜艳的黄色”带比绿色或蓝色带窄得多。这就是为什么简单地对每个 X 值进行采样不会生成黄色。

黄色在 value 附近60,因此您可以修改增量,使其落在 60 上。绘制 12 个宽度为 30 的矩形可以让您落在黄色上:

size(360,100);
colorMode(HSB, 360, 100, 100);                                         
noStroke();
for (int i = 0; i < 360; i++)   
{
    fill(i*30, 100, 100);
    rect(i*30, 0, 30, 100);
}
Run Code Online (Sandbox Code Playgroud)

颜色渐变与黄色

或者,您可以提前提出所需的值并将它们放入数组中,而不是使用均匀分布:

int[] hueValues = {0, 15, 30, 60, 90, 120, 150, 180, 210, 225, 240, 270, 300, 330, 360};

size(360,100);
colorMode(HSB, 360, 100, 100);                                         
noStroke();
for (int index = 0; index < hueValues.length; index++)   
{
    float rectWidth = width/hueValues.length;
    fill(hueValues[index], 100, 100);
    rect(index*rectWidth, 0, rectWidth, height);
}
Run Code Online (Sandbox Code Playgroud)

数组颜色渐变