(indexed: RGBA[], rgba: RGBA)
| 240 | } |
| 241 | |
| 242 | function nearestIndexed(indexed: RGBA[], rgba: RGBA): RGBA { |
| 243 | const target = oklab(rgba) |
| 244 | const hit = indexed.reduce( |
| 245 | (best, item) => { |
| 246 | const sample = oklab(item) |
| 247 | const dl = sample.l - target.l |
| 248 | const da = sample.a - target.a |
| 249 | const db = sample.b - target.b |
| 250 | const dist = dl * dl * 2 + da * da + db * db |
| 251 | if (dist >= best.dist) return best |
| 252 | return { |
| 253 | dist, |
| 254 | item, |
| 255 | } |
| 256 | }, |
| 257 | { |
| 258 | dist: Number.POSITIVE_INFINITY, |
| 259 | item: indexed[0]!, |
| 260 | }, |
| 261 | ) |
| 262 | |
| 263 | return RGBA.clone(hit.item) |
| 264 | } |
| 265 | |
| 266 | function paletteColor(colors: TerminalColors, index: number): RGBA { |
| 267 | const value = colors.palette[index] |
no test coverage detected