generated from qq15725/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palette.ts
156 lines (137 loc) · 3.88 KB
/
Palette.ts
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
import {
ImageToPixels,
MedianCut,
PixelsToColors,
} from './transformers'
import { Finder } from './Finder'
import type { PaletteOptions } from './options'
import type { ImageSource, QuantizedColor } from './types'
export type PaletteConfig = Required<PaletteOptions>
export class Palette {
config: PaletteConfig
colors: Array<QuantizedColor> = []
finder?: Finder
protected _stream: ReadableStream<Array<QuantizedColor>>
protected _streamControler!: ReadableStreamDefaultController<ImageSource>
constructor(options: PaletteOptions = {}) {
this.config = this._resolveOptions(options)
this._stream = this._createStream()
}
protected _resolveOptions(options: PaletteOptions): PaletteConfig {
const {
maxColors = 256,
statsMode = 'full',
algorithm = 'median-cut',
premultipliedAlpha = false,
tint = [0xFF, 0xFF, 0xFF],
samples = [],
} = options
return {
maxColors,
statsMode,
algorithm,
premultipliedAlpha,
tint,
samples,
}
}
protected _createStream() {
let quantizer
switch (this.config.algorithm) {
case 'median-cut':
default:
quantizer = new MedianCut(this.config.maxColors)
break
}
return new ReadableStream({
start: controler => {
this._streamControler = controler
this.config.samples.forEach(sample => controler.enqueue(sample))
},
})
.pipeThrough(new ImageToPixels())
.pipeThrough(new PixelsToColors(this.config.statsMode, this.config.premultipliedAlpha, this.config.tint))
.pipeThrough(quantizer)
}
addSample(sample: ImageSource): void {
this._streamControler.enqueue(sample)
}
generate(): Promise<Array<QuantizedColor>> {
return new Promise(resolve => {
this._streamControler.close()
this._stream.pipeTo(new WritableStream({
write: colors => {
this.colors = colors
this.finder = new Finder(colors, this.config.premultipliedAlpha, this.config.tint)
this._stream = this._createStream()
resolve(colors)
},
}))
})
}
match(color: Array<number> | string | number): { color: QuantizedColor; index: number } | undefined {
let rgba: Array<number>
if (typeof color === 'number') {
rgba = [
(color >> 24) & 0xFF,
(color >> 16) & 0xFF,
(color >> 8) & 0xFF,
color & 0xFF,
]
} else if (typeof color === 'string') {
const str = color.replace(/^#/, '')
rgba = [
`${ str[0] }${ str[1] }`,
`${ str[2] }${ str[3] }`,
`${ str[4] }${ str[5] }`,
].map(val => parseInt(val, 16))
} else if (Array.isArray(color)) {
rgba = color
} else {
throw new TypeError('Unsupported color format')
}
const index = this.finder?.findNearestIndex(rgba[0], rgba[1], rgba[2], rgba[3])
if (index === undefined || index < 0) return undefined
const targetColor = this.colors[index]
if (!targetColor) return undefined
return {
color: targetColor,
index,
}
}
toColors() {
return this.colors.slice()
}
toHexColors() {
return this.colors.map(color => color.hex)
}
toRgbColors() {
return this.colors.map(color => color.rgb)
}
toRgbIntColors() {
return this.colors.map(color => color.rgbInt)
}
toLabColors() {
return this.colors.map(color => color.lab)
}
toUint8Array(length = this.colors.length * 4): Uint8ClampedArray {
let lastRgb: any | undefined
const array = new Uint8ClampedArray(length)
for (let i = 0; i < length; i++) {
const p = i * 4
const rgb = this.colors[i]?.rgb ?? lastRgb
if (rgb) {
array[p] = rgb.r
array[p + 1] = rgb.g
array[p + 2] = rgb.b
array[p + 3] = 255
lastRgb = rgb
}
}
return array
}
clear(): void {
this.colors.length = 0
this._stream = this._createStream()
}
}