| 1225 | |
| 1226 | // ColorBox类(从main.js复制) |
| 1227 | class ColorBox { |
| 1228 | constructor(pixels, level = 0) { |
| 1229 | this.pixels = pixels; |
| 1230 | this.level = level; |
| 1231 | this.computeMinMax(); |
| 1232 | } |
| 1233 | |
| 1234 | computeMinMax() { |
| 1235 | let minR = 255, minG = 255, minB = 255, minA = 255; |
| 1236 | let maxR = 0, maxG = 0, maxB = 0, maxA = 0; |
| 1237 | |
| 1238 | for (const pixel of this.pixels) { |
| 1239 | minR = Math.min(minR, pixel[0]); |
| 1240 | minG = Math.min(minG, pixel[1]); |
| 1241 | minB = Math.min(minB, pixel[2]); |
| 1242 | minA = Math.min(minA, pixel[3]); |
| 1243 | maxR = Math.max(maxR, pixel[0]); |
| 1244 | maxG = Math.max(maxG, pixel[1]); |
| 1245 | maxB = Math.max(maxB, pixel[2]); |
| 1246 | maxA = Math.max(maxA, pixel[3]); |
| 1247 | } |
| 1248 | |
| 1249 | this.minR = minR; this.minG = minG; this.minB = minB; this.minA = minA; |
| 1250 | this.maxR = maxR; this.maxG = maxG; this.maxB = maxB; this.maxA = maxA; |
| 1251 | |
| 1252 | const rangeR = maxR - minR; |
| 1253 | const rangeG = maxG - minG; |
| 1254 | const rangeB = maxB - minB; |
| 1255 | const rangeA = maxA - minA; |
| 1256 | |
| 1257 | this.largestRange = Math.max(rangeR, rangeG, rangeB, rangeA); |
| 1258 | |
| 1259 | if (rangeR === this.largestRange) this.splitChannel = 0; |
| 1260 | else if (rangeG === this.largestRange) this.splitChannel = 1; |
| 1261 | else if (rangeB === this.largestRange) this.splitChannel = 2; |
| 1262 | else this.splitChannel = 3; |
| 1263 | } |
| 1264 | |
| 1265 | getAverageColor() { |
| 1266 | let r = 0, g = 0, b = 0, a = 0; |
| 1267 | for (const pixel of this.pixels) { |
| 1268 | r += pixel[0]; |
| 1269 | g += pixel[1]; |
| 1270 | b += pixel[2]; |
| 1271 | a += pixel[3]; |
| 1272 | } |
| 1273 | const count = this.pixels.length; |
| 1274 | return [ |
| 1275 | Math.round(r / count), |
| 1276 | Math.round(g / count), |
| 1277 | Math.round(b / count), |
| 1278 | Math.round(a / count) |
| 1279 | ]; |
| 1280 | } |
| 1281 | |
| 1282 | split() { |
| 1283 | if (this.pixels.length < 2) return null; |
| 1284 |
nothing calls this directly
no outgoing calls
no test coverage detected