生成可与人类区分的随机颜色

我试图在javascript中随机生成hex的颜色。

然而,产生的颜色几乎与彼此无法区分。
有没有办法改善它?


这是我正在使用的代码:

function randomColor(){ var allowed = "ABCDEF0123456789", S = "#"; while(S.length < 7){ S += allowed.charAt(Math.floor((Math.random()*16)+1)); } return S; } 

我听说过有关HSLHSV颜色模型的一些内容,但无法在我的代码中使用它。 请帮忙。

提前致谢

您可以使用一组固定的颜色,例如jquery.color.js插件中列出的颜色。

jquery.color.js插件的颜色列表:

 Colors = {}; Colors.names = { aqua: "#00ffff", azure: "#f0ffff", beige: "#f5f5dc", black: "#000000", blue: "#0000ff", brown: "#a52a2a", cyan: "#00ffff", darkblue: "#00008b", darkcyan: "#008b8b", darkgrey: "#a9a9a9", darkgreen: "#006400", darkkhaki: "#bdb76b", darkmagenta: "#8b008b", darkolivegreen: "#556b2f", darkorange: "#ff8c00", darkorchid: "#9932cc", darkred: "#8b0000", darksalmon: "#e9967a", darkviolet: "#9400d3", fuchsia: "#ff00ff", gold: "#ffd700", green: "#008000", indigo: "#4b0082", khaki: "#f0e68c", lightblue: "#add8e6", lightcyan: "#e0ffff", lightgreen: "#90ee90", lightgrey: "#d3d3d3", lightpink: "#ffb6c1", lightyellow: "#ffffe0", lime: "#00ff00", magenta: "#ff00ff", maroon: "#800000", navy: "#000080", olive: "#808000", orange: "#ffa500", pink: "#ffc0cb", purple: "#800080", violet: "#800080", red: "#ff0000", silver: "#c0c0c0", white: "#ffffff", yellow: "#ffff00" }; 

其余的只是从Javascript对象中选择一个随机属性 。

 Colors.random = function() { var result; var count = 0; for (var prop in this.names) if (Math.random() < 1/++count) result = prop; return result; }; 

使用Colors.random()可以获得人类可读的颜色。 我甚至在这里举了一个例子 。

选择最大不同颜色的最简单方法是使用HSL值而不是RGB然后操作Hue,因为它具有0到360值的值并且环绕(0是红色,因此是360);

