Javascript - Generate random dark color

前端 未结 4 1006
既然无缘
既然无缘 2021-02-20 11:43

I have this method to generate me random colors for font:

function getRandomRolor() {
     var letters = \'0123456789ABCDEF\'.split(\'\');
     var color = \'#         


        
4条回答
  •  独厮守ぢ
    2021-02-20 12:43

    You could use a custom function that takes a hex and darkens it by the percent lum. You can modify it to return whatever you want back

    function ColorLuminance(hex, lum) {
      // validate hex string
      hex = String(hex).replace(/[^0-9a-f]/gi, '');
      if (hex.length < 6) {
        hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
      }
      lum = lum || 0;
    
      // convert to decimal and change luminosity
      var rgb = "#", c, i;
      for (i = 0; i < 3; i++) {
        c = parseInt(hex.substr(i*2,2), 16);
        c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
        rgb += ("00"+c).substr(c.length);
      }
    
      return rgb;
    }
    

    You could also just use hsl (Hugh, Saturation, Luminosity or Lightness). The hsl link actually goes through the above code.

提交回复
热议问题