Python library to clorize text
Coloring is an other python library used to colorize texts in terminal using ANSI escape with a pythonic API.
Many excellent coloring library for python already exist like colorama. I wanted to create an other library with a more pythonic API that I could directly use without wrapping the existing functions.
I also wanted to add some features like
Install directly with pip
pip3 install coloring
Install locally
git clone https://github.com/nazime/coloring
cd coloring
pip3 install .
Coloring have many convenience functions meant to be used directly and easily. The library have +400 RGB colors parsed from X11 colors, +100 of those colors have convenience functions.
print_\
from coloring import *
text = "Hello, World!"
print_green(text)
print_red(text)
print_slate_blue(text)
print_chocolate(text)
print_green(text, 123, False, sep=",", end="")
print_b\
from coloring import *
text = "Hello, World!"
print_bgreen(text)
print_bred(text)
print_bslate_blue(text)
print_bchocolate(text)
print_bgreen(text, 123, False, sep=",", end="")
print_\: functions with the same signature as the built-in print function, with a specific style.
from coloring import *
text = "Hello, World!"
print_bold(text)
print_dim(text)
print_underline(text)
print_double_underline(text)
print_italic(text)
print_cross(text)
print_blink(text)
print_bold(text, 123, False, sep=",", end="")
\
from coloring import *
print("Hello,", green("World"), "!")
print("Hello,", red("World"), "!")
print("Hello,", slate_blue("World"), "!")
print("Hello,", chocolate("World"), "!")
b\
from coloring import *
print("Hello,", bgreen("World"), "!")
print("Hello,", bred("World"), "!")
print("Hello,", bslate_blue("World"), "!")
print("Hello,", bchocolate("World"), "!")
\: function that return a formatted string with the style \
from coloring import *
print(
"Simple ",
bold("bold "),
dim("dim"),
underline("underline "),
double_underline("double_underline "),
italic("italic "),
cross("cross "),
blink(" blink"),
)
There is also three convenience functions that may be useful when scripting.
from coloring import *
print_success("Flag found!")
print_failure("Flag not found!")
print_info("Script processing!")
cprint(*args, c: Color = None, bg: Color, s: str = “”, sep: str = “,”, end: str = “\n”, flush: bool = False, file=sys.stdout): cprint is the general function to print colorized text with a background and styles, the function support the same signature as the built-in print function. Color can be a string or a tuple containing RGB colors. See Styles syntax for styles.
from coloring import cprint
text = "Hello, World!"
cprint(text)
cprint(text, c="red")
cprint(text, c=(255, 0, 0))
cprint(text, s="b") # bold
cprint(text, bg="yellow") # background
cprint(text, c="red", bg="yellow", s="bcu")
rgbprint(red: int, green: int, blue: int, *args, bg: Color, s: str = “”, sep: str = “,”, end: str = “\n”, flush: bool = False, file=sys.stdout): Same as cprint function with a different syntax, take three RGB values of the color as parameter.
from coloring import rgbprint
text = "Hello, World!"
rgbprint(255, 0, 0, text)
rgbprint(255, 0, 0, text, bg="yellow")
rgbprint(255, 0, 0, text, 123, True, bg="yellow", s="bcu")
colorize(text: str, c: Color = None, bg: Color, s: str = “”): colorize is the general function to return a colorized string with a background and styles. Color can be a string or a tuple containing RGB colors. See Styles syntax for styles.
from coloring import colorize
text = "Hello, World!"
print("Hello,", colorize("World"), "!")
print("Hello,", colorize("World", c="red"), "!")
print("Hello,", colorize("World", c=(255, 0, 0)), "!")
print("Hello,", colorize("World", s="b"), "!")
print("Hello,", colorize("World", bg="yellow"), "!")
print("Hello,", colorize("World", c="red", bg="yellow", s="bcu"), "!")
It is possible to remove graphics (color, background and styles) from a string by removing the ANSI escapes.
All of the following functions take a string as parameter and return a string.
from coloring import *
text = (
"Simple "
+ green("color ")
+ colorize("background ", bg="yellow")
+ bold("bold ")
+ dim("dim ")
+ underline("underline ")
+ double_underline("double_underline ")
+ italic("italic ")
+ cross("cross ")
+ blink(" blink")
)
print(text)
print(rmgraphics(text))
print(rmcolor(text))
print(rmbackground(text))
print(rmstyle(text))
print(rmbold_and_dim(text))
print(rmunderline(text))
print(rmitalic(text))
print(rmcross(text))
print(rmblink(text))
It is possible to list all available colors with the list variable COLOR_NAMES or see their corresponding RGB values with dict variable COLORS.
from pprint import pprint
from coloring import COLOR_NAMES, COLORS, demo_colors
print("Number of colors (with redundancy):", len(COLORS))
print("10 first color names", COLOR_NAMES[:10])
# print first 10 colors with their RGB values
pprint({k: COLORS[k] for k in COLOR_NAMES[:10]})
The function demo_colors()
will print all available colors to see how it looks in your terminal.
from coloring import demo_colors
demo_colors()
It is possible to create convenience functions like green and print_green for a specific color, background and styling. It is interesting when you use the same format several times, it is also slightly optimized since it don’t have to parse the parameters each time.
create_print(color_or_red: Union[int, Color] = None, green: int = None, blue: int = None, *, bg: Color, s: str = “”, name: str = None): return a convenience function like color_green.
For the foreground color, it can be called with a string, a tuple or three int representing the RGB colors.
The parameter name can be used to change the name of the function (__name__ )
from coloring import create_print
myprint = create_print("red", bg="yellow", s="bU")
myprint("Hello, World!")
myprint = create_print(0, 255, 0, s="c")
myprint("Hello, World!")
create_color(color_or_red: Union[int, Color] = None, green: int = None, blue: int = None, *, bg: Color, s: str = “”, name: str = None): create color has the same signature as create_print
and return a function to colorize like the function green
.
from coloring import create_color
mycolor = create_color("red", bg="yellow", s="bU")
print("Hello,", mycolor("World"), "!")
mycolor = create_color(0, 255, 0, s="c")
print("Hello,", mycolor("World"), "!")
Most of the functions accept styles as a single string where each character represent a single style, the characters can be in any order. Most of the styles are represented by their first letter, except when the letter is already taken.
Example buc
mean bold, underlined and crossed.
echo Hello | coloring red
coloring --help
coloring red Hello | coloring rmgraphics
The coloring library support new version of windows by activating Console Virtual Terminal Sequences.
Contributions are highly welcomed and appreciated, you can contribute by doing one of the following: