ColorMask = {
function fromArray([r, g, b, a]) {
return ( r << 24 | g << 16 | b << 8 | a ) >>> 0;
}
function toArray(number) {
const r = (number & 0xff000000) >>> 24;
const g = (number & 0x00ff0000) >>> 16;
const b = (number & 0x0000ff00) >>> 8;
const a = (number & 0x000000ff);
return [r, g, b, a];
}
function fromObject(rgba) {
if (!rgba) return 0;
return fromArray([rgba.r, rgba.g, rgba.b, rgba.a ?? 255]);
}
function toObject(number) {
const [r, g, b, a] = toArray(number);
return { r, g, b, a };
}
function fromHex(hexColor) {
hexColor = hexColor.replace(/^[#x]|0x/i, "");
if (hexColor.length == 3) {
hexColor += "f";
}
if (hexColor.length == 4) {
hexColor = [...hexColor].map(c => c + c).join("")
return Number(`0x${hexColor}`);
}
if (hexColor.length == 6) {
hexColor += "ff";
}
if (hexColor.length == 8) {
return Number(`0x${hexColor}`);
}
throw "wyd"
}
function toHex(number) {
const [r, g, b, a] = toArray(number);
const values = (a == 255) ? [r, g, b] : [r, g, b, a]
return "#" + values.map(c => c.toString(16).padStart(2, "0")).join("");
}
return {
fromArray, toArray,
fromObject, toObject,
fromHex, toHex
};
}