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 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*! Fast Average Color | © 2022 Denis Seleznev | MIT License | https://github.com/fast-average-color/fast-average-color */
function toHex(num) {
	let str = num.toString(16)
	return str.length === 1 ? '0' + str : str
}
function arrayToHex(arr) {
	return '#' + arr.map(toHex).join('')
}
function isDark(color) {
	// http://www.w3.org/TR/AERT#color-contrast
	let result = (color[0] * 299 + color[1] * 587 + color[2] * 114) / 1000
	return result < 128
}
function prepareIgnoredColor(color) {
	if (!color) {
		return []
	}
	return isRGBArray(color) ? color : [color]
}
function isRGBArray(value) {
	return Array.isArray(value[0])
}
function isIgnoredColor(data, index, ignoredColor) {
	for (let i = 0; i < ignoredColor.length; i++) {
		if (isIgnoredColorAsNumbers(data, index, ignoredColor[i])) {
			return true
		}
	}
	return false
}
function isIgnoredColorAsNumbers(data, index, ignoredColor) {
	switch (ignoredColor.length) {
		case 3:
			// [red, green, blue]
			if (isIgnoredRGBColor(data, index, ignoredColor)) {
				return true
			}
			break
		case 4:
			// [red, green, blue, alpha]
			if (isIgnoredRGBAColor(data, index, ignoredColor)) {
				return true
			}
			break
		case 5:
			// [red, green, blue, alpha, threshold]
			if (isIgnoredRGBAColorWithThreshold(data, index, ignoredColor)) {
				return true
			}
			break
		default:
			return false
	}
}
function isIgnoredRGBColor(data, index, ignoredColor) {
	// Ignore if the pixel are transparent.
	if (data[index + 3] !== 255) {
		return true
	}
	if (data[index] === ignoredColor[0] &&
		data[index + 1] === ignoredColor[1] &&
		data[index + 2] === ignoredColor[2]) {
		return true
	}
	return false
}
function isIgnoredRGBAColor(data, index, ignoredColor) {
	if (data[index + 3] && ignoredColor[3]) {
		return data[index] === ignoredColor[0] &&
			data[index + 1] === ignoredColor[1] &&
			data[index + 2] === ignoredColor[2] &&
			data[index + 3] === ignoredColor[3]
	}
	// Ignore rgb components if the pixel are fully transparent.
	return data[index + 3] === ignoredColor[3]
}
function inRange(colorComponent, ignoredColorComponent, value) {
	return colorComponent >= (ignoredColorComponent - value) &&
		colorComponent <= (ignoredColorComponent + value)
}
function isIgnoredRGBAColorWithThreshold(data, index, ignoredColor) {
	let redIgnored = ignoredColor[0]
	let greenIgnored = ignoredColor[1]
	let blueIgnored = ignoredColor[2]
	let alphaIgnored = ignoredColor[3]
	let threshold = ignoredColor[4]
	let alphaData = data[index + 3]
	let alphaInRange = inRange(alphaData, alphaIgnored, threshold)
	if (!alphaIgnored) {
		return alphaInRange
	}
	if (!alphaData && alphaInRange) {
		return true
	}
	if (inRange(data[index], redIgnored, threshold) &&
		inRange(data[index + 1], greenIgnored, threshold) &&
		inRange(data[index + 2], blueIgnored, threshold) &&
		alphaInRange) {
		return true
	}
	return false
}

function dominantAlgorithm(arr, len, options) {
	let colorHash = {}
	let divider = 24
	let ignoredColor = options.ignoredColor
	let step = options.step
	let max = [0, 0, 0, 0, 0]
	for (let i = 0; i < len; i += step) {
		let red = arr[i]
		let green = arr[i + 1]
		let blue = arr[i + 2]
		let alpha = arr[i + 3]
		if (ignoredColor && isIgnoredColor(arr, i, ignoredColor)) {
			continue
		}
		let key = Math.round(red / divider) + ',' +
			Math.round(green / divider) + ',' +
			Math.round(blue / divider)
		if (colorHash[key]) {
			colorHash[key] = [
				colorHash[key][0] + red * alpha,
				colorHash[key][1] + green * alpha,
				colorHash[key][2] + blue * alpha,
				colorHash[key][3] + alpha,
				colorHash[key][4] + 1
			]
		}
		else {
			colorHash[key] = [red * alpha, green * alpha, blue * alpha, alpha, 1]
		}
		if (max[4] < colorHash[key][4]) {
			max = colorHash[key]
		}
	}
	let redTotal = max[0]
	let greenTotal = max[1]
	let blueTotal = max[2]
	let alphaTotal = max[3]
	let count = max[4]
	return alphaTotal ? [
		Math.round(redTotal / alphaTotal),
		Math.round(greenTotal / alphaTotal),
		Math.round(blueTotal / alphaTotal),
		Math.round(alphaTotal / count)
	] : options.defaultColor
}

