RGBRandom //JavaScript Repository
Description
Generates random RGB values between two color intervals.
Created: 2005.11.20
Created: 2005.11.20
Code (Download)
//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/geral/rgbrandom [rev. #1]
function RGBRandom(c, e){
var rgb = "#", r, c = parseInt(c, 16), e = parseInt(e, 16), n, h = "0123456789ABCDEF";
for(var i = 3; r = Math.round(((c >> (--i << 3) & 0xff) - (n = (e >> (i << 3) & 0xff))) * Math.random() + n), i + 1;)
for(var j = 2; j--;) rgb += h.charAt(r >> j * 4 & 0xf);
return rgb;
};
Example (Example)
<style type="text/css">
.default{
border: 1px solid #000;
margin: 3px;
width: 250px;
height: 80px;
float: left;
font: 10px verdana;
text-align: center;
color: #fff;
}
</style>
<div style="width: 780px; margin: auto;">
<script type="text/javascript">
//<![CDATA[
for(var i = 2 * 3, c; i--;) // #990000 - #ff0000 // RED
document.write("<div class='default' style='background:" + (c = RGBRandom("990000", "ff0000")) + ";'>" + c + "</div>");
for(var i = 2 * 3, c; i--;) // #009900 - #00ff00 // GREEN
document.write("<div class='default' style='background:" + (c = RGBRandom("009900", "00ff00")) + ";'>" + c + "</div>");
for(var i = 2 * 3, c; i--;) // #000099 - #0000ff // BLUE
document.write("<div class='default' style='background:" + (c = RGBRandom("000099", "0000ff")) + ";'>" + c + "</div>");
for(var i = 2 * 3, c; i--;) // #cccccc - #ffffff
document.write("<div class='default' style='color:#000;background:" + (c = RGBRandom("cccccc", "ffffff")) + ";'>" + c + "</div>");
//]]>
</script>
</div>
Help
- RGBRandom([start: String = "000000", end: String = "FFFFFF"]): String
-
Returns a RGB value within the limits.
The color intervals are distinct for the R(red), G(green) and B(blue).
- start
- initial RGB
- end
- final RGB
Rank (Votes: 12)
2.67