Darken a hex color by a percentage.
Code
Generalconst num = parseInt(hex.replace('#', ''), 16);
const amt = Math.round(2.55 * percent);
const R = Math.max(0, (num >> 16) - amt);
const G = Math.max(0, ((num >> 8) & 0x00FF) - amt);
const B = Math.max(0, (num & 0x0000FF) - amt);
return '#' + [R, G, B].map(x => x.toString(16).padStart(2, '0')).join('');Parameters
The hex color to darken
Percentage to darken (0-100)
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Blend Colors
Blend two hex colors together with a customizable ratio.
Hex to RGB
Convert a hex color code to RGB values.
Invert Color
Invert a hex color (get complementary color).
Is Light Color
Check if a hex color is light (good for determining text color).
Lighten Color
Lighten a hex color by a percentage.
Random Hex Color
Generate a random hexadecimal color code for use in CSS or graphics applications.