function simpleAlgorithm(arr, len, options) {
	let redTotal = 0
	let greenTotal = 0
	let blueTotal = 0
	let alphaTotal = 0
	let count = 0
	let ignoredColor = options.ignoredColor
	let step = options.step
	for (let i = 0; i < len; i += step) {
		let alpha = arr[i + 3]
		let red = arr[i] * alpha
		let green = arr[i + 1] * alpha
		let blue = arr[i + 2] * alpha
		if (ignoredColor && isIgnoredColor(arr, i, ignoredColor)) {
			continue
		}
		redTotal += red
		greenTotal += green
		blueTotal += blue
		alphaTotal += alpha
		count++
	}
	return alphaTotal ? [
		Math.round(redTotal / alphaTotal),
		Math.round(greenTotal / alphaTotal),
		Math.round(blueTotal / alphaTotal),
		Math.round(alphaTotal / count)
	] : options.defaultColor
}

function sqrtAlgorithm(arr, len, options) {
	let redTotal = 0
	let greenTotal = 0
	let blueTotal = 0
	let alphaTotal = 0
	let count = 0
	let ignoredColor = options.ignoredColor
	let step = options.step
	for (let i = 0; i < len; i += step) {
		let red = arr[i]
		let green = arr[i + 1]
		let blue = arr[i + 2]
		let alpha = arr[i + 3]
		if (ignoredColor && isIgnoredColor(arr, i, ignoredColor)) {
			continue
		}
		redTotal += red * red * alpha
		greenTotal += green * green * alpha
		blueTotal += blue * blue * alpha
		alphaTotal += alpha
		count++
	}
	return alphaTotal ? [
		Math.round(Math.sqrt(redTotal / alphaTotal)),
		Math.round(Math.sqrt(greenTotal / alphaTotal)),
		Math.round(Math.sqrt(blueTotal / alphaTotal)),
		Math.round(alphaTotal / count)
	] : options.defaultColor
}

function getDefaultColor(options) {
	return getOption(options, 'defaultColor', [0, 0, 0, 0])
}
function getOption(options, name, defaultValue) {
	return (options[name] === undefined ? defaultValue : options[name])
}

