Below is my code that shows all types of color like dark blue,light blue,dark pink,light pink etc. But I want to get only light colors using JavaScript. Is it possible?
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
How can I do it? Thank you.
There is no way to do this using CSS only. CSS doesn't support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array.
You can also use HSL colors.
HSL stands for hue, saturation, and lightness - and represents a cylindrical-coordinate representation of colors.
An HSL color value is specified with: hsl(hue, saturation, lightness).
Hue is a degree on the color wheel (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue. Saturation is a percentage value; 0% means a shade of gray and 100% is the full color. Lightness is also a percentage; 0% is black, 100% is white.
function getRandomColor() {
color = "hsl(" + Math.random() * 360 + ", 100%, 75%)";
return color;
}
you can do so by cutting the string off forcing the random to be high number in HEX
function getRandomColor() {
var letters = 'BCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * letters.length)];
}
return color;
}
quick dirty way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With