fast-average-color
Version:
A simple library that calculates the average color of images, videos and canvas in browser environment.
36 lines (29 loc) • 992 B
JavaScript
import { isIgnoredColor } from './ignoredColor';
export default function sqrtAlgorithm(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 red = arr[i];
const green = arr[i + 1];
const blue = arr[i + 2];
const alpha = arr[i + 3];
if (ignoredColor && isIgnoredColor(arr, i, ignoredColor)) {
continue;
}
redTotal += red * red * alpha;
greenTotal += green * green * alpha;
blueTotal += blue * blue * alpha;
alphaTotal += alpha;
count++;
}
return alphaTotal ? [
Math.round(Math.sqrt(redTotal / alphaTotal)),
Math.round(Math.sqrt(greenTotal / alphaTotal)),
Math.round(Math.sqrt(blueTotal / alphaTotal)),
Math.round(alphaTotal / count)
] : options.defaultColor;
}