let MIN_SIZE = 10
let MAX_SIZE = 100
function isSvg(filename) {
	return filename.search(/\.svg(\?|$)/i) !== -1
}
function getOriginalSize(resource) {
	if (isInstanceOfHTMLImageElement(resource)) {
		let width = resource.naturalWidth
		let height = resource.naturalHeight
		// For SVG images with only viewBox attribute
		if (!resource.naturalWidth && isSvg(resource.src)) {
			width = height = MAX_SIZE
		}
		return {
			width: width,
			height: height,
		}
	}
	if (isInstanceOfHTMLVideoElement(resource)) {
		return {
			width: resource.videoWidth,
			height: resource.videoHeight
		}
	}
	return {
		width: resource.width,
		height: resource.height
	}
}
function getSrc(resource) {
	if (isInstanceOfHTMLCanvasElement(resource)) {
		return 'canvas'
	}
	if (isInstanceOfOffscreenCanvas(resource)) {
		return 'offscreencanvas'
	}
	if (isInstanceOfImageBitmap(resource)) {
		return 'imagebitmap'
	}
	return resource.src
}
function isInstanceOfHTMLImageElement(resource) {
	return typeof HTMLImageElement !== 'undefined' && resource instanceof HTMLImageElement
}
let hasOffscreenCanvas = typeof OffscreenCanvas !== 'undefined'
function isInstanceOfOffscreenCanvas(resource) {
	return hasOffscreenCanvas && resource instanceof OffscreenCanvas
}
function isInstanceOfHTMLVideoElement(resource) {
	return typeof HTMLVideoElement !== 'undefined' && resource instanceof HTMLVideoElement
}
function isInstanceOfHTMLCanvasElement(resource) {
	return typeof HTMLCanvasElement !== 'undefined' && resource instanceof HTMLCanvasElement
}
function isInstanceOfImageBitmap(resource) {
	return typeof ImageBitmap !== 'undefined' && resource instanceof ImageBitmap
}
function prepareSizeAndPosition(originalSize, options) {
	let srcLeft = getOption(options, 'left', 0)
	let srcTop = getOption(options, 'top', 0)
	let srcWidth = getOption(options, 'width', originalSize.width)
	let srcHeight = getOption(options, 'height', originalSize.height)
	let destWidth = srcWidth
	let destHeight = srcHeight
	if (options.mode === 'precision') {
		return {
			srcLeft: srcLeft,
			srcTop: srcTop,
			srcWidth: srcWidth,
			srcHeight: srcHeight,
			destWidth: destWidth,
			destHeight: destHeight
		}
	}
	let factor
	if (srcWidth > srcHeight) {
		factor = srcWidth / srcHeight
		destWidth = MAX_SIZE
		destHeight = Math.round(destWidth / factor)
	}
	else {
		factor = srcHeight / srcWidth
		destHeight = MAX_SIZE
		destWidth = Math.round(destHeight / factor)
	}
	if (destWidth > srcWidth || destHeight > srcHeight ||
		destWidth < MIN_SIZE || destHeight < MIN_SIZE) {
		destWidth = srcWidth
		destHeight = srcHeight
	}
	return {
		srcLeft: srcLeft,
		srcTop: srcTop,
		srcWidth: srcWidth,
		srcHeight: srcHeight,
		destWidth: destWidth,
		destHeight: destHeight
	}
}
let isWebWorkers = typeof window === 'undefined'
function makeCanvas() {
	if (isWebWorkers) {
		return hasOffscreenCanvas ? new OffscreenCanvas(1, 1) : null
	}
	return document.createElement('canvas')
}

let ERROR_PREFIX = 'FastAverageColor: '
function getError(message) {
	return Error(ERROR_PREFIX + message)
}
function outputError(error, silent) {
	if (!silent) {
		console.error(error)
	}
}

