| 133 | } |
| 134 | |
| 135 | class LabBox { |
| 136 | constructor(px) { |
| 137 | this.pixels = px; |
| 138 | let minL = Infinity, maxL = -Infinity; |
| 139 | let mina = Infinity, maxa = -Infinity; |
| 140 | let minb = Infinity, maxb = -Infinity; |
| 141 | for (const [L, a, b] of px) { |
| 142 | if (L < minL) minL = L; if (L > maxL) maxL = L; |
| 143 | if (a < mina) mina = a; if (a > maxa) maxa = a; |
| 144 | if (b < minb) minb = b; if (b > maxb) maxb = b; |
| 145 | } |
| 146 | const Lr = maxL - minL, ar = maxa - mina, br = maxb - minb; |
| 147 | this.largestRange = Math.max(Lr, ar, br); |
| 148 | this.splitChannel = Lr >= ar && Lr >= br ? 0 : ar >= br ? 1 : 2; |
| 149 | } |
| 150 | getAvgRgb() { |
| 151 | const n = this.pixels.length; |
| 152 | let L = 0, a = 0, b = 0; |
| 153 | for (const p of this.pixels) { L += p[0]; a += p[1]; b += p[2]; } |
| 154 | return labToRgb(L / n, a / n, b / n); |
| 155 | } |
| 156 | split() { |
| 157 | if (this.pixels.length < 2) return null; |
| 158 | const ch = this.splitChannel; |
| 159 | this.pixels.sort((a, b) => a[ch] - b[ch]); |
| 160 | const mid = Math.floor(this.pixels.length / 2); |
| 161 | return [new LabBox(this.pixels.slice(0, mid)), new LabBox(this.pixels.slice(mid))]; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | let boxes = [new LabBox(labPixels)]; |
| 166 | while (boxes.length < colorCount) { |
nothing calls this directly
no outgoing calls
no test coverage detected