Package: coloring
ColoringColoring is an other python library used to colorize texts in terminal using ANSI escape with a pythonic API.Why an other coloring library?Manyexcellentcoloring library for python already exist likecolorama. I wanted to create an other library with a morepythonic APIthat I coulddirectly usewithout wrapping the existing functions.I also wanted to add some features likeSupport 24 bits colors (RGB colors) for foreground and backgroundSupport different styles (bold, dim, italic, cross, underline, double_underline, blink)Functions to remove colors, backgrounds and styles from strings (remove ANSI escape)Many conveniences functions, so I can directly use the libraryThe colorized prints functions respect the same signature as the built-in print functionUsable through CLI (mainly to remove ANSI escapes)InstallInstall directly with pippip3installcoloringInstall locallygitclonehttps://github.com/nazime/coloringcdcoloring
pip3install.DemoUsageConvenience functionsColoring have many convenience functions meant to be used directly and easily. The library have +400 RGB colors parsed fromX11 colors, +100 of those colors have convenience functions.print_<colorname>print_<colorname>(*args,  sep: str = ",", end: str = "\n", flush: bool = False, file=sys.stdout): functions with the same signature as the built-in print function, with a specific color.fromcoloringimport*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<colorname>print_b<colorname>(*args,  sep: str = ",", end: str = "\n", flush: bool = False, file=sys.stdout): functions with the same signature as the built-in print function, with a specific colorin bold style.fromcoloringimport*text="Hello, World!"print_bgreen(text)print_bred(text)print_bslate_blue(text)print_bchocolate(text)print_bgreen(text,123,False,sep=",",end="")print_<style>print_<style>(*args,  sep: str = ",", end: str = "\n", flush: bool = False, file=sys.stdout): functions with the same signature as the built-in print function, with a specific style.fromcoloringimport*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="")<colorname><colorname>(string: str) -> str: function that return a colorized string with the color <colorname>.fromcoloringimport*print("Hello,",green("World"),"!")print("Hello,",red("World"),"!")print("Hello,",slate_blue("World"),"!")print("Hello,",chocolate("World"),"!")b <colorname>b<colorname>(string: str) -> str: function that return a colorized string with the color <colorname> in bold style.fromcoloringimport*print("Hello,",bgreen("World"),"!")print("Hello,",bred("World"),"!")print("Hello,",bslate_blue("World"),"!")print("Hello,",bchocolate("World"),"!")<style><style>(string: str) -> str: function that return a formatted string with the style <style>.fromcoloringimport*print("Simple ",bold("bold "),dim("dim"),underline("underline "),double_underline("double_underline "),italic("italic "),cross("cross "),blink(" blink"),)Scripting functionsThere is also three convenience functions that may be useful when scripting.fromcoloringimport*print_success("Flag found!")print_failure("Flag not found!")print_info("Script processing!")General functionscprintcprint(*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](#Styles syntax) for styles.fromcoloringimportcprinttext="Hello, World!"cprint(text)cprint(text,c="red")cprint(text,c=(255,0,0))cprint(text,s="b")# boldcprint(text,bg="yellow")# backgroundcprint(text,c="red",bg="yellow",s="bcu")rgbprintrgbprint(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.fromcoloringimportrgbprinttext="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")colorizecolorize(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](#Styles syntax) for styles.fromcoloringimportcolorizetext="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"),"!")Remove graphicsIt 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.rmgraphics:Remove all graphics (color, background and style).rmcolor:Remove all foreground colors.rmbackground:Remove all background colors.rmstyle:Remove all styles (bold, dim, underline, double_underline, italic, cross, blink).rmbold_and_dim:Remove bold and dim styles. (Due to how ANSI escape work, it is not possible to remove only bold style).rmunderline:Remove underline and double underline styles.rmitalic:Remove italic style.rmcross:Remove cross tyle.rmblink:Remove blink style.fromcoloringimport*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))Available colorsIt is possible to list all available colors with the list variableCOLOR_NAMESor see their corresponding RGB values with dict variableCOLORS.frompprintimportpprintfromcoloringimportCOLOR_NAMES,COLORS,demo_colorsprint("Number of colors (with redundancy):",len(COLORS))print("10 first color names",COLOR_NAMES[:10])# print first 10 colors with their RGB valuespprint({k:COLORS[k]forkinCOLOR_NAMES[:10]})The functiondemo_colors()will print all available colors to see how it looks in your terminal.fromcoloringimportdemo_colorsdemo_colors()Create convenience functionsIt 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_printcreate_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__ )fromcoloringimportcreate_printmyprint=create_print("red",bg="yellow",s="bU")myprint("Hello, World!")myprint=create_print(0,255,0,s="c")myprint("Hello, World!")create_colorcreate_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 ascreate_printand return a function to colorize like the functiongreen.fromcoloringimportcreate_colormycolor=create_color("red",bg="yellow",s="bU")print("Hello,",mycolor("World"),"!")mycolor=create_color(0,255,0,s="c")print("Hello,",mycolor("World"),"!")Styles syntaxMost 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.b: boldd: dimc: crossk: blinku: underlineU: double underlinei: italicExamplebucmean bold, underlined and crossed.CLIechoHello|coloringred
coloring--help
coloringredHello|coloringrmgraphicsSupport WindowsThe coloring library support new version of windows by activatingConsole Virtual Terminal Sequences.ContributeContributions are highly welcomed and appreciated, you can contribute by doing one of the following:Resolve issues in theTODOlistResolve TODO and FIXME in the source codeWriting tests (the project use pytest)Reports/fix bugsImplement/propose new features (propose a feature before implementing it), test the feature when you implement it.Give feedbackImprove/Write the documentationTODO[EASY] Support hex colors (in function normalize_color)[EASY] Fix/improve/test/document gradient function[MEDIUM] Support gradient function with more than two colors