Skip to content

Instantly share code, notes, and snippets.

@chak10
Last active December 9, 2023 06:04
Show Gist options
  • Save chak10/dc24c61c9bf2f651cb6d290eeef864c1 to your computer and use it in GitHub Desktop.
Save chak10/dc24c61c9bf2f651cb6d290eeef864c1 to your computer and use it in GitHub Desktop.
Javascript Random Dark Color
function randDarkColor() {
var lum = -0.25;
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 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;
}
@ephraimlambarte
Copy link

Thanks!

@dwivedithedev
Copy link

Thank you. Very much appreciated.

@gowthamcodes
Copy link

is there any chance to get light colors return from this function?

@chak10
Copy link
Author

chak10 commented May 21, 2021

function generateLightColorHex() {
  let color = "#";
  for (let i = 0; i < 3; i++)
    color += ("0" + Math.floor(((1 + Math.random()) * Math.pow(16, 2)) / 2).toString(16)).slice(-2);
  return color;
}

you can try this. @gowthamcodes

@rgb-me
Copy link

rgb-me commented May 28, 2022

For a horizontal bar that will be number zero ["55", 0], how would I change the color function code structure so it shows no color in that one horizontal bar (while the rest show as normal with color)? I am using the light color bar code.
At present there is a tiny tiny slice of color on the far left when I use 0.
Here is a live example: https://codepen.io/crux1/pen/KKQZMzN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment