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

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

任何想法都很好。


当前回答

在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

其他回答

你可以让它们在一定的亮度范围内。这样可以稍微控制“霓虹灯”颜色的数量。例如,如果“亮度”

brightness = sqrt(R^2+G^2+B^2)

在一定的高度范围内,它就会被洗掉,变成浅色。相反,如果它在某个下限内,它就会更暗。这将消除任何疯狂的、突出的颜色,如果你选择一个非常高或非常低的边界,它们都将非常接近白色或黑色。

在javascript中:

function pastelColors(){
    var r = (Math.round(Math.random()* 127) + 127).toString(16);
    var g = (Math.round(Math.random()* 127) + 127).toString(16);
    var b = (Math.round(Math.random()* 127) + 127).toString(16);
    return '#' + r + g + b;
}

在这里看到了这个想法:http://blog.functionalfun.net/2008/07/random-pastel-colour-generator.html

我已经成功地使用triadmix和CIE94来避免相似的颜色。下图使用的输入颜色为红色、黄色和白色。在这里看到的。

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);

我强烈推荐使用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;
}