Reputation: 81
I have a random color overlay on the media-boxes of this site.
http://www.reportageborsen.se/reportageborsen/wordpress/
I had some really good help from the mates here at stackoverflow wich resulted in this script:
var getRandomInRange = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
$('.media-box').each(function() {
var mediaBox = $(this);
var mask = mediaBox.find('.mask');
var hue = 'rgb(' + getRandomInRange(100, 255) + ',' + getRandomInRange(100, 255) + ',' + getRandomInRange(100, 255) + ')';
mask.css({
backgroundColor : hue,
opacity : 0.7
});
mediaBox.hover(function() {
mask.stop(true, true).fadeIn();
}, function() {
mask.stop(true, true).fadeOut();
});
});
Fiddle link: http://jsfiddle.net/b5ZPq/3/
However, I would love to have more of the brighter colors and less of the greyish ones. I understand that it can be done with HSL values instead of RGB values.
So I tried to convert the css background rgb values to hsl values and also converted the script, but I didn't get it to work.
var getRandomInRange = function(min, max) {
return Math.floor(Math.random() * (max - min + 1), 10) + min;
};
$('.media-box').each(function() {
var mediaBox = $(this);
var mask = mediaBox.find('.mask');
var hue = 'hsl(' + getRandomInRange(0, 360) + ',' + getRandomInRange(70, 100) + '%' + getRandomInRange(45, 55) + '%)';
mask.css({
backgroundColor: hue,
opacity: 0.7
});
mediaBox.hover(function() {
mask.stop(true, true).fadeIn();
}, function() {
mask.stop(true, true).fadeOut();
});
});
http://jsfiddle.net/zolana/Kc9U4/5/ (the fiddle, updated, working: http://jsfiddle.net/zolana/Kc9U4/9/ )
(I'm not looking for a script that converts all the RGB values to HSL values (I know there are scripts with that purpose) rather to have a solid script for this specific task.)
Upvotes: 2
Views: 637
Reputation: 10057
Remember that when you use HSL colors (and others), you need to separate each value with a comma and use the correct notation. In this case, it looks like the following.
hsl ( int hue , int saturation % , int lightness % )
You were missing a comma after the second argument (specifically right after the percent sign).
var hue = 'hsl(' + getRandomInRange(0, 360) + ',' + getRandomInRange(70, 100) + '%,' + getRandomInRange(45, 55) + '%)';
Upvotes: 4
Reputation: 545
Check out this code I wrote:
function randomColor(){
var h = Math.random();
var s = 0.99;
var v = 0.99;
h = h + 0.618033988749895;
h = h % 1;
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return "rgba("+Math.floor(r*255)+","+ Math.floor(g*255)+","+ Math.floor(b*255)+","+ 0.2+")";
}
It generates a random Hue value, constant values for s and v. So this returns a random bright rgb color. Also the colors are bright and different because I have used the golden ratio. Try to use this and get back if you get any problems.
Upvotes: 1