kaguranana-birthday-project/node_modules/granim/lib/getLightness.js
2021-12-12 18:49:32 -08:00

28 lines
836 B
JavaScript

'use strict';
module.exports = function() {
var currentColors = this.getCurrentColors();
var gradientAverage = null;
var lightnessAverage, i;
var colorsAverage = currentColors.map(function(el) {
// Compute the average lightness of each color
// in the current gradient
return Math.max(el[0], el[1], el[2]);
});
for (i = 0; i < colorsAverage.length; i++) {
// Add all the average lightness of each color
gradientAverage = gradientAverage === null ?
colorsAverage[i] : gradientAverage + colorsAverage[i];
if (i === colorsAverage.length - 1) {
// if it's the last lightness average
// divide it by the total length to
// have the global average lightness
lightnessAverage = Math.round(gradientAverage / (i + 1));
}
}
return lightnessAverage >= 128 ? 'light' : 'dark';
};