Zonies-Bot/functions/colorCodes.js
2025-05-12 15:20:40 -05:00

46 lines
1.5 KiB
JavaScript

/**
* ANSI color codes mapping for terminal text coloring
* @type {Object<string, string>}
*/
const ANSI_COLORS = {
'0': '\x1b[30m', // Black
'1': '\x1b[34m', // Dark Blue
'2': '\x1b[32m', // Dark Green
'3': '\x1b[36m', // Dark Aqua
'4': '\x1b[31m', // Dark Red
'5': '\x1b[35m', // Dark Purple
'6': '\x1b[33m', // Gold
'7': '\x1b[37m', // Gray
'8': '\x1b[90m', // Dark Gray
'9': '\x1b[94m', // Blue
'a': '\x1b[92m', // Green
'b': '\x1b[96m', // Aqua
'c': '\x1b[91m', // Red
'd': '\x1b[95m', // Light Purple
'e': '\x1b[93m', // Yellow
'f': '\x1b[97m', // White
'r': '\x1b[0m', // Reset
};
/**
* Translates color codes in text to ANSI color sequences
* @param {string} altColorChar - The character used to prefix color codes (e.g., '&' or '§')
* @param {string} textToTranslate - The text containing color codes to translate
* @returns {string} The text with color codes replaced by ANSI sequences
* @throws {Error} If altColorChar is not a single character
*/
function colors(altColorChar, textToTranslate) {
if (typeof altColorChar !== 'string' || altColorChar.length !== 1) {
throw new Error('altColorChar must be a single character');
}
if (typeof textToTranslate !== 'string') {
throw new Error('textToTranslate must be a string');
}
const regex = new RegExp(`${altColorChar}([0-9a-fr])`, 'g');
return textToTranslate.replace(regex, (_, code) => ANSI_COLORS[code] || '');
}
module.exports = colors;