When it comes to colors, they are essentially mixtures of red, green, and blue with values ranging from 0-255 for each. The darkest version of a color is represented by 0 (which is basically black), and the lightest is represented by 255 (white).
If you're aiming for a "dark" color, stick to the lower half of the 0-255 range and choose values between 0 and 127. To convert these values into hex colors, simply use .toString(16)
. Repeat this process three times - once for R, once for G, and once for B.
To ensure that each base 16 value between 0-127 has a leading 0 when necessary, just add a "0"
at the beginning and then use slice to extract the last two characters, omitting the unnecessary "0"
.
Selecting colors randomly doesn't require much explanation.
document.getElementById("dark").style.background = "#" + ("0" + rando(127).toString(16)).slice(-2) + ("0" + rando(127).toString(16)).slice(-2) + ("0" + rando(127).toString(16)).slice(-2);
document.getElementById("darkFromPool").style.background = rando(["black", "darkgreen", "darkred"]).value;
div { height: 50px; margin: 10px 0px 30px 0px; border-radius: 4px; }
<script src="https://randojs.com/1.0.0.js"></script>
Random dark: <div id="dark"></div>
Random dark from pool: <div id="darkFromPool"></div>
This code simplifies randomness using randojs.com to enhance readability. If you intend to utilize this code, ensure that the following script tag is included in the head section of your HTML document:
<script src="https://randojs.com/1.0.0.js"></script>