-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (114 loc) · 3.17 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var quantize = require('quantize');
var Jimp = require('jimp');
var ColorBox = {
minVal: (arr) => {
return Math.min.apply(null, arr);
},
maxVal: (arr) => {
return Math.max.apply(null, arr);
},
getColorType: (rgb) => {
//TODO: increase granularity to include CMY?
var min = ColorBox.minVal(rgb);
var max = ColorBox.maxVal(rgb);
//init to RGB
var RGB = { 0: "R", 1: "G", 2: "B" };
var colorType = RGB[ rgb.indexOf(max) ];
return colorType;
},
getLumSatValue: (rgb) => {
var luminosity = ColorBox.maxVal(rgb);
var saturation = (luminosity - ColorBox.minVal(rgb)) / luminosity;
if ( luminosity == 0 ) {
return 0;
} else {
return luminosity + Math.round(saturation * 255);
}
},
sortByLumSat: (colors) => {
return colors.slice(0).sort((a, b) => {
return ColorBox.getLumSatValue(b) - ColorBox.getLumSatValue(a);
});
},
isGrayish: (rgb) => {
return (ColorBox.minVal(rgb) + 5)> ColorBox.maxVal(rgb);
},
getMostFrequentColorType: (colors) => {
var tally = {};
var count = 0;
var mostCommon = "";
colors.forEach((color) => {
var val = ColorBox.getColorType(color);
if (!tally[val]) {
tally[val] = 1;
} else {
tally[val] += 1;
}
if (tally[val] > count) {
count = tally[val];
mostCommon = val;
}
});
return mostCommon;
},
getFirstFrequentColor: (colors) => {
var mostFrequentColorType = ColorBox.getMostFrequentColorType(colors);
var firstFrequentColor = colors.filter((color) => {
return ColorBox.getColorType(color) == mostFrequentColorType;
})[0];
return firstFrequentColor;
},
requestDominantColorsFromImageByUrl: (imageUrl) => {
return new Promise((resolve, reject) => {
Jimp.read(imageUrl, function (err, image) {
if (err) {
console.error('Error reading image file...', imageUrl, err);
return reject(err);
}
const pixelArray = [];
image.scan(
0,
0,
image.bitmap.width,
image.bitmap.height,
function (x, y, idx) {
// straight outta the jimp doc!
// https://github.com/oliver-moran/jimp/tree/master/packages/jimp#low-level-manipulation
var r = this.bitmap.data[idx + 0];
var g = this.bitmap.data[idx + 1];
var b = this.bitmap.data[idx + 2];
pixelArray.push([r, g, b]);
}
);
var quantizedColors = quantize(pixelArray, 256).palette();
if (quantizedColors) {
return resolve(quantizedColors);
} else {
return reject();
}
});
});
},
getBaseColor: (colors) => {
return ColorBox.getFirstFrequentColor(colors);
},
getAccentColor: (colors) => {
var colorsByLumSat = ColorBox.sortByLumSat(colors);
return colorsByLumSat[0];
},
getBaseAndAccentColor: (colors) => {
var baseColor = ColorBox.getBaseColor(colors);
var accentColor = ColorBox.getAccentColor(colors);
return [ baseColor, accentColor ];
},
requestBaseAndAccentColorFromImageByUrl: (imageUrl) => {
return ColorBox.requestDominantColorsFromImageByUrl(imageUrl)
.then((colors) => {
return ColorBox.getBaseAndAccentColor(colors);
})
.catch((err) => {
console.error('Error loading colors from image. err:', err);
});
}
}
module.exports = ColorBox;