我正在寻找一种简单的算法来生成大量随机的、美观的颜色。所以没有疯狂的霓虹灯颜色,让人联想到粪便的颜色等等。

我已经找到了这个问题的解决方案,但它们依赖于替代调色板而不是RGB。 我宁愿直接使用RGB,而不是来回映射。这些其他解决方案也最多只能生成32种左右令人愉悦的随机颜色。

任何想法都很好。


当前回答

使用色彩分明的颜色。

用javascript编写。

它生成了一个视觉上不同颜色的调色板。

Distinct-colors是高度可配置的:

选择调色板中有多少种颜色 将色调限制在一个特定的范围内 限制色度(饱和度)到一个特定的范围 将亮度限制在一个特定的范围内 配置调色板的一般质量

其他回答

在php中:

function pastelColors() {
    $r = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);
    $g = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);
    $b = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);

    return "#" . $r . $g . $b;
}

来源:https://stackoverflow.com/a/12266311/2875783

我强烈推荐使用CG HSVtoRGB着色器功能,它们很棒…它给你像画家一样的自然色彩控制,而不是像CRT显示器那样的控制,你大概不是!

这是一种使1浮点值的方法。即灰色,变成1000ds的颜色和亮度和饱和度的组合等:

int rand = a global color randomizer that you can control by script/ by a crossfader etc.
float h = perlin(grey,23.3*rand)
float s = perlin(grey,54,4*rand)
float v = perlin(grey,12.6*rand)

Return float4 HSVtoRGB(h,s,v);

结果是可怕的颜色随机化!它不是天然的,但它使用自然的颜色梯度,它看起来有机和可控的闪光/粉彩参数。

对于柏林,你可以使用这个函数,它是柏林的快速之字形版本。

function  zig ( xx : float ): float{    //lfo nz -1,1
    xx= xx+32;
    var x0 = Mathf.Floor(xx);
    var x1 = x0+1;
    var v0 = (Mathf.Sin (x0*.014686)*31718.927)%1;
    var v1 = (Mathf.Sin  (x1*.014686)*31718.927)%1;
    return Mathf.Lerp( v0 , v1 , (xx)%1 )*2-1;
}

转换到另一个调色板是一种更好的方式。他们这样做是有原因的:其他调色板是“感性的”——也就是说,他们把相似的颜色放在一起,调整一个变量会以可预测的方式改变颜色。对于RGB来说,这些都不是真的,在RGB中,颜色之间没有明显的“搭配良好”的关系。

JavaScript改编的David Crow的原始答案,包括IE和Nodejs的特定代码。

generateRandomComplementaryColor = function(r, g, b){
    //--- JavaScript code
    var red = Math.floor((Math.random() * 256));
    var green = Math.floor((Math.random() * 256));
    var blue = Math.floor((Math.random() * 256));
    //---

    //--- Extra check for Internet Explorers, its Math.random is not random enough.
    if(!/MSIE 9/i.test(navigator.userAgent) && !/MSIE 10/i.test(navigator.userAgent) && !/rv:11.0/i.test(navigator.userAgent)){
        red = Math.floor((('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]) * 256));
        green = Math.floor((('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]) * 256));
        blue = Math.floor((('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]) * 256));
    };
    //---

    //--- nodejs code
    /*
    crypto = Npm.require('crypto');
    red = Math.floor((parseInt(crypto.randomBytes(8).toString('hex'), 16)) * 1.0e-19 * 256);
    green = Math.floor((parseInt(crypto.randomBytes(8).toString('hex'), 16)) * 1.0e-19 * 256);
    blue = Math.floor((parseInt(crypto.randomBytes(8).toString('hex'), 16)) * 1.0e-19 * 256);
    */
    //---

    red = (red + r)/2;
    green = (green + g)/2;
    blue = (blue + b)/2;

    return 'rgb(' + Math.floor(red) + ', ' + Math.floor(green) + ', ' + Math.floor(blue) + ')';
}

运行函数使用:

generateRandomComplementaryColor(240, 240, 240);

从算法上很难得到你想要的东西——人们研究颜色理论已经很长时间了,他们甚至不知道所有的规则。

然而,有一些规则可以用来剔除糟糕的颜色组合(例如,有冲突的颜色和选择互补的颜色的规则)。

我建议你去图书馆的艺术区看看关于色彩理论的书籍,在你尝试一个好颜色之前更好地理解什么是好颜色——看起来你甚至不知道为什么某些组合有效,而另一些则不行。

亚当