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)
117 lines • 3.23 kB
JavaScript
import { Format } from '../color/format.js';
export class PaletteInt32 {
get data() {
return this._data;
}
get numColors() {
return this._numColors;
}
get numChannels() {
return this._numChannels;
}
get byteLength() {
return this._data.byteLength;
}
get buffer() {
return this._data.buffer;
}
get format() {
return Format.int32;
}
get maxChannelValue() {
return 0x7fffffff;
}
constructor(numColors, numChannels, data) {
this._numColors = numColors;
this._numChannels = numChannels;
this._data = data !== null && data !== void 0 ? data : new Int32Array(numColors * numChannels);
}
static from(other) {
return new PaletteInt32(other.numColors, other.numChannels, other.data);
}
setRgb(index, r, g, b) {
let _index = index;
_index *= this._numChannels;
this._data[_index] = Math.trunc(r);
if (this._numChannels > 1) {
this._data[_index + 1] = Math.trunc(g);
if (this._numChannels > 2) {
this._data[_index + 2] = Math.trunc(b);
}
}
}
setRgba(index, r, g, b, a) {
let _index = index;
_index *= this._numChannels;
this._data[_index] = Math.trunc(r);
if (this._numChannels > 1) {
this._data[_index + 1] = Math.trunc(g);
if (this._numChannels > 2) {
this._data[_index + 2] = Math.trunc(b);
if (this._numChannels > 3) {
this._data[_index + 3] = Math.trunc(a);
}
}
}
}
set(index, channel, value) {
let _index = index;
if (channel < this._numChannels) {
_index *= this._numChannels;
this._data[_index + channel] = Math.trunc(value);
}
}
get(index, channel) {
return channel < this._numChannels
? this._data[index * this._numChannels + channel]
: 0;
}
getRed(index) {
let _index = index;
_index *= this._numChannels;
return this._data[_index];
}
getGreen(index) {
let _index = index;
if (this._numChannels < 2) {
return 0;
}
_index *= this._numChannels;
return this._data[_index + 1];
}
getBlue(index) {
let _index = index;
if (this._numChannels < 3) {
return 0;
}
_index *= this._numChannels;
return this._data[_index + 2];
}
getAlpha(index) {
let _index = index;
if (this._numChannels < 4) {
return 0;
}
_index *= this._numChannels;
return this._data[_index + 3];
}
setRed(index, value) {
this.set(index, 0, value);
}
setGreen(index, value) {
this.set(index, 1, value);
}
setBlue(index, value) {
this.set(index, 2, value);
}
setAlpha(index, value) {
this.set(index, 3, value);
}
clone() {
return PaletteInt32.from(this);
}
toUint8Array() {
return new Uint8Array(this.buffer);
}
}
//# sourceMappingURL=palette-int32.js.map