我正在寻找一种简单的算法来生成大量随机的、美观的颜色。所以没有疯狂的霓虹灯颜色,让人联想到粪便的颜色等等。
我已经找到了这个问题的解决方案,但它们依赖于替代调色板而不是RGB。 我宁愿直接使用RGB,而不是来回映射。这些其他解决方案也最多只能生成32种左右令人愉悦的随机颜色。
任何想法都很好。
我正在寻找一种简单的算法来生成大量随机的、美观的颜色。所以没有疯狂的霓虹灯颜色,让人联想到粪便的颜色等等。
我已经找到了这个问题的解决方案,但它们依赖于替代调色板而不是RGB。 我宁愿直接使用RGB,而不是来回映射。这些其他解决方案也最多只能生成32种左右令人愉悦的随机颜色。
任何想法都很好。
当前回答
使用色彩分明的颜色。
用javascript编写。
它生成了一个视觉上不同颜色的调色板。
Distinct-colors是高度可配置的:
选择调色板中有多少种颜色 将色调限制在一个特定的范围内 限制色度(饱和度)到一个特定的范围 将亮度限制在一个特定的范围内 配置调色板的一般质量
其他回答
在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
我会使用色轮并在任意位置加上黄金角(137.5度)
http://en.wikipedia.org/wiki/Golden_angle
为了每次都能得到不重叠的不同颜色。
调整颜色轮的亮度,你也可以得到不同的亮/暗颜色组合。
我发现这篇博客文章很好地解释了这个问题和使用黄金比例的解决方案。
http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
更新:我刚刚发现了另一种方法:
它被称为RYB(红黄蓝)方法,本文对其进行了描述:
http://threekings.tk/mirror/ryb_TR.pdf
作为“油漆灵感的颜色合成”。
算法生成颜色,并选择每个新颜色以最大化其与先前选择的颜色的欧几里得距离。
在这里你可以找到一个很好的javascript实现:
http://afriggeri.github.com/RYB/
更新2:
巴黎政治学院刚刚发布了一个名为“I want Hue”的工具,可以为数据科学家生成调色板。使用不同的颜色空间,并通过使用k-means聚类或力向量(斥力图)生成调色板,这些方法的结果非常好,他们在他们的网页上展示了理论和实现。
http://tools.medialab.sciences-po.fr/iwanthue/index.php
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);
在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
使用色彩分明的颜色。
用javascript编写。
它生成了一个视觉上不同颜色的调色板。
Distinct-colors是高度可配置的:
选择调色板中有多少种颜色 将色调限制在一个特定的范围内 限制色度(饱和度)到一个特定的范围 将亮度限制在一个特定的范围内 配置调色板的一般质量