forked from mrflix/dimensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
201 lines (169 loc) · 5.37 KB
/
utils.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
function getDimensions(values) {
let min = Infinity;
let max = 0;
for (let i = 0, l = values.length; i < l; i++) {
if (values[i] < min)
min = values[i];
if (values[i] > max)
max = values[i];
}
return {
min: min,
center: min + Math.floor((max - min) / 2),
max: max,
length: max - min
};
}
function getColorAt(x, y, imgData, width, height) {
if (!inBoundaries(x, y, width, height))
return -1;
const i = y * width * 4 + x * 4;
return rgbToHsl(imgData[i], imgData[i + 1], imgData[i + 1]);
}
function getLightnessAt(data, x, y, width, height) {
const result = inBoundaries(x, y, width, height) ? data[y * width + x] : -1;
return result;
}
function setLightnessAt(data, x, y, value, width, height) {
return inBoundaries(x, y, width, height) ? data[y * width + x] = value : -1;
}
//
// inBoundaries
// ============
//
// checks if x and y are in the canvas boundaries
//
function inBoundaries(x, y, width, height) {
if (x >= 0 && x < width && y >= 0 && y < height)
return true;
else
return false;
}
//
// Grayscale
// ---------
//
// reduces the input image data to an array of gray shades.
//
function grayscale(imgData) {
const gray = new Int16Array(imgData.length / 4);
for (let i = 0, n = 0, l = imgData.length; i < l; i += 4, n++) {
const r = imgData[i],
g = imgData[i + 1],
b = imgData[i + 2];
// weighted grayscale algorithm
gray[n] = Math.round(r * 0.3 + g * 0.59 + b * 0.11);
}
return gray;
}
/**
* Converts an RGB color value to HSL. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and l in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSL representation
*/
function rgbToHsl(r, g, b) {
r /= 255, g /= 255, b /= 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
//
// measureDistances
// ================
//
// measures the distances to the next boundary
// around pageX and pageY.
//
function measureDistances(data, imgData, width, height, input) {
let distances = {
top: 0,
right: 0,
bottom: 0,
left: 0
};
const directions = {
top: { x: 0, y: -1 },
right: { x: 1, y: 0 },
bottom: { x: 0, y: 1 },
left: { x: -1, y: 0 }
};
let area = 0;
const startLightness = getLightnessAt(data, input.x, input.y, width, height);
let lastLightness;
for (const direction in distances) {
const vector = directions[direction];
let boundaryFound = false;
let sx = input.x;
let sy = input.y;
let currentLightness;
// reset lightness to start lightness
lastLightness = startLightness;
while (!boundaryFound) {
sx += vector.x;
sy += vector.y;
currentLightness = getLightnessAt(data, sx, sy, width, height);
if (currentLightness > -1 && Math.abs(currentLightness - lastLightness) < dimensionsThreshold) {
distances[direction]++;
lastLightness = currentLightness;
} else {
boundaryFound = true;
}
}
area += distances[direction];
}
if (area <= 6) {
distances = { top: 0, right: 0, bottom: 0, left: 0 };
let similarColorStreakThreshold = 8;
for (const direction in distances) {
const vector = directions[direction];
let boundaryFound = false;
let sx = input.x;
let sy = input.y;
let currentLightness;
let similarColorStreak = 0;
lastLightness = startLightness;
while (!boundaryFound) {
sx += vector.x;
sy += vector.y;
currentLightness = getLightnessAt(data, sx, sy, width, height);
if (currentLightness > -1) {
distances[direction]++;
if (Math.abs(currentLightness - lastLightness) < dimensionsThreshold) {
similarColorStreak++;
if (similarColorStreak === similarColorStreakThreshold) {
distances[direction] -= (similarColorStreakThreshold + 1);
boundaryFound = true;
}
} else {
lastLightness = currentLightness;
similarColorStreak = 0;
}
} else {
boundaryFound = true;
}
}
}
}
distances.x = input.x;
distances.y = input.y;
distances.backgroundColor = getColorAt(input.x, input.y, imgData, width, height);
return distances;
}