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)
182 lines • 4.44 kB
JavaScript
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 ColorFloat32 {
get format() {
return Format.float32;
}
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 Float32Array(data);
}
else {
this.data = data.slice();
}
}
static from(other) {
const c = new ColorFloat32(other.length);
c.data = other.data;
return c;
}
static fromArray(color) {
return new ColorFloat32(color);
}
static rgb(r, g, b) {
const data = new Float32Array([r, g, b]);
return new ColorFloat32(data);
}
static rgba(r, g, b, a) {
const data = new Float32Array([r, g, b, a]);
return new ColorFloat32(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] = value;
}
}
set(c) {
this.setRgba(c.r, c.g, c.b, c.a);
}
setRgb(r, g, b) {
this.data[0] = r;
const nc = this.data.length;
if (nc > 1) {
this.data[1] = g;
if (nc > 2) {
this.data[2] = b;
}
}
}
setRgba(r, g, b, a) {
this.data[0] = r;
const nc = this.data.length;
if (nc > 1) {
this.data[1] = g;
if (nc > 2) {
this.data[2] = b;
if (nc > 3) {
this.data[3] = a;
}
}
}
}
toArray() {
return ArrayUtils.generate(this.length, (i) => this.getChannel(i));
}
clone() {
return ColorFloat32.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-float32.js.map