isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
57 lines (56 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hexToColor = hexToColor;
exports.hexToKColor = hexToKColor;
const log_1 = require("./log");
const HEX_STRING_LENGTH = 6;
/**
* Converts a hex string like "#33aa33" to a KColor object.
*
* @param hexString A hex string like "#ffffff" or "ffffff". (The "#" character is optional.)
* @param alpha Optional. Range is from 0 to 1. Default is 1. (The same as the `Color` constructor.)
*/
function hexToColor(hexString, alpha = 1) {
const { r, g, b } = hexToRGB(hexString);
// Color values should be between 0 and 1.
const base = 255;
return Color(r / base, g / base, b / base, alpha);
}
/**
* Converts a hex string like "#33aa33" to a Color object.
*
* @param hexString A hex string like "#ffffff" or "ffffff". (The "#" character is optional.)
* @param alpha Range is from 0 to 1. Default is 1.
*/
function hexToKColor(hexString, alpha = 1) {
const { r, g, b } = hexToRGB(hexString);
// KColor values should be between 0 and 1.
const base = 255;
return KColor(r / base, g / base, b / base, alpha);
}
function hexToRGB(hexString) {
hexString = hexString.replace("#", "");
if (hexString.length !== HEX_STRING_LENGTH) {
(0, log_1.logError)(`Hex strings must be of length: ${HEX_STRING_LENGTH}`);
return { r: 0, g: 0, b: 0 };
}
const rString = hexString.slice(0, 2);
const r = tonumber(`0x${rString}`);
if (r === undefined) {
(0, log_1.logError)(`Failed to convert \`0x${rString}\` to a number.`);
return { r: 0, g: 0, b: 0 };
}
const gString = hexString.slice(2, 4);
const g = tonumber(`0x${gString}`);
if (g === undefined) {
(0, log_1.logError)(`Failed to convert \`0x${gString}\` to a number.`);
return { r: 0, g: 0, b: 0 };
}
const bString = hexString.slice(4, 6);
const b = tonumber(`0x${bString}`);
if (b === undefined) {
(0, log_1.logError)(`Failed to convert \`0x${bString}\` to a number.`);
return { r: 0, g: 0, b: 0 };
}
return { r, g, b };
}