Get only light colors using JavaScript

Below is my code, which shows all types of colors, such as blue, cyan, 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 this? Thank.

+4
source share
3 answers

you can do this by cutting the string, causing random to be a large number in the 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

+9
source

You can also use HSL colors .

HSL stands for hue, saturation and lightness - and is a representation of colors in the form of a cylindrical coordinate.

HSL : hsl (, , ).

( 0 360) - 0 ( 360) - , 120 - , 240 - . - ; 0% , 100% - . ; 0% - , 100% .

function getRandomColor() {
  color = "hsl(" + Math.random() * 360 + ", 100%, 75%)";
  return color;
}
+7

Slight improvement:

function getRandomColor() {
  return 'rgb(' + 
    (Math.floor(Math.random()*56)+200) + ', ' +
    (Math.floor(Math.random()*56)+200) + ', ' +
    (Math.floor(Math.random()*56)+200) +
    ')';
}
+1
source

Source: https://habr.com/ru/post/1540170/


All Articles