1

Starting from this question : Random color generator in JavaScript and its first answer:

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

I'm asking: is there a way to avoid a certain range of colours?

Say I want to avoid C0C0C0 (silver) and all the similar shades.. I was trying to understand how the colours work here: http://www.w3schools.com/tags/ref_colorpicker.asp. I wrote C0C0C0 and I got all the similar colours but obviously the list is not complete because going up and down from C0C0C0 we have C6C6C6 and ADADAD but I would also avoid C0C0C1 which differs from silver only in the last bit.

I hope I explained myself. Can you do that somehow?

Thanks Cheers

4

2 回答 2

4

正如有人所说,类似是模糊的词:)

如果该功能真的很重要,您可以尝试使用定义“颜色差异”的 CIE LAB 颜色模型。L*a*b 型号

它有值:L, a , b

您可以计算 ΔE = sqrt((ΔL)^2 + (Δa)^2 + (Δb)^2)

模型说:

0<ΔE<1 - cannot see the difference

1 <ΔE < 2 - the difference can be seen by experienced observer

2 <ΔE < 3,5 - the difference can be seen also by unexperienced observer

3,5 <ΔE < 5 - the difference is distinct

5 < ΔE - both colors are totally different

因此,您会将 RGB 转换为 L*a*b(例如:http ://www.easyrgb.com/index.php?X=MATH )并查看是否是“不同”颜色

我从来没有这样做过,这只是想法:)

于 2013-05-28T13:41:06.100 回答
1
function get_random_color() {
    var color = new Array;

    do {
      for (var i = 0; i < 3; i++ ) {
        color[i] = Math.round(Math.random() * 255);
      }
    } while (close_to_gray(color));

    // Reformat the r, g, b binaries to #XXXXXX here, call it 'color_s'
    return color_s;
}

function close_to_gray(color) {
    var spread = 1;

    for ( var i = 0; i < 3; i++ ) {
      for ( var j = i+1; j < 3; j++ )
        if ( abs(color[i] - color[j]) > spread )
          return false;
    }

    return true;
}

类似的东西。spread您可以根据自己的喜好发挥价值。:)

于 2013-05-28T13:40:05.373 回答