node-libpng
Version:
Unofficial bindings for node to libpng.
83 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToRGBA = exports.isColorRGBA = exports.colorRGBA = void 0;
const gray_scale_alpha_1 = require("./gray-scale-alpha");
const gray_scale_1 = require("./gray-scale");
const rgb_1 = require("./rgb");
const palette_1 = require("./palette");
/**
* Create a new color of type `number`.
*
* @param r The value for the `red` part of the color.
* @param g The value for the `green` part of the color.
* @param b The value for the `blue` part of the color.
* @param a The value for the alpha channel part of the color.
*
* @return The color in rgba representation.
*/
function colorRGBA(r, g, b, a) {
const color = [r, g, b, a];
Object.defineProperty(color, "r", { get() { return this[0]; } });
Object.defineProperty(color, "g", { get() { return this[1]; } });
Object.defineProperty(color, "b", { get() { return this[2]; } });
Object.defineProperty(color, "a", { get() { return this[3]; } });
return color;
}
exports.colorRGBA = colorRGBA;
/**
* Checks if the given parameter is a color of type `ColorRGBA`.
*
* @param color The input to check.
*
* @return `true` if `color` was of type `ColorRGBA` and `false` otherwise.
*/
function isColorRGBA(color) {
if (typeof color !== "object" || !Array.isArray(color)) {
return false;
}
if (color.length !== 4) {
return false;
}
if (typeof color.r !== "number") {
return false;
}
if (typeof color.g !== "number") {
return false;
}
if (typeof color.b !== "number") {
return false;
}
if (typeof color.a !== "number") {
return false;
}
return true;
}
exports.isColorRGBA = isColorRGBA;
/**
* Convert a color in any format to rgba format.
*
* @param color The color to convert.
* @param palette The palette to use if the color is in palette format. Will be ignored otherwise.
*
* @return The color converted to rgba format or `undefined` if the color couldn't be converted.
*/
function convertToRGBA(color, palette) {
if (isColorRGBA(color)) {
return color;
}
if (rgb_1.isColorRGB(color)) {
return rgb_1.convertRGBToRGBA(color);
}
if (gray_scale_1.isColorGrayScale(color)) {
return gray_scale_1.convertGrayScaleToRGBA(color);
}
if (gray_scale_alpha_1.isColorGrayScaleAlpha(color)) {
return gray_scale_alpha_1.convertGrayScaleAlphaToRGBA(color);
}
if (palette_1.isColorPalette(color)) {
return palette_1.convertPaletteToRGBA(color, palette);
}
return;
}
exports.convertToRGBA = convertToRGBA;
//# sourceMappingURL=rgba.js.map