2017-08-07 21:03:34 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.scope.colorStatistics = {};
|
|
|
|
scope = window.scope.colorStatistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Statistic{
|
|
|
|
constructor(){
|
|
|
|
this.colors = Object.create(null);
|
|
|
|
this.numberOfColors = 0;
|
|
|
|
|
|
|
|
this.minBoundary = {count: Number.NEGATIVE_INFINITY};
|
|
|
|
this.maxBoundary = {count: Number.POSITIVE_INFINITY, previousColor: this.minBoundary};
|
|
|
|
this.minBoundary.nextColor = this.maxBoundary;
|
|
|
|
}
|
|
|
|
addColor(r, g, b, a){
|
|
|
|
var index = String.fromCharCode(r, g, b, a);
|
|
|
|
var color = this.colors[index];
|
|
|
|
if (!color){
|
2017-10-06 16:06:31 +02:00
|
|
|
color = {
|
|
|
|
index,
|
|
|
|
color: [r, g, b, a],
|
|
|
|
count: 0,
|
|
|
|
previousColor: this.minBoundary,
|
|
|
|
nextColor: this.minBoundary.nextColor
|
|
|
|
};
|
2017-08-07 21:03:34 +02:00
|
|
|
this.numberOfColors += 1;
|
|
|
|
this.minBoundary.nextColor = color;
|
|
|
|
color.nextColor.previousColor = color;
|
|
|
|
this.colors[index] = color;
|
|
|
|
}
|
|
|
|
color.count += 1;
|
|
|
|
if (color.count > color.nextColor.count){
|
|
|
|
// swap colors to remain in right order
|
2017-10-03 15:35:31 +02:00
|
|
|
// a_ -> b_ -> c -> d becomes a_ -> c -> b_ -> d
|
|
|
|
var a_ = color.previousColor;
|
|
|
|
var b_ = color;
|
2017-08-07 21:03:34 +02:00
|
|
|
var c = color.nextColor;
|
|
|
|
var d = color.nextColor.nextColor;
|
|
|
|
|
2017-10-03 15:35:31 +02:00
|
|
|
a_.nextColor = c;
|
|
|
|
c.previousColor = a_;
|
2017-08-07 21:03:34 +02:00
|
|
|
|
2017-10-03 15:35:31 +02:00
|
|
|
c.nextColor = b_;
|
|
|
|
b_.previousColor = c;
|
2017-08-07 21:03:34 +02:00
|
|
|
|
2017-10-03 15:35:31 +02:00
|
|
|
b_.nextColor = d;
|
|
|
|
d.previousColor = b_;
|
2017-08-07 21:03:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
getMaxColors(n){
|
2017-10-03 15:35:31 +02:00
|
|
|
n = Math.min(n, this.numberOfColors);
|
2017-08-07 21:03:34 +02:00
|
|
|
var colors = Object.create(null);
|
|
|
|
var current = this.maxBoundary;
|
|
|
|
for (;n && current;n -= 1){
|
|
|
|
current = current.previousColor;
|
|
|
|
colors[current.index] = current;
|
|
|
|
}
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.compute = function computeColorStatistics(rawData){
|
|
|
|
var statistic = new Statistic();
|
|
|
|
for (var i = 0, l = rawData.length; i < l; i += 4){
|
|
|
|
statistic.addColor(
|
|
|
|
rawData[i + 0],
|
|
|
|
rawData[i + 1],
|
|
|
|
rawData[i + 2],
|
|
|
|
rawData[i + 3]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return statistic;
|
2017-10-03 15:35:31 +02:00
|
|
|
};
|
2017-08-07 21:03:34 +02:00
|
|
|
}());
|