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)
175 lines • 4.26 kB
JavaScript
import { ArrayUtils } from '../common/array-utils.js';
import { ColorUtils } from './color-utils.js';
import { Format } from './format.js';
export class ColorUint1 {
get format() {
return Format.uint1;
}
get length() {
return this._length;
}
get maxChannelValue() {
return 1;
}
get maxIndexValue() {
return 1;
}
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 ColorUint1(other._length);
c._data = other._data;
return c;
}
static fromArray(color) {
return new ColorUint1(color);
}
static rgb(r, g, b) {
return new ColorUint1([r, g, b]);
}
static rgba(r, g, b, a) {
return new ColorUint1([r, g, b, a]);
}
getChannel(channel) {
return channel < this.length ? (this._data >>> (7 - channel)) & 0x1 : 0;
}
getChannelNormalized(channel) {
return this.getChannel(channel) / this.maxChannelValue;
}
setChannel(index, value) {
let _index = index;
if (_index >= this.length) {
return;
}
_index = 7 - _index;
let v = this._data;
if (value !== 0) {
v |= 1 << _index;
}
else {
v &= ~((1 << _index) & 0xff);
}
this._data = v;
}
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 ColorUint1.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-uint1.js.map