-
Notifications
You must be signed in to change notification settings - Fork 5
/
quantize.js
114 lines (93 loc) · 2.73 KB
/
quantize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Quantize image using median cut
// Convert raw color data to an pixel array
function rawToArray(raw) {
var out = [];
for (var i = 0; i < raw.length; i += 4) {
out.push([raw[i], raw[i + 1], raw[i + 2]]);
}
return out;
}
// Check if a number is a power of two
function powTwo(n) {
return n && (n & (n - 1)) === 0;
}
// Find the greatest color channel range in an array of pixels
function greatestColorChannel(pixels) {
var sums = [0, 0, 0];
for (var i = 0; i < pixels.length; i += 1) {
sums[0] += pixels[i][0];
sums[1] += pixels[i][1];
sums[2] += pixels[i][2];
}
if (sums[0] > sums[1]) {
if (sums[0] > sums[2]) {
return 0;
} else {
return 2;
}
} else {
if (sums[1] > sums[2]) {
return 1;
} else {
return 2;
}
}
}
// Sort array by colors with greatest value in a specified channel
function sortByChannel(pixels, channel) {
var comparison = function(a, b) {
return a[channel] - b[channel];
}
return pixels.sort(comparison);
}
// Split a pixel array in half and return the halves
function halfPixels(pixels) {
var half1;
var half2 = pixels;
half1 = half2.splice(0, Math.ceil(half2.length / 2));
return [half1, half2];
}
// Get average color from an array of pixels
function averagePixels(pixels) {
var sums = [0, 0, 0];
for (var i = 0; i < pixels.length; i += 1) {
sums[0] += pixels[i][0];
sums[1] += pixels[i][1];
sums[2] += pixels[i][2];
}
return [sums[0] / pixels.length, sums[1] / pixels.length, sums[2] / pixels.length];
}
// Find n in 2^n = x
function findExponent(x) {
var i = 0;
do {
i += 1;
} while (Math.pow(2, i) != x);
return i;
}
// Make a palette from an array of pixel arrays by averaging each pixel array
function makePalette(buckets) {
var palette = [];
for (var i = 0; i < buckets.length; i += 1) {
palette.push(averagePixels(buckets[i]));
}
return palette;
}
// Apply median cut on an array of pixels
function medianCut(pixels, paletteSize) {
if (powTwo(paletteSize) == false) {
return [];
}
var buckets = [pixels];
var repeats = findExponent(paletteSize);
for (var i = 0; i < repeats; i += 1) {
var splitBuckets = [];
for (var n = 0; n < buckets.length; n += 1) {
var splitBucket = halfPixels(buckets[n]);
splitBuckets.push(splitBucket[0]);
splitBuckets.push(splitBucket[1]);
}
buckets = splitBuckets;
}
return makePalette(buckets);
}