Darken Color

Darken a hex color by a percentage.

Code

General
const 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