Alon Shmiel
Alon Shmiel

Reputation: 7121

randomize colors that have a coloring of light blue

I am trying to randomize colors that have a coloring of light blue..

I know I can get all the colors of:

rgb(110, 179, 185), rgb(110, 179, 186), .... ,rgb(110, 179, 255);

and

rgb(110, 178, 185), ... , rgb(110, 178, 255);

but there are a lot of variations that exist..

Is it possible?

I have a function that randomizes a color:

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;

}

and I found this code:

http://jsfiddle.net/tWyq6/4/

any help appreciated!

Upvotes: 0

Views: 96

Answers (4)

loxxy
loxxy

Reputation: 13151

Here you go:

function get_random_blue() {
    return "rgb(110," 
        + ((179 - Math.random()*2)  <<0) + "," 
        + ((200 + Math.random()*55) <<0) + ")";
}

Sample Use :

 get_random_blue()   // "rgb(110,177,222)"
 get_random_blue()   // "rgb(110,177,243)"
 get_random_blue()   // "rgb(110,177,200)"
 get_random_blue()   // "rgb(110,178,225)"
 get_random_blue()   // "rgb(110,178,209)"
 get_random_blue()   // "rgb(110,178,244)"

Upvotes: 2

Eugene
Eugene

Reputation: 732

I would recommend you to use another color model like HSV or HSL. It is much easier to pick required color (represented by hue) and then randomize other properties (saturation and value/lightness). Sample using:

function rand(min, max) {
    // return float random between min and max;
}
var randBlue = hsl2rgb(240, 1, rand(0.7, 0.9));

Here is updated fiddle

Upvotes: 1

Rob Sedgwick
Rob Sedgwick

Reputation: 5226

H i, Perhaps this should be a comment , but it was getting large, so i will throw it here

Red Green and Blue is RGB

You will notice in your example RGB's that the last number is higher, this is what will make your colour considered more blue, as long as that channel is higher than the rest ( the R and the G ), you will create a colour that is considered 'Blue'.

Like a mixing desk, the channel is pushed higher and so is louder. ( hope no suck egg there )

  • Your randomisation function needs to ensure the 'B' value is higher than the the 'R' and the 'G' value.
  • The lower the values ( eg - R:0,G:0,B:10 ) will result in darker colours
  • The higher the values ( eg - R:220,G:220,B:255 ) will result in lighter colours.

So in your random colour functions you will need to set up some rules :

  • a number threshold to never be lower than ( this is your lightness ) - say 150
  • that the 'B' value is always higher than the 'R' and the 'G' value ( this is your blue )
  • The max for any value is 255

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66388

Well, one way is just alter the functions you found and tweak to your needs:

function randHex(a, b) {
    return (Math.floor(Math.random() * a) + b).toString(16);
}

function randColor() {
    return "6E" + randHex(20, 160) + "" + randHex(56, 200);
}

This will give back colors with 110 as red, 160 to 180 as green and 56 to 256 as blue.

Updated fiddle.

Upvotes: 2

Related Questions