Для любого (скорее всего, моего будущего), ищущего быстрый / грязный / удобный / es6 способ смешать два шестнадцатеричных цвета на определенную величину, попробуйте это:
// blend two hex colors together by an amount
function blendColors(colorA, colorB, amount) {
const [rA, gA, bA] = colorA.match(/\w\w/g).map((c) => parseInt(c, 16));
const [rB, gB, bB] = colorB.match(/\w\w/g).map((c) => parseInt(c, 16));
const r = Math.round(rA + (rB - rA) * amount).toString(16).padStart(2, '0');
const g = Math.round(gA + (gB - gA) * amount).toString(16).padStart(2, '0');
const b = Math.round(bA + (bB - bA) * amount).toString(16).padStart(2, '0');
return '#' + r + g + b;
}
Где amount
следуетбыть 0
до 1
, где 0
точно равно colorA
, 1
точно colorB
и 0.5
как "средняя точка".