Skip to content

Instantly share code, notes, and snippets.

@duhaime
Created July 10, 2019 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duhaime/5bbbf16148566f34640a9610d1dc7c52 to your computer and use it in GitHub Desktop.
Save duhaime/5bbbf16148566f34640a9610d1dc7c52 to your computer and use it in GitHub Desktop.
random dark color - javascript
function randomDarkColor() {
var lum = -0.35;
var hex = String('#' + Math.random().toString(16).slice(2, 8).toUpperCase()).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
var c, i, rgb = '#';
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment