Skip to content

Instantly share code, notes, and snippets.

@Draco18s
Last active May 26, 2023 17:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Draco18s/df3a75fbf965556c528061b4b014a49b to your computer and use it in GitHub Desktop.
Save Draco18s/df3a75fbf965556c528061b4b014a49b to your computer and use it in GitHub Desktop.
/**
All Ansi color code constants that are valid in Bitburner
nano functions/color.js
*/
export const Color = {
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
lightgray: "\x1b[37m",
background_black: "\x1b[40m",
background_red: "\x1b[41m",
background_green: "\x1b[42m",
background_yellow: "\x1b[43m",
background_blue: "\x1b[44m",
background_magenta: "\x1b[45m",
background_cyan: "\x1b[46m",
background_lightgray: "\x1b[47m",
reset: "\x1b[0m",
bold: "\x1b[1m",
underline: "\x1b[4m",
Combine(a, b) {
var a1 = a.substring(a.indexOf('[')+1,a.length-1);
var b1 = b.substring(b.indexOf('[')+1,b.length-1);
return `\x1b[${a1};${b1}m`;
},
Extended(color_id) {
return `\x1b[38;5;${color_id%256}m`;
},
BGExtended(color_id) {
return `\x1b[48;5;${color_id%256}m`;
},
AsRGB(r, g, b) {
if (r === g && g === b) {
if (r < 8) {
return 16;
}
if (r > 248) {
return 231;
}
return Math.round(((r - 8) / 247) * 24) + 232;
}
var ansi = 16
+ (36 * Math.round(r / 255 * 5))
+ (6 * Math.round(g / 255 * 5))
+ Math.round(b / 255 * 5);
return `\x1b[38;5;${ansi}m`;
},
BackgroundRGB(r, g, b) {
if (r === g && g === b) {
if (r < 8) {
return 16;
}
if (r > 248) {
return 231;
}
return Math.round(((r - 8) / 247) * 24) + 232;
}
var ansi = 16
+ (36 * Math.round(r / 255 * 5))
+ (6 * Math.round(g / 255 * 5))
+ Math.round(b / 255 * 5);
return `\x1b[48;5;${ansi}m`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment