2017-10-11 85 views
2

(HI)我不是色度学专家,但我想知道如何实现一个脚本,它会生成随机颜色,但是基于主色。javascript生成类似的随机颜色(着色器||色调||单色)

也许是随机阴影或浅色调

例如:#f25f9a的。 http://www.color-hex.com/color/f25f9a

#f25f9a 
#f36fa4 
#f47eae 
#f58fb8 
#f79fc2 
#f8afcc 
#f9bfd6 
#fbcfe0 
#fcdfea 
#fdeff4 
#ffffff 

,所以我需要做一个随机颜色

function colors() { return ('0x' + Math.floor(Math.random() * 16777215).toString(16) || 0xffffff) }; 

,之后将其转换为十六进制

function ColorToHex(hexb){return '#'+hexb.slice(2);} 

然后生成基于随机色彩或着色器或单色的颜色ColorToHex 它为一个插件开发与帧调试精灵。

感谢您的帮助,如果您知道任何代码片段?

+0

您所选择的颜色看起来像#COLOR一个线性渐变为白色,这是你想要的吗? – Kaiido

+0

对不起,我忘了链接,这里是一个很好的例子:http://www.color-hex.com/color/f25f9a。 – jon

+0

颜色是3D空间中的点。如果要生成随机的“相似”颜色,请在三维空间中选择(a)在给定点附近的随机点,(b)在某个坐标系的某条线上与该点共线。 –

回答

3

您可以将单色的增量作为255的变量作为随机乘法的可变部分。采用相同的颜色随机值,并以想要的格式构建一个字符串。

function getRandomColor(color) { 
 
    var p = 1, 
 
     temp, 
 
     random = Math.random(), 
 
     result = '#'; 
 

 
    while (p < color.length) { 
 
     temp = parseInt(color.slice(p, p += 2), 16) 
 
     temp += Math.floor((255 - temp) * random); 
 
     result += temp.toString(16).padStart(2, '0'); 
 
    } 
 
    return result; 
 
} 
 

 

 
var color = '#f25f9a', 
 
    i, 
 
    rc; 
 

 
for (i = 0; i < 10; i++) { 
 
    rc = getRandomColor(color); 
 
    document.body.innerHTML += '<div style="background-color: ' + rc + ';">' + rc + '</div>'; 
 
}

0
let randomColor = Math.ceil((Math.random() * Math.pow(255, 3))).toString(16); 
     while (randomColor.length < 6) { 
     randomColor = '0' + randomColor; 
     } 
     randomColor = '#' + randomColor; 
+1

不错的代码片段,但它只是一个颜色生成器,它只生成随机颜色,而不是随机的基于特定颜色 – jon

2

我不知道这是真的问什么(我仍然不知道问什么),我几乎可以肯定它会让比色法家伙生气,但这是一种懒惰(即非马屁精)的方式来实现类似的效果:

该解决方案使用屏幕外画布绘制渐变,然后从此渐变中提取像素。

// returns an array of CSS color strings 
 
// @from: CSS color string of first color 
 
// @to: CSS color string of last color 
 
// @numberOfShades: number of shades to be returned (from and end included) 
 
function getGradColors(from, to, numberOfShades){ 
 
    // generate a canvas context and store it in cache 
 
    var ctx = getGradColors.ctx || (getGradColors.ctx = document.createElement('canvas').getContext('2d')); 
 
    // set our canvas dimensions according to the number of shades required 
 
    var w = ctx.canvas.width = numberOfShades || 10; 
 
    ctx.canvas.height = 1; 
 
    // create a linear gradient 
 
    // (to keep 'from' and 'to' values, we set its x to 1 and width to width -1) 
 
    var grad = ctx.createLinearGradient(1,0,w-1, 0); 
 
    grad.addColorStop(0, from || 'white'); 
 
    grad.addColorStop(1, to || 'black'); 
 
    ctx.fillStyle = grad; 
 
    ctx.fillRect(0,0,w,1); // draw it 
 
    var data = ctx.getImageData(0,0,w,1); // get the pixels info ([r, g, b, a, r, g...]) 
 
    var colors = []; 
 
    data.data.forEach(function(comp, i){ 
 
    if(i%4===0){ // map each pixel in its own array 
 
     colors.push([]); 
 
     } 
 
    if(i%4===3){ // alpha 
 
     comp /= 255; 
 
     } 
 
    colors[colors.length - 1].push(comp); 
 
    }); 
 
    return colors.map(function(c){ 
 
    // return a CSS computed value 
 
    ctx.fillStyle = 'rgba('+c.join()+')'; 
 
    return ctx.fillStyle; 
 
    }); 
 
    } 
 
    
 
var shadesOfWhite = getGradColors('#f25f9a', 'white', 10); 
 
console.log('to white: ', shadesOfWhite); 
 
shadesOfWhite.forEach(generateSpan); 
 

 
container.appendChild(document.createElement('br')); 
 

 
var shadesOfBlack = getGradColors('#f25f9a', 'black', 10); 
 
console.log('to black: ', shadesOfBlack); 
 
shadesOfBlack.forEach(generateSpan); 
 

 
function generateSpan(color){ 
 
    var span = document.createElement('span'); 
 
    span.style.backgroundColor = color; 
 
    container.appendChild(span); 
 
    }
#container > span{ 
 
    width: 10vw; 
 
    height: 5vw; 
 
    display: inline-block; 
 
    } 
 
body{margin: 0}
<div id="container"></div>

+0

哎非常不错!,但也非常沉重和性能消耗。 我搜索一个纯数学算法 – jon

+0

@jon如果你不会每10秒钟称它一千次,你不应该注意到太多的性能影响。 – Kaiido