UNPKG

fast-average-color

Version:

A simple library that calculates the average color of images, videos and canvas in browser environment.

36 lines (29 loc) 940 B
import { isIgnoredColor } from './ignoredColor'; export default function simpleAlgorithm(arr, len, options) { let redTotal = 0; let greenTotal = 0; let blueTotal = 0; let alphaTotal = 0; let count = 0; const ignoredColor = options.ignoredColor; for (let i = 0; i < len; i += options.step) { const alpha = arr[i + 3]; const red = arr[i] * alpha; const green = arr[i + 1] * alpha; const blue = arr[i + 2] * alpha; if (ignoredColor && isIgnoredColor(arr, i, ignoredColor)) { continue; } redTotal += red; greenTotal += green; blueTotal += blue; alphaTotal += alpha; count++; } return alphaTotal ? [ Math.round(redTotal / alphaTotal), Math.round(greenTotal / alphaTotal), Math.round(blueTotal / alphaTotal), Math.round(alphaTotal / count) ] : options.defaultColor; }