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)

177 lines 4.44 kB
import { ArrayUtils } from '../common/array-utils.js'; import { ColorUtils } from './color-utils.js'; import { Format } from './format.js'; export class ColorUint2 { get format() { return Format.uint2; } get length() { return this._length; } get maxChannelValue() { return 3; } get maxIndexValue() { return 3; } 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 = 0; } else { this._length = data.length; this._data = 0; this.setRgba(data.length > 0 ? data[0] : 0, data.length > 1 ? data[1] : 0, data.length > 2 ? data[2] : 0, data.length > 3 ? data[3] : 0); } } static from(other) { const c = new ColorUint2(other._length); c._data = other._data; return c; } static fromArray(color) { return new ColorUint2(color); } static rgb(r, g, b) { return new ColorUint2([r, g, b]); } static rgba(r, g, b, a) { return new ColorUint2([r, g, b, a]); } getChannel(channel) { return channel < this.length ? (this._data >>> (6 - (channel << 1))) & 0x3 : 0; } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { const _index = index; if (_index >= this.length) { return; } const _mask = [ ~(0x3 << (6 - (0 << 1))) & 0xff, ~(0x3 << (6 - (1 << 1))) & 0xff, ~(0x3 << (6 - (2 << 1))) & 0xff, ~(0x3 << (6 - (3 << 1))) & 0xff, ]; const mask = _mask[index]; const x = Math.trunc(value) & 0x3; this._data = (this._data & mask) | (x << (6 - (index << 1))); } 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 ColorUint2.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-uint2.js.map