Reputation: 7121
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:
any help appreciated!
Upvotes: 0
Views: 96
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
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
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 )
So in your random colour functions you will need to set up some rules :
Upvotes: 1
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.
Upvotes: 2