UNPKG

@bencevans/color-array-average

Version:

Calculate the average color given an array of CSS Hex Colors

22 lines (17 loc) 631 B
const averageColor = colors => { const [totalR, totalG, totalB] = colors.reduce((prev, curr) => { curr = curr.substring(1); for (let index = 0; index < 3; index++) { let col = curr.substr(index * (curr.length / 3), (curr.length / 3)); col = col.length === 1 ? col + col : col; col = parseInt(col, 16); prev[index] += (col / colors.length); } return prev; }, [0, 0, 0]); return '#' + Math.round(totalR).toString(16).padStart(2, '0') + Math.round(totalG).toString(16).padStart(2, '0') + Math.round(totalB).toString(16).padStart(2, '0'); }; module.exports = averageColor;