- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
import { BLEND_MODES } from '@pixi/constants';
/**
* Corrects PixiJS blend, takes premultiplied alpha into account
* @memberof PIXI.utils
* @function mapPremultipliedBlendModes
* @private
* @returns {Array<number[]>} Mapped modes.
*/
function mapPremultipliedBlendModes(): number[][]
{
const pm = [];
const npm = [];
for (let i = 0; i < 32; i++)
{
pm[i] = i;
npm[i] = i;
}
pm[BLEND_MODES.NORMAL_NPM] = BLEND_MODES.NORMAL;
pm[BLEND_MODES.ADD_NPM] = BLEND_MODES.ADD;
pm[BLEND_MODES.SCREEN_NPM] = BLEND_MODES.SCREEN;
npm[BLEND_MODES.NORMAL] = BLEND_MODES.NORMAL_NPM;
npm[BLEND_MODES.ADD] = BLEND_MODES.ADD_NPM;
npm[BLEND_MODES.SCREEN] = BLEND_MODES.SCREEN_NPM;
const array: number[][] = [];
array.push(npm);
array.push(pm);
return array;
}
/**
* maps premultiply flag and blendMode to adjusted blendMode
* @memberof PIXI.utils
* @constant premultiplyBlendMode
* @type {Array<number[]>}
*/
export const premultiplyBlendMode = mapPremultipliedBlendModes();
/**
* changes blendMode according to texture format
* @memberof PIXI.utils
* @function correctBlendMode
* @param {number} blendMode - supposed blend mode
* @param {boolean} premultiplied - whether source is premultiplied
* @returns {number} true blend mode for this texture
*/
export function correctBlendMode(blendMode: number, premultiplied: boolean): number
{
return premultiplyBlendMode[premultiplied ? 1 : 0][blendMode];
}
/**
* combines rgb and alpha to out array
* @memberof PIXI.utils
* @function premultiplyRgba
* @param {Float32Array|number[]} rgb - input rgb
* @param {number} alpha - alpha param
* @param {Float32Array} [out] - output
* @param {boolean} [premultiply=true] - do premultiply it
* @returns {Float32Array} vec4 rgba
*/
export function premultiplyRgba(
rgb: Float32Array | number[],
alpha: number,
out?: Float32Array,
premultiply?: boolean
): Float32Array
{
out = out || new Float32Array(4);
if (premultiply || premultiply === undefined)
{
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;
}
/**
* premultiplies tint
* @memberof PIXI.utils
* @function premultiplyTint
* @param {number} tint - integer RGB
* @param {number} alpha - floating point alpha (0.0-1.0)
* @returns {number} tint multiplied by alpha
*/
export function premultiplyTint(tint: number, alpha: number): number
{
if (alpha === 1.0)
{
return (alpha * 255 << 24) + tint;
}
if (alpha === 0.0)
{
return 0;
}
let R = ((tint >> 16) & 0xFF);
let G = ((tint >> 8) & 0xFF);
let B = (tint & 0xFF);
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;
}
/**
* converts integer tint and float alpha to vec4 form, premultiplies by default
* @memberof PIXI.utils
* @function premultiplyTintToRgba
* @param {number} tint - input tint
* @param {number} alpha - alpha param
* @param {Float32Array} [out] - output
* @param {boolean} [premultiply=true] - do premultiply it
* @returns {Float32Array} vec4 rgba
*/
export function premultiplyTintToRgba(tint: number, alpha: number, out: Float32Array, premultiply?: boolean): Float32Array
{
out = out || new Float32Array(4);
out[0] = ((tint >> 16) & 0xFF) / 255.0;
out[1] = ((tint >> 8) & 0xFF) / 255.0;
out[2] = (tint & 0xFF) / 255.0;
if (premultiply || premultiply === undefined)
{
out[0] *= alpha;
out[1] *= alpha;
out[2] *= alpha;
}
out[3] = alpha;
return out;
}