@pixi/utils
Version:
Collection of utilities used by PixiJS
78 lines (73 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var constants = require('@pixi/constants');
function mapPremultipliedBlendModes() {
const pm = [];
const npm = [];
for (let i = 0; i < 32; i++) {
pm[i] = i;
npm[i] = i;
}
pm[constants.BLEND_MODES.NORMAL_NPM] = constants.BLEND_MODES.NORMAL;
pm[constants.BLEND_MODES.ADD_NPM] = constants.BLEND_MODES.ADD;
pm[constants.BLEND_MODES.SCREEN_NPM] = constants.BLEND_MODES.SCREEN;
npm[constants.BLEND_MODES.NORMAL] = constants.BLEND_MODES.NORMAL_NPM;
npm[constants.BLEND_MODES.ADD] = constants.BLEND_MODES.ADD_NPM;
npm[constants.BLEND_MODES.SCREEN] = constants.BLEND_MODES.SCREEN_NPM;
const array = [];
array.push(npm);
array.push(pm);
return array;
}
const premultiplyBlendMode = mapPremultipliedBlendModes();
function correctBlendMode(blendMode, premultiplied) {
return premultiplyBlendMode[premultiplied ? 1 : 0][blendMode];
}
function premultiplyRgba(rgb, alpha, out, premultiply) {
out = out || new Float32Array(4);
if (premultiply || premultiply === void 0) {
out[0] = rgb[0] * alpha;
out[1] = rgb[1] * alpha;
out[2] = rgb[2] * alpha;
} else {
out[0] = rgb[0];
out[1] = rgb[1];
out[2] = rgb[2];
}
out[3] = alpha;
return out;
}
function premultiplyTint(tint, alpha) {
if (alpha === 1) {
return (alpha * 255 << 24) + tint;
}
if (alpha === 0) {
return 0;
}
let R = tint >> 16 & 255;
let G = tint >> 8 & 255;
let B = tint & 255;
R = R * alpha + 0.5 | 0;
G = G * alpha + 0.5 | 0;
B = B * alpha + 0.5 | 0;
return (alpha * 255 << 24) + (R << 16) + (G << 8) + B;
}
function premultiplyTintToRgba(tint, alpha, out, premultiply) {
out = out || new Float32Array(4);
out[0] = (tint >> 16 & 255) / 255;
out[1] = (tint >> 8 & 255) / 255;
out[2] = (tint & 255) / 255;
if (premultiply || premultiply === void 0) {
out[0] *= alpha;
out[1] *= alpha;
out[2] *= alpha;
}
out[3] = alpha;
return out;
}
exports.correctBlendMode = correctBlendMode;
exports.premultiplyBlendMode = premultiplyBlendMode;
exports.premultiplyRgba = premultiplyRgba;
exports.premultiplyTint = premultiplyTint;
exports.premultiplyTintToRgba = premultiplyTintToRgba;
//# sourceMappingURL=premultiply.js.map