UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

92 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RgbaColorPacker = void 0; const e_rgba_masks_js_1 = require("./e-rgba-masks.js"); /** * @public * Utility for packing and unpacking RGBA into int32 with one byte per channel. */ class RgbaColorPacker { /** * @param color - Bit packed color. * @returns a value between 0 - 255 representing the red component. */ static unpackR(color) { return (color & e_rgba_masks_js_1.ERgbaMasks.R) >>> e_rgba_masks_js_1.ERgbaShift.R; } /** * @param color - Bit packed color. * @returns a value between 0 - 255 representing the green component. */ static unpackG(color) { return (color & e_rgba_masks_js_1.ERgbaMasks.G) >>> e_rgba_masks_js_1.ERgbaShift.G; } /** * @param color - Bit packed color. * @returns a value between 0 - 255 representing the blue component. */ static unpackB(color) { return (color & e_rgba_masks_js_1.ERgbaMasks.B) >>> e_rgba_masks_js_1.ERgbaShift.B; } /** * @param color - Bit packed color. * @returns a value between 0 - 255 representing the alpha component. */ static unpackA(color) { return (color & e_rgba_masks_js_1.ERgbaMasks.A) >>> e_rgba_masks_js_1.ERgbaShift.A; } /** * * @param r - Red 0 - 255. * @param g - Green 0 - 255. * @param b - Blue 0 - 255. * @param a - Alpha 0 - 255. */ static packColor(r, g, b, a) { return (r << e_rgba_masks_js_1.ERgbaShift.R) | (g << e_rgba_masks_js_1.ERgbaShift.G) | (b << e_rgba_masks_js_1.ERgbaShift.B) | (a << e_rgba_masks_js_1.ERgbaShift.A); } /** * Given a packed color, produce a dom color string like `rgba(255, 255, 255, 1)`. */ static makeDomColorString(color) { const tmp = RgbaColorPacker.colorStringTmp; tmp[1] = (color & e_rgba_masks_js_1.ERgbaMasks.R) >>> e_rgba_masks_js_1.ERgbaShift.R; tmp[3] = (color & e_rgba_masks_js_1.ERgbaMasks.G) >>> e_rgba_masks_js_1.ERgbaShift.G; tmp[5] = (color & e_rgba_masks_js_1.ERgbaMasks.B) >>> e_rgba_masks_js_1.ERgbaShift.B; tmp[7] = ((color & e_rgba_masks_js_1.ERgbaMasks.A) >>> e_rgba_masks_js_1.ERgbaShift.A) * 0.00392156862745098; // divide by 255 return tmp.join(""); } /** * Given a packed color, produce a dom color string like `#FF0000`. * * @remarks * Made up of only 6 components, the alpha channel will be masked out. */ static getHexColorString(value) { if (value !== value) { return "NaN"; } // mask off the alpha channel value = value & (~e_rgba_masks_js_1.ERgbaMasks.A); // switch from ABGR to RGB value = ((value & 0xFF) << 16) | (value & 0xFF00) | ((value & 0xFF0000) >> 16); RgbaColorPacker.hexColorTmp[1] = value .toString(16) .toUpperCase() .padStart(6, "0"); return RgbaColorPacker.hexColorTmp.join(""); } static generateRandomPackedRGBA() { return ((Math.random() * 255) << e_rgba_masks_js_1.ERgbaShift.R) | ((Math.random() * 255) << e_rgba_masks_js_1.ERgbaShift.G) | ((Math.random() * 255) << e_rgba_masks_js_1.ERgbaShift.B) | ((Math.random() * 255) << e_rgba_masks_js_1.ERgbaShift.A); } } exports.RgbaColorPacker = RgbaColorPacker; RgbaColorPacker.colorStringTmp = ["rgba(", 1, ",", 3, ",", 5, ",", 7, ")"]; RgbaColorPacker.hexColorTmp = ["#", 0]; //# sourceMappingURL=rgba-color-packer.js.map