UNPKG

image-in-browser

Version:

Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)

187 lines 4.78 kB
import { ArrayUtils } from '../common/array-utils.js'; import { MathUtils } from '../common/math-utils.js'; import { ColorUtils } from './color-utils.js'; import { Format } from './format.js'; export class ColorUint4 { get format() { return Format.uint4; } get length() { return this._length; } get maxChannelValue() { return 15; } get maxIndexValue() { return 15; } get isLdrFormat() { return true; } get isHdrFormat() { return false; } get hasPalette() { return false; } get palette() { return undefined; } get index() { return this.r; } set index(i) { this.r = i; } get r() { return this.getChannel(0); } set r(r) { this.setChannel(0, r); } get g() { return this.getChannel(1); } set g(g) { this.setChannel(1, g); } get b() { return this.getChannel(2); } set b(b) { this.setChannel(2, b); } get a() { return this.getChannel(3); } set a(a) { this.setChannel(3, a); } get rNormalized() { return this.r / this.maxChannelValue; } set rNormalized(v) { this.r = v * this.maxChannelValue; } get gNormalized() { return this.g / this.maxChannelValue; } set gNormalized(v) { this.g = v * this.maxChannelValue; } get bNormalized() { return this.b / this.maxChannelValue; } set bNormalized(v) { this.b = v * this.maxChannelValue; } get aNormalized() { return this.a / this.maxChannelValue; } set aNormalized(v) { this.a = v * this.maxChannelValue; } get luminance() { return ColorUtils.getLuminance(this); } get luminanceNormalized() { return ColorUtils.getLuminanceNormalized(this); } constructor(data) { if (typeof data === 'number') { this._length = data; this._data = new Uint8Array(this.length < 3 ? 1 : 2); } else { this._length = data.length; this._data = new Uint8Array(this._length < 3 ? 1 : 2); this.setRgba(this._length > 0 ? data[0] : 0, this._length > 1 ? data[1] : 0, this._length > 2 ? data[2] : 0, this._length > 3 ? data[3] : 0); } } static from(other) { const c = new ColorUint4(other._length); c._data = other._data; return c; } static fromArray(color) { return new ColorUint4(color); } static rgb(r, g, b) { const c = new ColorUint4(3); c.setRgb(r, g, b); return c; } static rgba(r, g, b, a) { const c = new ColorUint4(4); c.setRgba(r, g, b, a); return c; } getChannel(channel) { return channel < 0 || channel >= this.length ? 0 : channel < 2 ? (this._data[0] >>> (4 - (channel << 2))) & 0xf : (this._data[1] >>> (4 - ((channel & 0x1) << 2))) & 0xf; } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { let _index = index; if (_index >= this.length) { return; } const vi = MathUtils.clamp(Math.trunc(value), 0, 15); let i = 0; if (_index > 1) { _index &= 0x1; i = 1; } if (_index === 0) { this._data[i] = (this._data[i] & 0xf) | (vi << 4); } else if (_index === 1) { this._data[i] = (this._data[i] & 0xf0) | vi; } } set(c) { this.setRgba(c.r, c.g, c.b, c.a); } setRgb(r, g, b) { this.r = r; this.g = g; this.b = b; } setRgba(r, g, b, a) { this.r = r; this.g = g; this.b = b; this.a = a; } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } clone() { return ColorUint4.from(this); } equals(other) { if (other.length !== this.length) { return false; } for (let i = 0; i < this.length; i++) { if (other.getChannel(i) !== this.getChannel(i)) { return false; } } return true; } convert(opt) { return ColorUtils.convertColor({ from: this, format: opt === null || opt === void 0 ? void 0 : opt.format, numChannels: opt === null || opt === void 0 ? void 0 : opt.numChannels, alpha: opt === null || opt === void 0 ? void 0 : opt.alpha, }); } } //# sourceMappingURL=color-uint4.js.map