Skip to content

Instantly share code, notes, and snippets.

@PROPHESSOR
Last active March 15, 2019 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PROPHESSOR/99fc4c0a169c9ee82eb750e199cdda00 to your computer and use it in GitHub Desktop.
Save PROPHESSOR/99fc4c0a169c9ee82eb750e199cdda00 to your computer and use it in GitHub Desktop.
ColorNinja is a single file easy to use linux terminal colorful library for many programming languages. It support 16 colors *nix terminals.
// Color Ninja v1.0.0 for C
// Copyright (c) PROPHESSOR 2019
// Compilation: gcc -g colorninja.cpp -o colorninja
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAIN // Comment this if you're using this file as library
#define uint8_t unsigned // Comment this if you'd included the library like stdint.h
const uint8_t ColorNinja_BLACK = 0;
const uint8_t ColorNinja_RED = 1;
const uint8_t ColorNinja_GREEN = 2;
const uint8_t ColorNinja_YELLOW = 3;
const uint8_t ColorNinja_BROWN = 3;
const uint8_t ColorNinja_BLUE = 4;
const uint8_t ColorNinja_MAGENTA = 5;
const uint8_t ColorNinja_CYAN = 6;
const uint8_t ColorNinja_WHITE = 7;
const uint8_t ColorNinja_UNKNOWN = 9;
#define ColorNinja_NORMAL 0, 0
const uint8_t ColorNinja_LIGHT = 1 << 0;
const uint8_t ColorNinja_BOLD = 1 << 1;
const uint8_t ColorNinja_ITALIC = 1 << 2;
uint8_t ColorNinja_private_flags = 0;
uint8_t ColorNinja_private_bg = 9;
void ColorNinja_SetDefault(uint8_t flags, uint8_t bg) {
ColorNinja_private_flags = flags;
ColorNinja_private_bg = bg;
}
char * ColorNinja_private_getColor(uint8_t color, uint8_t flags, uint8_t bg) {
const char *ESC = "\033[";
char *output = malloc(sizeof(char) * (flags & ColorNinja_BOLD ? 4 : 0
+ flags & ColorNinja_ITALIC ? 4 : 0
+ bg != 9 ? 4 : 0
+ 4)); // FIXME: Memory leak
if (flags & ColorNinja_BOLD) sprintf(output, "%s%s1m", output, ESC);
if (flags & ColorNinja_ITALIC) sprintf(output, "%s%s4m", output, ESC);
if (bg != 9) sprintf(output, "%s%s%d;4%dm", output, ESC, flags & ColorNinja_LIGHT ? 1 : 2, bg);
sprintf(output, "%s%s%d;3%dm", output, ESC, flags & ColorNinja_LIGHT ? 1 : 2, color);
return output;
};
char * ColorNinja_private_generate(char *text, uint8_t color, uint8_t flags, uint8_t bg) {
flags |= ColorNinja_private_flags;
char *getcolor = ColorNinja_private_getColor(color, flags, bg);
char *output = malloc(sizeof(char) * (strlen(getcolor) + strlen(text) + 4)); // FIXME: Memory leak
sprintf(output, "%s%s\033[0m", getcolor, text);
return output;
}
char * ColorNinja_color(char *text, uint8_t color, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, color, flags, bg);
}
char * ColorNinja_background(char *text, uint8_t bg, uint8_t flags) {
return ColorNinja_private_generate(text, 9, flags, bg);
}
char * ColorNinja_black(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_BLACK, flags, bg);
}
char * ColorNinja_red(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_RED, flags, bg);
}
char * ColorNinja_green(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_GREEN, flags, bg);
}
char * ColorNinja_yellow(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_YELLOW, flags, bg);
}
char * ColorNinja_blue(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_BLUE, flags, bg);
}
char * ColorNinja_magenta(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_MAGENTA, flags, bg);
}
char * ColorNinja_cyan(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_CYAN, flags, bg);
}
char * ColorNinja_white(char *text, uint8_t flags, uint8_t bg) {
return ColorNinja_private_generate(text, ColorNinja_WHITE, flags, bg);
}
#ifdef MAIN
int main(int argc, char *argv[]) {
ColorNinja_SetDefault(ColorNinja_LIGHT | ColorNinja_BOLD, 0); // Optional
printf("%s%s%s%s%s%s%s%s%s\n", ColorNinja_red("C", ColorNinja_NORMAL), ColorNinja_green("o", ColorNinja_NORMAL), ColorNinja_yellow("l", ColorNinja_NORMAL), ColorNinja_blue("o", ColorNinja_NORMAL), ColorNinja_magenta("r ", ColorNinja_NORMAL), ColorNinja_cyan("N", ColorNinja_NORMAL), ColorNinja_white("inja ", ColorNinja_NORMAL), ColorNinja_yellow("v1.0.0 ", ColorNinja_NORMAL), ColorNinja_white("for C", ColorNinja_NORMAL));
printf("Copyright (c) PROPHESSOR 2019\n");
ColorNinja_SetDefault(ColorNinja_NORMAL); // Restore defaults
printf("%s\n", ColorNinja_red("red", ColorNinja_NORMAL));
printf("%s\n", ColorNinja_red("light red", ColorNinja_LIGHT, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_red("bold red", ColorNinja_BOLD, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_red("light bold red", ColorNinja_LIGHT | ColorNinja_BOLD, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_red("italic red", ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_red("light italic red", ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_red("italic bold red", ColorNinja_LIGHT | ColorNinja_BOLD, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_red("light italic bold red", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_black("light italic bold black", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_green("light italic bold green", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_yellow("light italic bold yellow", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_blue("light italic bold blue", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_magenta("light italic bold magenta", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_cyan("light italic bold cyan", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_white("light italic bold white", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN));
printf("%s\n", ColorNinja_cyan("light italic bold cyan letters on magenta background", ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_MAGENTA));
printf("%s\n", ColorNinja_cyan("dark italic bold cyan letters on magenta background", ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_MAGENTA));
for (uint8_t i = 0; i < 8; i++) {
char tmp[] = "Test custom light color #";
tmp[24] = '0' + i;
printf("%s\n", (ColorNinja_color(tmp, i, ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC, ColorNinja_UNKNOWN)));
}
for (uint8_t i = 0; i < 8; i++) {
char tmp[] = "Test custom dark background #";
tmp[28] = '0' + i;
printf("%s\n", (ColorNinja_background(tmp, i, ColorNinja_LIGHT | ColorNinja_BOLD | ColorNinja_ITALIC)));
}
}
#endif
// Color Ninja v1.0.0 for C++
// Copyright (c) PROPHESSOR 2019
// Compilation: g++ -g colorninja.cpp -o colorninja -std=c++11
#include <iostream>
#include <string>
#define MAIN // Comment this if you're using this file as library
#define uint8_t unsigned // Comment this if you'd included the library like stdint.h
class ColorNinja {
public:
const uint8_t BLACK = 0;
const uint8_t RED = 1;
const uint8_t GREEN = 2;
const uint8_t YELLOW = 3;
const uint8_t BROWN = 3;
const uint8_t BLUE = 4;
const uint8_t MAGENTA = 5;
const uint8_t CYAN = 6;
const uint8_t WHITE = 7;
const uint8_t UNKNOWN = 9;
bool light = false;
bool bold = false;
bool italic = false;
uint8_t bg = 9;
ColorNinja(bool _light=false, bool _bold=false, bool _italic=false, uint8_t _bg=9)
: light(_light), bold(_bold), italic(_italic), bg(_bg) {
}
inline ~ColorNinja() {}
std::string color(std::string text, uint8_t color, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, color, light, bold, italic, bg);
}
std::string background(std::string text, uint8_t bg, uint8_t light=2, uint8_t bold=2, uint8_t italic=2) {
return generate(text, 9, light, bold, italic, bg);
}
std::string black(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, BLACK, light, bold, italic, bg);
}
std::string red(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, RED, light, bold, italic, bg);
}
std::string green(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, GREEN, light, bold, italic, bg);
}
std::string yellow(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, YELLOW, light, bold, italic, bg);
}
std::string blue(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, BLUE, light, bold, italic, bg);
}
std::string magenta(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, MAGENTA, light, bold, italic, bg);
}
std::string cyan(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, CYAN, light, bold, italic, bg);
}
std::string white(std::string text, uint8_t light=2, uint8_t bold=2, uint8_t italic=2, uint8_t bg=9) {
return generate(text, WHITE, light, bold, italic, bg);
}
private:
std::string generate(std::string text, uint8_t color, uint8_t light, uint8_t bold, uint8_t italic, uint8_t bg) {
if (light > 1) light = (int)this->light;
if (bold > 1) bold = (int)this->bold;
if (italic > 1) italic = (int)this->italic;
if (bg == 9) bg = (int)this->bg;
return getColor(color, light, bold, italic, bg) + text + "\033[0m";
}
std::string getColor(uint8_t color, bool light, bool bold, bool italic, uint8_t bg) {
const char *ESC = "\033[";
std::string output = "";
if (bold == 1) {
output += ESC;
output += "1m";
}
if (italic == 1) {
output += ESC;
output += "4m";
}
if (bg != 9) {
output += ESC;
output += light ? '1' : '2';
output += ";4";
output += '0' + bg;
output += "m";
}
output += ESC;
output += light ? '1' : '2';
output += ";3";
output += '0' + color;
output += "m";
return output;
}
};
#ifdef MAIN
int main(int argc, char *argv[]) {
{
ColorNinja c(true, true, false, 0);
std::cout << c.red("C") << c.green("o") << c.yellow("l") << c.blue("o") << c.magenta("r ") << c.cyan("N") << c.white("inja ") << c.yellow("v1.0.0 ") << c.white("for C++", false) << std::endl;
std::cout << "Copyright (c) PROPHESSOR 2019" << std::endl;
}
ColorNinja c;
std::cout << c.red("red") << std::endl;
std::cout << c.red("light red", true) << std::endl;
std::cout << c.red("bold red", false, true) << std::endl;
std::cout << c.red("light bold red", true, true) << std::endl;
std::cout << c.red("italic red", false, false, true) << std::endl;
std::cout << c.red("light italic red", true, false, true) << std::endl;
std::cout << c.red("italic bold red", false, true, true) << std::endl;
std::cout << c.red("light italic bold red", true, true, true) << std::endl;
std::cout << c.black("light italic bold black", true, true, true) << std::endl;
std::cout << c.green("light italic bold green", true, true, true) << std::endl;
std::cout << c.yellow("light italic bold yellow", true, true, true) << std::endl;
std::cout << c.blue("light italic bold blue", true, true, true) << std::endl;
std::cout << c.magenta("light italic bold magenta", true, true, true) << std::endl;
std::cout << c.cyan("light italic bold cyan", true, true, true) << std::endl;
std::cout << c.white("light italic bold white", true, true, true) << std::endl;
std::cout << c.cyan("light italic bold cyan letters on magenta background", true, true, true, c.MAGENTA) << std::endl;
std::cout << c.cyan("dark italic bold cyan letters on magenta background", false, true, true, c.MAGENTA) << std::endl;
for (uint8_t i = 0; i < 8; i++) {
std::string tmp = "Test custom light color ";
tmp += '0' + i;
std::cout << (c.color(tmp, i, true)) << std::endl;
}
for (uint8_t i = 0; i < 8; i++) {
std::string tmp = "Test custom dark background ";
tmp += '0' + i;
std::cout << (c.background(tmp, i, false)) << std::endl;
}
}
#endif
// Color Ninja v1.0.0 for Java
// Copyright (c) PROPHESSOR 2019
// Compilation: javac colorninja.java; java ColorNinja
class ColorNinja {
public final int BLACK = 0;
public final int RED = 1;
public final int GREEN = 2;
public final int YELLOW = 3;
public final int BROWN = YELLOW;
public final int BLUE = 4;
public final int MAGENTA = 5;
public final int CYAN = 6;
public final int WHITE = 7;
public final int UNKNOWN = 9;
public final int NORMAL = 0;
public final int LIGHT = 1 << 0;
public final int BOLD = 1 << 1;
public final int ITALIC = 1 << 2;
private int flags = 0;
private int bg = UNKNOWN;
public ColorNinja() {}
public ColorNinja(int flags) {
this.flags = flags;
}
public ColorNinja(int flags, int bg) {
this.flags = flags;
this.bg = bg;
}
static void test() {
ColorNinja c = new ColorNinja();
System.out.println(c.red("red"));
System.out.println(c.red("light red", c.LIGHT));
System.out.println(c.red("bold red", c.BOLD));
System.out.println(c.red("light bold red", c.LIGHT | c.BOLD));
System.out.println(c.red("italic red", c.ITALIC));
System.out.println(c.red("light italic red", c.LIGHT | c.ITALIC));
System.out.println(c.red("italic bold red", c.BOLD | c.ITALIC));
System.out.println(c.red("light italic bold red", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.red("light italic bold red", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.black("light italic bold black", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.green("light italic bold green", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.yellow("light italic bold yellow", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.blue("light italic bold blue", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.magenta("light italic bold magenta", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.cyan("light italic bold cyan", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.white("light italic bold white", c.LIGHT | c.BOLD | c.ITALIC));
System.out.println(c.cyan("light italic bold cyan letters on magenta background", c.LIGHT | c.BOLD | c.ITALIC, c.MAGENTA));
System.out.println(c.cyan("dark italic bold cyan letters on magenta background", c.BOLD | c.ITALIC, c.MAGENTA));
for (int i = 0; i < 8; i++) System.out.println(c.color("Test custom light color " + i, i, c.LIGHT, c.UNKNOWN));
for (int i = 0; i < 8; i++) System.out.println(c.background("Test custom dark background " + i, i, c.NORMAL));
}
public static void main(String[] args) {
ColorNinja c = new ColorNinja();
c.setFlags(c.LIGHT | c.BOLD);
System.out.println(c.red("C") + c.green("o") + c.yellow("l") + c.blue("o") + c.magenta("r ") + c.cyan("N") + c.white("inja ") + c.yellow("v1.0.0 ") + c.white("for Java"));
System.out.println("Copyright (c) PROPHESSOR 2019");
ColorNinja.test();
}
public void setFlags(int flags) {
this.flags = flags;
}
public void setBg(int bg) {
this.bg = bg;
}
public String color(String text, int color, int flags, int bg) {
return generate(text, color, flags, bg);
}
public String background(String text, int bg, int flags) {
return generate(text, 9, flags, bg);
}
public String black(String text, int flags, int bg) {
return generate(text, BLACK, flags, bg);
}
public String black(String text, int flags) {
return black(text, flags, this.bg);
}
public String black(String text) {
return black(text, this.flags, this.bg);
}
public String red(String text, int flags, int bg) {
return generate(text, RED, flags, bg);
}
public String red(String text, int flags) {
return red(text, flags, this.bg);
}
public String red(String text) {
return red(text, this.flags, this.bg);
}
public String green(String text, int flags, int bg) {
return generate(text, GREEN, flags, bg);
}
public String green(String text, int flags) {
return green(text, flags, this.bg);
}
public String green(String text) {
return green(text, this.flags, this.bg);
}
public String yellow(String text, int flags, int bg) {
return generate(text, YELLOW, flags, bg);
}
public String yellow(String text, int flags) {
return yellow(text, flags, this.bg);
}
public String yellow(String text) {
return yellow(text, this.flags, this.bg);
}
public String blue(String text, int flags, int bg) {
return generate(text, BLUE, flags, bg);
}
public String blue(String text, int flags) {
return blue(text, flags, this.bg);
}
public String blue(String text) {
return blue(text, this.flags, this.bg);
}
public String magenta(String text, int flags, int bg) {
return generate(text, MAGENTA, flags, bg);
}
public String magenta(String text, int flags) {
return magenta(text, flags, this.bg);
}
public String magenta(String text) {
return magenta(text, this.flags, this.bg);
}
public String cyan(String text, int flags, int bg) {
return generate(text, CYAN, flags, bg);
}
public String cyan(String text, int flags) {
return cyan(text, flags, this.bg);
}
public String cyan(String text) {
return cyan(text, this.flags, this.bg);
}
public String white(String text, int flags, int bg) {
return generate(text, WHITE, flags, bg);
}
public String white(String text, int flags) {
return white(text, flags, this.bg);
}
public String white(String text) {
return white(text, this.flags, this.bg);
}
private String generate(String text, int color, int flags, int bg) {
return getColor(color, flags, bg) + text + "\u001b[0m";
}
private String getColor(int color, int flags, int bg) {
String ESC = "\u001b[";
String output = "";
if ((flags & BOLD) != 0) output += ESC + "1m";
if ((flags & ITALIC) != 0) output += ESC + "4m";
if (bg != UNKNOWN) output += ESC + (((flags & LIGHT) != 0) ? '1' : '2') + ";4" + bg + "m";
output += ESC + (((flags & LIGHT) != 0) ? '1' : '2') + ";3" + color + "m";
return output;
}
};
// usr/bin/node
// Color Ninja v1.0.0 for JavaScript (Node.JS)
// Copyright (c) PROPHESSOR 2019
const ColorNinja_Symbols = {
'generate': Symbol('generate'),
'getColor': Symbol('getColor')
};
class ColorNinja {
constructor({light=false, bold=false, italic=false, bg=0} = {}) {
[this.light, this.bold, this.italic, this.bg] = [light, bold, italic, bg];
this.BLACK = 0;
this.RED = 1;
this.GREEN = 2;
this.YELLOW = 3;
this.BROWN = this.YELLOW;
this.BLUE = 4;
this.MAGENTA = 5;
this.CYAN = 6;
this.WHITE = 7;
}
static test() {
const c = new ColorNinja();
console.log(c.red("red"));
console.log(c.red("light red", {light: true}));
console.log(c.red("bold red", {bold: true}));
console.log(c.red("light bold red", {light: true, bold: true}));
console.log(c.red("italic red", {italic: true}));
console.log(c.red("light italic red", {italic: true, light: true}));
console.log(c.red("italic bold red", {italic: true, bold: true}));
console.log(c.red("light italic bold red", {light: true, bold: true, italic: true}));
console.log(c.red("light italic bold red", {light: true, bold: true, italic: true}));
console.log(c.black("light italic bold black", {light: true, bold: true, italic: true}));
console.log(c.green("light italic bold green", {light: true, bold: true, italic: true}));
console.log(c.yellow("light italic bold yellow", {light: true, bold: true, italic: true}));
console.log(c.blue("light italic bold blue", {light: true, bold: true, italic: true}));
console.log(c.magenta("light italic bold magenta", {light: true, bold: true, italic: true}));
console.log(c.cyan("light italic bold cyan", {light: true, bold: true, italic: true}));
console.log(c.white("light italic bold white", {light: true, bold: true, italic: true}));
console.log(c.cyan("light italic bold cyan letters on magenta background", {light: true, bold: true, italic: true, bg: c.MAGENTA}));
console.log(c.cyan("dark italic bold cyan letters on magenta background", {light: false, bold: true, italic: true, bg: c.MAGENTA}));
for (let i = 0; i < 8; i++) console.log(c.color(`Test custom light color ${i}`, i, {light: true}));
for (let i = 0; i < 8; i++) console.log(c.background(`Test custom dark background ${i}`, i, {light: false}));
}
color(text, color, args) {
return this[ColorNinja_Symbols.generate](text, color, args);
}
background(text, bg, args) {
args.bg = bg;
return this[ColorNinja_Symbols.generate](text, 9, args);
}
black(text, args) {
return this[ColorNinja_Symbols.generate](text, this.BLACK, args);
}
red(text, args) {
return this[ColorNinja_Symbols.generate](text, this.RED, args);
}
green(text, args) {
return this[ColorNinja_Symbols.generate](text, this.GREEN, args);
}
yellow(text, args) {
return this[ColorNinja_Symbols.generate](text, this.YELLOW, args);
}
blue(text, args) {
return this[ColorNinja_Symbols.generate](text, this.BLUE, args);
}
magenta(text, args) {
return this[ColorNinja_Symbols.generate](text, this.MAGENTA, args);
}
cyan(text, args) {
return this[ColorNinja_Symbols.generate](text, this.CYAN, args);
}
white(text, args) {
return this[ColorNinja_Symbols.generate](text, this.WHITE, args);
}
[ColorNinja_Symbols.generate](text, color, {light=this.light, bold=this.bold, italic=this.italic, bg=this.bg} = {}) {
return `${this[ColorNinja_Symbols.getColor](color, light, bold, italic, bg)}${text}\x1b[0m`;
}
[ColorNinja_Symbols.getColor](color, light, bold, italic, bg) {
const ESC = '\x1b['
let output = ''
if (bold) output += `${ESC}1m`
if (italic) output += `${ESC}4m`
if (bg != null) output += `${ESC}${light ? 1 : 2};4${bg}m`
output += `${ESC}${light ? 1 : 2};3${color}m`;
return output
}
};
if (typeof require !== 'undefined' && !module.parent) {
const c = new ColorNinja({bg: 0, light: true, bold: true})
console.log(c.red('C') + c.green('o') + c.yellow('l') + c.blue('o') + c.magenta('r ') + c.cyan('N') + c.white('inja ') + c.yellow('v1.0.0 ') + c.white('for JavaScript (Node.JS)', {light: false}))
console.log('Copyright (c) PROPHESSOR 2019')
ColorNinja.test()
}
-- Color Ninja v1.0.0 for Lua
-- Copyright (c) PROPHESSOR 2019
ColorNinja = {}
function ColorNinja:new(light, bold, italic, bg)
local this = {}
this.light = light
this.bold = bold
this.italic = italic
this.bg = bg
this.BLACK = 0
this.RED = 1
this.GREEN = 2
this.YELLOW = 3
this.BROWN = this.YELLOW
this.BLUE = 4
this.MAGENTA = 5
this.CYAN = 6
this.WHITE = 7
function this:color(text, color, light, bold, italic, bg)
return self:__generate(text, color, light, bold, italic, bg)
end
function this:background(text, bg, light, bold, italic)
return self:__generate(text, 9, light, bold, italic, bg)
end
function this:black(text, light, bold, italic, bg)
return self:__generate(text, self.BLACK, light, bold, italic, bg)
end
function this:red(text, light, bold, italic, bg)
return self:__generate(text, self.RED, light, bold, italic, bg)
end
function this:green(text, light, bold, italic, bg)
return self:__generate(text, self.GREEN, light, bold, italic, bg)
end
function this:yellow(text, light, bold, italic, bg)
return self:__generate(text, self.YELLOW, light, bold, italic, bg)
end
function this:blue(text, light, bold, italic, bg)
return self:__generate(text, self.BLUE, light, bold, italic, bg)
end
function this:magenta(text, light, bold, italic, bg)
return self:__generate(text, self.MAGENTA, light, bold, italic, bg)
end
function this:cyan(text, light, bold, italic, bg)
return self:__generate(text, self.CYAN, light, bold, italic, bg)
end
function this:white(text, light, bold, italic, bg)
return self:__generate(text, self.WHITE, light, bold, italic, bg)
end
function this:__generate(text, color, light, bold, italic, bg)
light = light or self.light
bold = bold or self.bold
italic = italic or self.italic
return self:__getColor(color, light, bold, italic, bg) .. text .. '\x1b[0m'
end
function this:__getColor(color, light, bold, italic, bg)
ESC = '\x1b['
output = ''
if bold then
output = output .. ESC .. '1m'
end
if italic then
output = output .. ESC .. '4m'
end
if bg then
output = output .. ESC .. (light and 1 or 2) .. ';4' .. bg .. 'm'
end
output = output .. ESC .. (light and 1 or 2) .. ';3' .. color .. 'm'
return output
end
setmetatable(this, self)
self.__index = self
return this
end
function ColorNinja:test()
c = ColorNinja:new(true, true, false, 0)
print(c:red('C') .. c:green('o') .. c:yellow('l') .. c:blue('o') .. c:magenta('r ') .. c:cyan('N') .. c:white('inja ') .. c:yellow('v1.0.0 ') .. c:white('for Lua', False))
print('Copyright (c) PROPHESSOR 2019')
c = ColorNinja:new(false, false, false, 0)
print(c:red("red"))
print(c:red("light red", true))
print(c:red("light red", true))
print(c:red("bold red", false, true))
print(c:red("light bold red", true, true))
print(c:red("light bold red", true, true))
print(c:red("italic red", false, false, true))
print(c:red("light italic red", true, false, true))
print(c:red("italic bold red", false, true, true))
print(c:red("light italic bold red", true, true, true))
print(c:red("light italic bold red", true, true, true))
print(c:black("light italic bold black", true, true, true))
print(c:green("light italic bold green", true, true, true))
print(c:yellow("light italic bold yellow", true, true, true))
print(c:blue("light italic bold blue", true, true, true))
print(c:magenta("light italic bold magenta", true, true, true))
print(c:cyan("light italic bold cyan", true, true, true))
print(c:white("light italic bold white", true, true, true))
print(c:cyan("light italic bold cyan letters on magenta background", true, true, true, c.MAGENTA))
print(c:cyan("dark italic bold cyan letters on magenta background", false, true, true, c.MAGENTA))
for i = 0, 7 do
print(c:color('Test custom light color ' .. i, i, true))
end
for i = 0, 7 do
print(c:background('Test custom dark background ' .. i, i, false))
end
end
ColorNinja:test() -- Comment this if you don't want to start testing
#/usr/bin/python2
# Color Ninja v1.0.0 for Python 2
# Copyright (c) PROPHESSOR 2019
class ColorNinja():
def __init__(self, light=False, bold=False, italic=False, bg=0):
self.light, self.bold, self.italic, self.bg = light, bold, italic, bg
self.BLACK = 0
self.RED = 1
self.GREEN = 2
self.YELLOW = 3
self.BROWN = self.YELLOW
self.BLUE = 4
self.MAGENTA = 5
self.CYAN = 6
self.WHITE = 7
@classmethod
def test(self):
c = ColorNinja()
print c.red("red")
print c.red("light red", True)
print c.red("light red", light=True)
print c.red("bold red", bold=True)
print c.red("light bold red", light=True, bold=True)
print c.red("light bold red", True, True)
print c.red("italic red", italic=True)
print c.red("light italic red", italic=True, light=True)
print c.red("italic bold red", italic=True, bold=True)
print c.red("light italic bold red", light=True, bold=True, italic=True)
print c.red("light italic bold red", True, True, True)
print c.black("light italic bold black", True, True, True)
print c.green("light italic bold green", True, True, True)
print c.yellow("light italic bold yellow", True, True, True)
print c.blue("light italic bold blue", True, True, True)
print c.magenta("light italic bold magenta", True, True, True)
print c.cyan("light italic bold cyan", True, True, True)
print c.white("light italic bold white", True, True, True)
print c.cyan("light italic bold cyan letters on magenta background", light=True, bold=True, italic=True, bg=c.MAGENTA)
print c.cyan("dark italic bold cyan letters on magenta background", light=False, bold=True, italic=True, bg=c.MAGENTA)
for i in range(0, 8):
print c.color('Test custom light color %d' % i, i, light=True)
for i in range(0, 8):
print c.background('Test custom dark background %d' % i, i, light=False)
def color(self, text, color, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, color, **self.__processWithDefaultValues((light, bold, italic, bg)))
def background(self, text, bg, light=None, bold=None, italic=None):
return self.__generate(text, 9, **self.__processWithDefaultValues((light, bold, italic, bg)))
def black(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.BLACK, **self.__processWithDefaultValues((light, bold, italic, bg)))
def red(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.RED, **self.__processWithDefaultValues((light, bold, italic, bg)))
def green(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.GREEN, **self.__processWithDefaultValues((light, bold, italic, bg)))
def yellow(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.YELLOW, **self.__processWithDefaultValues((light, bold, italic, bg)))
def blue(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.BLUE, **self.__processWithDefaultValues((light, bold, italic, bg)))
def magenta(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.MAGENTA, **self.__processWithDefaultValues((light, bold, italic, bg)))
def cyan(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.CYAN, **self.__processWithDefaultValues((light, bold, italic, bg)))
def white(self, text, light=None, bold=None, italic=None, bg=None):
return self.__generate(text, self.WHITE, **self.__processWithDefaultValues((light, bold, italic, bg)))
def __processWithDefaultValues(self, values): # (light, bold, italic)
return {'light':self.light if values[0] == None else values[0], 'bold':self.bold if values[1] == None else values[1], 'italic':self.italic if values[2] == None else values[2], 'bg':self.bg if values[3] == None else values[3]}
def __generate(self, text, color, light, bold, italic, bg):
return '%s%s\033[0m' % (self.__getColor(color, light, bold, italic, bg), text)
def __getColor(self, color, light, bold, italic, bg):
ESC = '\033['
output = ''
if bold:
output += '%s1m' % ESC
if italic:
output += '%s4m' % ESC
if bg != None:
output += '%s%d;4%dm' % (ESC, 1 if light else 2, bg)
output += '%s%d;3%dm' % (ESC, 1 if light else 2, color)
return output
if __name__ == '__main__':
c = ColorNinja(bg=0, light=True, bold=True)
print c.red('C') + c.green('o') + c.yellow('l') + c.blue('o') + c.magenta('r ') + c.cyan('N') + c.white('inja ') + c.yellow('v1.0.0 ') + c.white('for Python 2', light=False)
print 'Copyright (c) PROPHESSOR 2019'
ColorNinja.test()
#/usr/bin/python
# Color Ninja v1.0.0 for Python 3
# Copyright (c) PROPHESSOR 2019
class ColorNinja():
def __init__(self, light:bool=False, bold:bool=False, italic:bool=False, bg:int=0):
self.light, self.bold, self.italic, self.bg = light, bold, italic, bg
self.BLACK = 0
self.RED = 1
self.GREEN = 2
self.YELLOW = 3
self.BROWN = self.YELLOW
self.BLUE = 4
self.MAGENTA = 5
self.CYAN = 6
self.WHITE = 7
@classmethod
def test(self):
c = ColorNinja()
print(c.red("red"))
print(c.red("light red", True))
print(c.red("light red", light=True))
print(c.red("bold red", bold=True))
print(c.red("light bold red", light=True, bold=True))
print(c.red("light bold red", True, True))
print(c.red("italic red", italic=True))
print(c.red("light italic red", italic=True, light=True))
print(c.red("italic bold red", italic=True, bold=True))
print(c.red("light italic bold red", light=True, bold=True, italic=True))
print(c.red("light italic bold red", True, True, True))
print(c.black("light italic bold black", True, True, True))
print(c.green("light italic bold green", True, True, True))
print(c.yellow("light italic bold yellow", True, True, True))
print(c.blue("light italic bold blue", True, True, True))
print(c.magenta("light italic bold magenta", True, True, True))
print(c.cyan("light italic bold cyan", True, True, True))
print(c.white("light italic bold white", True, True, True))
print(c.cyan("light italic bold cyan letters on magenta background", light=True, bold=True, italic=True, bg=c.MAGENTA))
print(c.cyan("dark italic bold cyan letters on magenta background", light=False, bold=True, italic=True, bg=c.MAGENTA))
for i in range(0, 8):
print(c.color('Test custom light color %d' % i, i, light=True))
for i in range(0, 8):
print(c.background('Test custom dark background %d' % i, i, light=False))
def color(self, text:str, color:int, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, color, **self.__processWithDefaultValues((light, bold, italic, bg)))
def background(self, text:str, bg:int, light:bool=None, bold:bool=None, italic:bool=None):
return self.__generate(text, 9, **self.__processWithDefaultValues((light, bold, italic, bg)))
def black(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.BLACK, **self.__processWithDefaultValues((light, bold, italic, bg)))
def red(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.RED, **self.__processWithDefaultValues((light, bold, italic, bg)))
def green(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.GREEN, **self.__processWithDefaultValues((light, bold, italic, bg)))
def yellow(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.YELLOW, **self.__processWithDefaultValues((light, bold, italic, bg)))
def blue(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.BLUE, **self.__processWithDefaultValues((light, bold, italic, bg)))
def magenta(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.MAGENTA, **self.__processWithDefaultValues((light, bold, italic, bg)))
def cyan(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.CYAN, **self.__processWithDefaultValues((light, bold, italic, bg)))
def white(self, text:str, light:bool=None, bold:bool=None, italic:bool=None, bg:int=None):
return self.__generate(text, self.WHITE, **self.__processWithDefaultValues((light, bold, italic, bg)))
def __processWithDefaultValues(self, values): # (light, bold, italic)
return {'light':self.light if values[0] == None else values[0], 'bold':self.bold if values[1] == None else values[1], 'italic':self.italic if values[2] == None else values[2], 'bg':self.bg if values[3] == None else values[3]}
def __generate(self, text:str, color:int, light:bool, bold:bool, italic:bool, bg:int):
return '%s%s\033[0m' % (self.__getColor(color, light, bold, italic, bg), text)
def __getColor(self, color:int, light:bool, bold:bool, italic:bool, bg:int):
ESC = '\033['
output = ''
if bold:
output += '%s1m' % ESC
if italic:
output += '%s4m' % ESC
if bg != None:
output += '%s%d;4%dm' % (ESC, 1 if light else 2, bg)
output += '%s%d;3%dm' % (ESC, 1 if light else 2, color)
return output
if __name__ == '__main__':
c = ColorNinja(bg=0, light=True, bold=True)
print(c.red('C') + c.green('o') + c.yellow('l') + c.blue('o') + c.magenta('r ') + c.cyan('N') + c.white('inja ') + c.yellow('v1.0.0 ') + c.white('for Python 3', light=False))
print('Copyright (c) PROPHESSOR 2019')
ColorNinja.test()
@PROPHESSOR
Copy link
Author

PROPHESSOR commented Mar 15, 2019

  • Python 2
  • Python 3
  • Node.JS
  • C++
  • C
  • Lua
  • Java
  • Perl
  • Ruby
  • Go
  • Pascal
  • BASIC
  • PHP
  • C#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment