Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only light colors randomly using JavaScript

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.

like image 804
user3610762 Avatar asked May 12 '14 05:05

user3610762


People also ask

How do I generate a random color in CSS?

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.


2 Answers

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;
}
like image 173
Kruga Avatar answered Oct 22 '22 08:10

Kruga


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

like image 41
Aliendroid Avatar answered Oct 22 '22 09:10

Aliendroid