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)

199 lines 5.18 kB
import { ArrayUtils } from '../common/array-utils.js'; import { Float16 } from '../common/float16.js'; import { Channel } from './channel.js'; import { ColorUtils } from './color-utils.js'; import { Format } from './format.js'; export class ColorFloat16 { get format() { return Format.float16; } get length() { return this.data.length; } get maxChannelValue() { return 1; } get maxIndexValue() { return 1; } 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.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.data = new Uint16Array(data); } else { this.data = data.slice(); } } static from(other) { const c = new ColorFloat16(other.length); c.data = other.data; return c; } static fromArray(color) { const c = new ColorFloat16(color); const l = color.length; for (let i = 0; i < l; ++i) { c.data[i] = Float16.doubleToFloat16(color[i]); } return c; } static rgb(r, g, b) { const data = new Uint16Array([ Float16.doubleToFloat16(r), Float16.doubleToFloat16(g), Float16.doubleToFloat16(b), ]); return new ColorFloat16(data); } static rgba(r, g, b, a) { const data = new Uint16Array([ Float16.doubleToFloat16(r), Float16.doubleToFloat16(g), Float16.doubleToFloat16(b), Float16.doubleToFloat16(a), ]); return new ColorFloat16(data); } getChannel(channel) { if (channel === Channel.luminance) { return this.luminance; } else { return channel < this.data.length ? Float16.float16ToDouble(this.data[channel]) : 0; } } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { if (index < this.data.length) { this.data[index] = Float16.doubleToFloat16(value); } } set(c) { this.setRgba(c.r, c.g, c.b, c.a); } setRgb(r, g, b) { this.data[0] = Float16.doubleToFloat16(r); const nc = this.data.length; if (nc > 1) { this.data[1] = Float16.doubleToFloat16(g); if (nc > 2) { this.data[2] = Float16.doubleToFloat16(b); } } } setRgba(r, g, b, a) { this.data[0] = Float16.doubleToFloat16(r); const nc = this.data.length; if (nc > 1) { this.data[1] = Float16.doubleToFloat16(g); if (nc > 2) { this.data[2] = Float16.doubleToFloat16(b); if (nc > 3) { this.data[3] = Float16.doubleToFloat16(a); } } } } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } clone() { return ColorFloat16.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-float16.js.map