11cb0ef41Sopenharmony_ciconst ANSI_BACKGROUND_OFFSET = 10; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst styles = { 101cb0ef41Sopenharmony_ci modifier: { 111cb0ef41Sopenharmony_ci reset: [0, 0], 121cb0ef41Sopenharmony_ci // 21 isn't widely supported and 22 does the same thing 131cb0ef41Sopenharmony_ci bold: [1, 22], 141cb0ef41Sopenharmony_ci dim: [2, 22], 151cb0ef41Sopenharmony_ci italic: [3, 23], 161cb0ef41Sopenharmony_ci underline: [4, 24], 171cb0ef41Sopenharmony_ci overline: [53, 55], 181cb0ef41Sopenharmony_ci inverse: [7, 27], 191cb0ef41Sopenharmony_ci hidden: [8, 28], 201cb0ef41Sopenharmony_ci strikethrough: [9, 29], 211cb0ef41Sopenharmony_ci }, 221cb0ef41Sopenharmony_ci color: { 231cb0ef41Sopenharmony_ci black: [30, 39], 241cb0ef41Sopenharmony_ci red: [31, 39], 251cb0ef41Sopenharmony_ci green: [32, 39], 261cb0ef41Sopenharmony_ci yellow: [33, 39], 271cb0ef41Sopenharmony_ci blue: [34, 39], 281cb0ef41Sopenharmony_ci magenta: [35, 39], 291cb0ef41Sopenharmony_ci cyan: [36, 39], 301cb0ef41Sopenharmony_ci white: [37, 39], 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci // Bright color 331cb0ef41Sopenharmony_ci blackBright: [90, 39], 341cb0ef41Sopenharmony_ci gray: [90, 39], // Alias of `blackBright` 351cb0ef41Sopenharmony_ci grey: [90, 39], // Alias of `blackBright` 361cb0ef41Sopenharmony_ci redBright: [91, 39], 371cb0ef41Sopenharmony_ci greenBright: [92, 39], 381cb0ef41Sopenharmony_ci yellowBright: [93, 39], 391cb0ef41Sopenharmony_ci blueBright: [94, 39], 401cb0ef41Sopenharmony_ci magentaBright: [95, 39], 411cb0ef41Sopenharmony_ci cyanBright: [96, 39], 421cb0ef41Sopenharmony_ci whiteBright: [97, 39], 431cb0ef41Sopenharmony_ci }, 441cb0ef41Sopenharmony_ci bgColor: { 451cb0ef41Sopenharmony_ci bgBlack: [40, 49], 461cb0ef41Sopenharmony_ci bgRed: [41, 49], 471cb0ef41Sopenharmony_ci bgGreen: [42, 49], 481cb0ef41Sopenharmony_ci bgYellow: [43, 49], 491cb0ef41Sopenharmony_ci bgBlue: [44, 49], 501cb0ef41Sopenharmony_ci bgMagenta: [45, 49], 511cb0ef41Sopenharmony_ci bgCyan: [46, 49], 521cb0ef41Sopenharmony_ci bgWhite: [47, 49], 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci // Bright color 551cb0ef41Sopenharmony_ci bgBlackBright: [100, 49], 561cb0ef41Sopenharmony_ci bgGray: [100, 49], // Alias of `bgBlackBright` 571cb0ef41Sopenharmony_ci bgGrey: [100, 49], // Alias of `bgBlackBright` 581cb0ef41Sopenharmony_ci bgRedBright: [101, 49], 591cb0ef41Sopenharmony_ci bgGreenBright: [102, 49], 601cb0ef41Sopenharmony_ci bgYellowBright: [103, 49], 611cb0ef41Sopenharmony_ci bgBlueBright: [104, 49], 621cb0ef41Sopenharmony_ci bgMagentaBright: [105, 49], 631cb0ef41Sopenharmony_ci bgCyanBright: [106, 49], 641cb0ef41Sopenharmony_ci bgWhiteBright: [107, 49], 651cb0ef41Sopenharmony_ci }, 661cb0ef41Sopenharmony_ci}; 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciexport const modifierNames = Object.keys(styles.modifier); 691cb0ef41Sopenharmony_ciexport const foregroundColorNames = Object.keys(styles.color); 701cb0ef41Sopenharmony_ciexport const backgroundColorNames = Object.keys(styles.bgColor); 711cb0ef41Sopenharmony_ciexport const colorNames = [...foregroundColorNames, ...backgroundColorNames]; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_cifunction assembleStyles() { 741cb0ef41Sopenharmony_ci const codes = new Map(); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci for (const [groupName, group] of Object.entries(styles)) { 771cb0ef41Sopenharmony_ci for (const [styleName, style] of Object.entries(group)) { 781cb0ef41Sopenharmony_ci styles[styleName] = { 791cb0ef41Sopenharmony_ci open: `\u001B[${style[0]}m`, 801cb0ef41Sopenharmony_ci close: `\u001B[${style[1]}m`, 811cb0ef41Sopenharmony_ci }; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci group[styleName] = styles[styleName]; 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci codes.set(style[0], style[1]); 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci Object.defineProperty(styles, groupName, { 891cb0ef41Sopenharmony_ci value: group, 901cb0ef41Sopenharmony_ci enumerable: false, 911cb0ef41Sopenharmony_ci }); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci Object.defineProperty(styles, 'codes', { 951cb0ef41Sopenharmony_ci value: codes, 961cb0ef41Sopenharmony_ci enumerable: false, 971cb0ef41Sopenharmony_ci }); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci styles.color.close = '\u001B[39m'; 1001cb0ef41Sopenharmony_ci styles.bgColor.close = '\u001B[49m'; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci styles.color.ansi = wrapAnsi16(); 1031cb0ef41Sopenharmony_ci styles.color.ansi256 = wrapAnsi256(); 1041cb0ef41Sopenharmony_ci styles.color.ansi16m = wrapAnsi16m(); 1051cb0ef41Sopenharmony_ci styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET); 1061cb0ef41Sopenharmony_ci styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET); 1071cb0ef41Sopenharmony_ci styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js 1101cb0ef41Sopenharmony_ci Object.defineProperties(styles, { 1111cb0ef41Sopenharmony_ci rgbToAnsi256: { 1121cb0ef41Sopenharmony_ci value: (red, green, blue) => { 1131cb0ef41Sopenharmony_ci // We use the extended greyscale palette here, with the exception of 1141cb0ef41Sopenharmony_ci // black and white. normal palette only has 4 greyscale shades. 1151cb0ef41Sopenharmony_ci if (red === green && green === blue) { 1161cb0ef41Sopenharmony_ci if (red < 8) { 1171cb0ef41Sopenharmony_ci return 16; 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci if (red > 248) { 1211cb0ef41Sopenharmony_ci return 231; 1221cb0ef41Sopenharmony_ci } 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci return Math.round(((red - 8) / 247) * 24) + 232; 1251cb0ef41Sopenharmony_ci } 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci return 16 1281cb0ef41Sopenharmony_ci + (36 * Math.round(red / 255 * 5)) 1291cb0ef41Sopenharmony_ci + (6 * Math.round(green / 255 * 5)) 1301cb0ef41Sopenharmony_ci + Math.round(blue / 255 * 5); 1311cb0ef41Sopenharmony_ci }, 1321cb0ef41Sopenharmony_ci enumerable: false, 1331cb0ef41Sopenharmony_ci }, 1341cb0ef41Sopenharmony_ci hexToRgb: { 1351cb0ef41Sopenharmony_ci value: hex => { 1361cb0ef41Sopenharmony_ci const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16)); 1371cb0ef41Sopenharmony_ci if (!matches) { 1381cb0ef41Sopenharmony_ci return [0, 0, 0]; 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci let [colorString] = matches; 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci if (colorString.length === 3) { 1441cb0ef41Sopenharmony_ci colorString = [...colorString].map(character => character + character).join(''); 1451cb0ef41Sopenharmony_ci } 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci const integer = Number.parseInt(colorString, 16); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci return [ 1501cb0ef41Sopenharmony_ci /* eslint-disable no-bitwise */ 1511cb0ef41Sopenharmony_ci (integer >> 16) & 0xFF, 1521cb0ef41Sopenharmony_ci (integer >> 8) & 0xFF, 1531cb0ef41Sopenharmony_ci integer & 0xFF, 1541cb0ef41Sopenharmony_ci /* eslint-enable no-bitwise */ 1551cb0ef41Sopenharmony_ci ]; 1561cb0ef41Sopenharmony_ci }, 1571cb0ef41Sopenharmony_ci enumerable: false, 1581cb0ef41Sopenharmony_ci }, 1591cb0ef41Sopenharmony_ci hexToAnsi256: { 1601cb0ef41Sopenharmony_ci value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)), 1611cb0ef41Sopenharmony_ci enumerable: false, 1621cb0ef41Sopenharmony_ci }, 1631cb0ef41Sopenharmony_ci ansi256ToAnsi: { 1641cb0ef41Sopenharmony_ci value: code => { 1651cb0ef41Sopenharmony_ci if (code < 8) { 1661cb0ef41Sopenharmony_ci return 30 + code; 1671cb0ef41Sopenharmony_ci } 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci if (code < 16) { 1701cb0ef41Sopenharmony_ci return 90 + (code - 8); 1711cb0ef41Sopenharmony_ci } 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci let red; 1741cb0ef41Sopenharmony_ci let green; 1751cb0ef41Sopenharmony_ci let blue; 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci if (code >= 232) { 1781cb0ef41Sopenharmony_ci red = (((code - 232) * 10) + 8) / 255; 1791cb0ef41Sopenharmony_ci green = red; 1801cb0ef41Sopenharmony_ci blue = red; 1811cb0ef41Sopenharmony_ci } else { 1821cb0ef41Sopenharmony_ci code -= 16; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci const remainder = code % 36; 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci red = Math.floor(code / 36) / 5; 1871cb0ef41Sopenharmony_ci green = Math.floor(remainder / 6) / 5; 1881cb0ef41Sopenharmony_ci blue = (remainder % 6) / 5; 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci const value = Math.max(red, green, blue) * 2; 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci if (value === 0) { 1941cb0ef41Sopenharmony_ci return 30; 1951cb0ef41Sopenharmony_ci } 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci // eslint-disable-next-line no-bitwise 1981cb0ef41Sopenharmony_ci let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red)); 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci if (value === 2) { 2011cb0ef41Sopenharmony_ci result += 60; 2021cb0ef41Sopenharmony_ci } 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ci return result; 2051cb0ef41Sopenharmony_ci }, 2061cb0ef41Sopenharmony_ci enumerable: false, 2071cb0ef41Sopenharmony_ci }, 2081cb0ef41Sopenharmony_ci rgbToAnsi: { 2091cb0ef41Sopenharmony_ci value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)), 2101cb0ef41Sopenharmony_ci enumerable: false, 2111cb0ef41Sopenharmony_ci }, 2121cb0ef41Sopenharmony_ci hexToAnsi: { 2131cb0ef41Sopenharmony_ci value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)), 2141cb0ef41Sopenharmony_ci enumerable: false, 2151cb0ef41Sopenharmony_ci }, 2161cb0ef41Sopenharmony_ci }); 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci return styles; 2191cb0ef41Sopenharmony_ci} 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ciconst ansiStyles = assembleStyles(); 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ciexport default ansiStyles; 224