如果您需要10种可区分的颜色,则可以将360除以10,然后通过将该值乘以索引(基于零)来选择单个颜色。 这是一个示例函数,允许您从以下颜色中选择颜色:

 function selectColor(colorNum, colors){ if (colors < 1) colors = 1; // defaults to one color - avoid divide by zero return "hsl(" + (colorNum * (360 / colors) % 360) + ",100%,50%)"; } 

这样,您可以通过随机化索引随机化颜色选择,但颜色将始终位于同一个调色板中。

这将从10的调色板中选择一个随机颜色:

 var color = selectColor(Math.floor(Math.random() * 10), 10); 

这样:

 var color = selectColor(Math.floor(Math.random() * 999), 10); 

或者您可以从调色板中选择特定颜色,例如13调色板中的第9种颜色(索引8):

 var color = selectColor(8, 13); 

这是一个小提琴: http : //jsfiddle.net/2UE2B/

例

我知道我参加这个派对已经很晚了,但是我写了一个更精细的function来为另一个项目生成一组对比色随机颜色。 它们(至少在某种程度上)具有吸引力且真正随机(不基于预定义的颜色)但我的代码比其他一些响应更复杂(所以它不仅仅是为了获得基础知识)

这适用于想要在其页面上具有多种随机颜色的用户,并且希望确保没有两种颜色太相似。

小提琴

 var generateRandomColors=function(number){ /* This generates colors using the following algorithm: Each time you create a color: Create a random, but attractive, color{ Red, Green, and Blue are set to random luminosity. One random value is reduced significantly to prevent grayscale. Another is increased by a random amount up to 100%. They are mapped to a random total luminosity in a medium-high range (bright but not white). } Check for similarity to other colors{ Check if the colors are very close together in value. Check if the colors are of similar hue and saturation. Check if the colors are of similar luminosity. If the random color is too similar to another, and there is still a good opportunity to change it: Change the hue of the random color and try again. } Output array of all colors generated */ //if we've passed preloaded colors and they're in hex format if(typeof(arguments[1])!='undefined'&&arguments[1].constructor==Array&&arguments[1][0]&&arguments[1][0].constructor!=Array){ for(var i=0;i360){ //if it's too high hsl[0]-=360 //decrease it mod 360 }else if(hsl[0]<0){ //if it's too low hsl[0]+=360 //increase it mod 360 } return hslToRGB(hsl); //convert back to rgb },differenceRecursions={//stores recursion data, so if all else fails we can use one of the hues already generated differences:[],//used to calculate the most distant hue values:[]//used to store the actual colors },fixDifference=function(color){//recursively asserts that the current color is distinctive if(differenceRecursions.values.length>23){//first, check if this is the 25th recursion or higher. (can we try any more unique hues?) //if so, get the biggest value in differences that we have and its corresponding value var ret=differenceRecursions.values[differenceRecursions.differences.indexOf(Math.max.apply(null,differenceRecursions.differences))]; differenceRecursions={differences:[],values:[]}; //then reset the recursions array, because we're done now return ret; //and then return up the recursion chain } //okay, so we still have some hues to try. var differences=[]; //an array of the "difference" numbers we're going to generate. for(var i=0;i1){ //if there's a problem with range or luminosity //set the biggest difference for these colors to be whatever is most significant differences.push(Math.min(differenceRange+lumDifference,sumDifference)); } differences.push(sumDifference); //otherwise output the raw difference in RGB values } var breakdownAt=64, //if you're generating this many colors or more, don't try so hard to make unique hues, because you might fail. breakdownFactor=25, //how much should additional colors decrease the acceptable difference shiftByDegrees=15, //how many degrees of hue should we iterate through if this fails acceptableDifference=250, //how much difference is unacceptable between colors breakVal=loadedColors.length/number*(number-breakdownAt), //break down progressively (if it's the second color, you can still make it a unique hue) totalDifference=Math.min.apply(null,differences); //get the color closest to the current color if(totalDifference>acceptableDifference-(breakVal<0?0:breakVal)*breakdownFactor){ //if the current color is acceptable differenceRecursions={differences:[],values:[]} //reset the recursions object, because we're done return color; //and return that color } //otherwise the current color is too much like another //start by adding this recursion's data into the recursions object differenceRecursions.differences.push(totalDifference); differenceRecursions.values.push(color); color=shiftHue(color,shiftByDegrees); //then increment the color's hue return fixDifference(color); //and try again },color=function(){ //generate a random color var scale=function(x){ //maps [0,1] to [300,510] return x*210+300 //(no brighter than #ff0 or #0ff or #f0f, but still pretty bright) },randVal=function(){ //random value between 300 and 510 return Math.floor(scale(Math.random())) },luminosity=randVal(), //random luminosity red=randVal(), //random color values green=randVal(), //these could be any random integer but we'll use the same function as for luminosity blue=randVal(), rescale, //we'll define this later thisColor=[red,green,blue], //an array of the random values /* #ff0 and #9e0 are not the same colors, but they are on the same range of the spectrum, namely without blue. Try to choose colors such that consecutive colors are on different ranges of the spectrum. This shouldn't always happen, but it should happen more often then not. Using a factor of 2.3, we'll only get the same range of spectrum 15% of the time. */ valueToReduce=Math.floor(lastLoadedReduction+1+Math.random()*2.3)%3, //which value to reduce /* Because 300 and 510 are fairly close in reference to zero, increase one of the remaining values by some arbitrary percent betweeen 0% and 100%, so that our remaining two values can be somewhat different. */ valueToIncrease=Math.floor(valueToIncrease+1+Math.random()*2)%3, //which value to increase (not the one we reduced) increaseBy=Math.random()+1; //how much to increase it by lastLoadedReduction=valueToReduce; //next time we make a color, try not to reduce the same one thisColor[valueToReduce]=Math.floor(thisColor[valueToReduce]/16); //reduce one of the values thisColor[valueToIncrease]=Math.ceil(thisColor[valueToIncrease]*increaseBy) //increase one of the values rescale=function(x){ //now, rescale the random numbers so that our output color has the luminosity we want return x*luminosity/thisColor.reduce(function(a,b){return a+b}) //sum red, green, and blue to get the total luminosity }; thisColor=fixDifference(thisColor.map(function(a){return rescale(a)})); //fix the hue so that our color is recognizable if(Math.max.apply(null,thisColor)>255){ //if any values are too large rescale=function(x){ //rescale the numbers to legitimate hex values return x*255/Math.max.apply(null,thisColor) } thisColor=thisColor.map(function(a){return rescale(a)}); } return thisColor; }; for(var i=loadedColors.length;i 

请注意,虽然我在我的示例中没有这样做,但这也可用于向集合添加新的不同的随机颜色:

 generateRandomColors(1,generateRandomColors(10)) 

对于随机生成的颜色,我倾向于选择这样简单的东西:

 ​function randomColor () { var max = 0xffffff; return '#' + Math.round( Math.random() * max ).toString( 16 ); }​ 

我不确定你的意思是不可识别的。 此方法不提供太多自定义,但至少可以轻松保持数字不会太亮或太暗。

如果要在生成的颜色之间留出更大的间隙,可以尝试减少允许的字符数。 我曾经使用过类似的方法,我只使用0369cf作为要拉出的字符池。 将其与重复检查相结合往往会提供更多可区分的颜色,并且仅使用#fff 3字符语法。

这是您修改为使用此方法的原始函数:

 function randomColor(){ var allowed = "0369cf".split( '' ), s = "#"; while ( s.length < 4 ) { s += allowed.splice( Math.floor( ( Math.random() * allowed.length ) ), 1 ); } return s; } 

你所说的是你不想生成随机颜色,你说你想生成不同的颜色。
您可以在此处找到有关如何执行此操作的详细教程: http : //krazydad.com/tutorials/makecolors.php 。

我使用教程中的相关代码演示了如何生成非重复颜色:

http://jsfiddle.net/rGL52/

与教程代码的唯一不同之处在于makegradient()函数返回一个颜色数组,您可以稍后在页面中随意应用这些颜色。

试试这个:

 function getRandomColor() { var letters = '0123456789ABCDEF'.split(''); var color = '#'; for (var i = 0; i < 6; i++ ) { color += letters[Math.round(Math.random() * 15)]; } return color; } 

看到它在行动: http : //jsfiddle.net/3wjgG/1/

我同意所有的答案,我们真的不知道你在这里期待什么……

这种可能性可以让您在css元素的rgb(r,g,b)输出和hex输出之间进行选择……

这是一个简单的例子,你只需要调整这个草案,但它在Firefox上有效:

  

然后你可以检索下面的值:

  

我希望这可以帮到你。 最好的祝福。

首先,为什么要从字符串构建hex值? 只需使用数字作为值,然后使用类似yourNumber.toString(16)输出。

然后,为了使颜色更加清晰,不要为每个颜色分量使用0到255的整个范围,但可以跳过10或20,或者你需要产生足够宽的差异。

我写了一个名为SwitchColors.js的小脚本,可以在这里找到: https : //github.com/akulmehta/SwitchColors.js

该脚本产生更饱和的颜色,并且可以控制亮度。 虽然它可能不会产生视觉上可区分的颜色,但它产生高饱和度和鲜艳的颜色,这也是有吸引力的。