Blend Colors

Blend two hex colors together with a customizable ratio.

Code

General
const c1 = parseInt(color1.slice(1), 16), c2 = parseInt(color2.slice(1), 16);
const r = Math.round(((c1 >> 16) & 255) * (1 - ratio) + ((c2 >> 16) & 255) * ratio);
const g = Math.round(((c1 >> 8) & 255) * (1 - ratio) + ((c2 >> 8) & 255) * ratio);
const b = Math.round((c1 & 255) * (1 - ratio) + (c2 & 255) * ratio);
return '#' + [r, g, b].map(x => x.toString(16).padStart(2, '0')).join('');

Parameters

First hex color

Second hex color

Blend ratio (0-1)

Browser·fetch() may be limited by CORS

More JavaScript Snippets