1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00
CanvasBlocker/lib/colorStatistics.js

109 lines
2.7 KiB
JavaScript
Raw Normal View History

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 {
2019-03-12 22:24:23 +01:00
scope = require.register("./colorStatistics", {});
2017-08-07 21:03:34 +02:00
}
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){
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
};
scope.hasMoreColors = function hasMoreColors(rawData, threshold, statistic){
if (statistic){
return statistic.numberOfColors > threshold;
}
else {
var colors = Object.create(null);
var count = 0;
for (var i = 0, l = rawData.length; i < l; i += 4){
var index = String.fromCharCode(
rawData[i + 0],
rawData[i + 1],
rawData[i + 2],
rawData[i + 3]
);
if (!Object.prototype.hasOwnProperty.call(colors, index)){
colors[index] = true;
count += 1;
if (count > threshold){
return true;
}
}
}
return count > threshold;
}
};
2017-08-07 21:03:34 +02:00
}());