let FastAverageColor = /** @class */ (function () {
	function FastAverageColor() {
		this.canvas = null
		this.ctx = null
	}
	/**

	 * Get asynchronously the average color from not loaded image.

	 */
	FastAverageColor.prototype.getColorAsync = function (resource, options) {
		if (!resource) {
			return Promise.reject(getError('call .getColorAsync() without resource.'))
		}
		if (typeof resource === 'string') {
			// Web workers
			if (typeof Image === 'undefined') {
				return Promise.reject(getError('resource as string is not supported in this environment'))
			}
			let img = new Image()
			img.crossOrigin = options && options.crossOrigin || ''
			img.src = resource
			return this.bindImageEvents(img, options)
		}
		else if (isInstanceOfHTMLImageElement(resource) && !resource.complete) {
			return this.bindImageEvents(resource, options)
		}
		else {
			let result = this.getColor(resource, options)
			return result.error ? Promise.reject(result.error) : Promise.resolve(result)
		}
	}
	/**

	 * Get the average color from images, videos and canvas.

	 */
	FastAverageColor.prototype.getColor = function (resource, options) {
		options = options || {}
		let defaultColor = getDefaultColor(options)
		if (!resource) {
			let error = getError('call .getColor(null) without resource')
			outputError(error, options.silent)
			return this.prepareResult(defaultColor, error)
		}
		let originalSize = getOriginalSize(resource)
		let size = prepareSizeAndPosition(originalSize, options)
		if (!size.srcWidth || !size.srcHeight || !size.destWidth || !size.destHeight) {
			let error = getError("incorrect sizes for resource \"".concat(getSrc(resource), "\""))
			outputError(error, options.silent)
			return this.prepareResult(defaultColor, error)
		}
		if (!this.canvas) {
			this.canvas = makeCanvas()
			if (!this.canvas) {
				let error = getError('OffscreenCanvas is not supported in this browser')
				outputError(error, options.silent)
				return this.prepareResult(defaultColor, error)
			}
		}
		if (!this.ctx) {
			this.ctx = this.canvas.getContext('2d', { willReadFrequently: true })
			if (!this.ctx) {
				let error = getError('Canvas Context 2D is not supported in this browser')
				outputError(error, options.silent)
				return this.prepareResult(defaultColor)
			}
			this.ctx.imageSmoothingEnabled = false
		}
		this.canvas.width = size.destWidth
		this.canvas.height = size.destHeight
		try {
			this.ctx.clearRect(0, 0, size.destWidth, size.destHeight)
			this.ctx.drawImage(resource, size.srcLeft, size.srcTop, size.srcWidth, size.srcHeight, 0, 0, size.destWidth, size.destHeight)
			let bitmapData = this.ctx.getImageData(0, 0, size.destWidth, size.destHeight).data
			return this.prepareResult(this.getColorFromArray4(bitmapData, options))
		}
		catch (originalError) {
			let error = getError("security error (CORS) for resource ".concat(getSrc(resource), ".\nDetails: https://developer.mozilla.org/en/docs/Web/HTML/CORS_enabled_image"))
			outputError(error, options.silent)
			!options.silent && console.error(originalError)
			return this.prepareResult(defaultColor, error)
		}
	}
	/**

	 * Get the average color from a array when 1 pixel is 4 bytes.

	 */
	FastAverageColor.prototype.getColorFromArray4 = function (arr, options) {
		options = options || {}
		let bytesPerPixel = 4
		let arrLength = arr.length
		let defaultColor = getDefaultColor(options)
		if (arrLength < bytesPerPixel) {
			return defaultColor
		}
		let len = arrLength - arrLength % bytesPerPixel
		let step = (options.step || 1) * bytesPerPixel
		let algorithm
		switch (options.algorithm || 'sqrt') {
			case 'simple':
				algorithm = simpleAlgorithm
				break
			case 'sqrt':
				algorithm = sqrtAlgorithm
				break
			case 'dominant':
				algorithm = dominantAlgorithm
				break
			default:
				throw getError("".concat(options.algorithm, " is unknown algorithm"))
		}
		return algorithm(arr, len, {
			defaultColor: defaultColor,
			ignoredColor: prepareIgnoredColor(options.ignoredColor),
			step: step
		})
	}
	/**

	 * Get color data from value ([r, g, b, a]).

	 */
	FastAverageColor.prototype.prepareResult = function (value, error) {
		let rgb = value.slice(0, 3)
		let rgba = [value[0], value[1], value[2], value[3] / 255]
		let isDarkColor = isDark(value)
		return {
			value: [value[0], value[1], value[2], value[3]],
			rgb: 'rgb(' + rgb.join(',') + ')',
			rgba: 'rgba(' + rgba.join(',') + ')',
			hex: arrayToHex(rgb),
			hexa: arrayToHex(value),
			isDark: isDarkColor,
			isLight: !isDarkColor,
			error: error,
		}
	}
	/**

	 * Destroy the instance.

	 */
	FastAverageColor.prototype.destroy = function () {
		if (this.canvas) {
			this.canvas.width = 1
			this.canvas.height = 1
			this.canvas = null
		}
		this.ctx = null
	}
	FastAverageColor.prototype.bindImageEvents = function (resource, options) {
		let _this = this
		return new Promise(function (resolve, reject) {
			let onload = function () {
				unbindEvents()
				let result = _this.getColor(resource, options)
				if (result.error) {
					reject(result.error)
				}
				else {
					resolve(result)
				}
			}
			let onerror = function () {
				unbindEvents()
				reject(getError("Error loading image \"".concat(resource.src, "\".")))
			}
			let onabort = function () {
				unbindEvents()
				reject(getError("Image \"".concat(resource.src, "\" loading aborted")))
			}
			let unbindEvents = function () {
				resource.removeEventListener('load', onload)
				resource.removeEventListener('error', onerror)
				resource.removeEventListener('abort', onabort)
			}
			resource.addEventListener('load', onload)
			resource.addEventListener('error', onerror)
			resource.addEventListener('abort', onabort)
		})
	}
	return FastAverageColor
}())

export default FastAverageColor