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)

191 lines 4.92 kB
import { ArrayUtils } from '../common/array-utils.js'; import { Channel } from './channel.js'; import { ColorUtils } from './color-utils.js'; import { Format } from './format.js'; export class ColorInt32 { get format() { return Format.int32; } get length() { return this._data.length; } get maxChannelValue() { return 0x7fffffff; } get maxIndexValue() { return 0x7fffffff; } get isLdrFormat() { return false; } get isHdrFormat() { return true; } get hasPalette() { return false; } get palette() { return undefined; } get index() { return this.r; } set index(i) { this.r = i; } get r() { return this._data.length > 0 ? this._data[0] : 0; } set r(r) { if (this._data.length > 0) { this._data[0] = Math.trunc(r); } } get g() { return this._data.length > 1 ? this._data[1] : 0; } set g(g) { if (this._data.length > 1) { this._data[1] = Math.trunc(g); } } get b() { return this._data.length > 2 ? this._data[2] : 0; } set b(b) { if (this._data.length > 2) { this._data[2] = Math.trunc(b); } } get a() { return this._data.length > 3 ? this._data[3] : 0; } set a(a) { if (this._data.length > 3) { this._data[3] = Math.trunc(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._data = new Int32Array(data); } else { this._data = data.slice(); } } static from(other) { const c = new ColorInt32(other.length); c._data = other._data; return c; } static fromArray(color) { const data = new Int32Array(color); return new ColorInt32(data); } static rgb(r, g, b) { const data = new Int32Array([r, g, b]); return new ColorInt32(data); } static rgba(r, g, b, a) { const data = new Int32Array([r, g, b, a]); return new ColorInt32(data); } getChannel(channel) { if (channel === Channel.luminance) { return this.luminance; } else { return channel < this._data.length ? this._data[channel] : 0; } } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { if (index < this._data.length) { this._data[index] = Math.trunc(value); } } set(c) { this.setRgba(c.r, c.g, c.b, c.a); } setRgb(r, g, b) { this._data[0] = Math.trunc(r); const nc = this._data.length; if (nc > 1) { this._data[1] = Math.trunc(g); if (nc > 2) { this._data[2] = Math.trunc(b); } } } setRgba(r, g, b, a) { this._data[0] = Math.trunc(r); const nc = this._data.length; if (nc > 1) { this._data[1] = Math.trunc(g); if (nc > 2) { this._data[2] = Math.trunc(b); if (nc > 3) { this._data[3] = Math.trunc(a); } } } } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } clone() { return ColorInt32.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-int32.js.map