Package: pycss
PycssPycss is a module that will help you to colorise your output in the terminal.
This module also allows you to do a variety of logic opertations on various color formats, such as RGB and Hex color codes.How does it work?Pycss uses ANSI color codes.
Read about theANSI codes.Note: This module may not work wih certain terminals as they may not support ANSI color codes.InstallationTo install thepycssmodule, use the following command:pip install pycssImportpycsswith the following code snippet:# Not pycssimportcssDocumentationBefore the documentation starts, you should be familiar with css colors.Inbuilt foreground colorsYou can access the css colors by refferring to the color variablescss.COLORNAME.Note:The css colors are 3-bit and 4-bit colors. If your terminal does not support 3-bit and 4-bit colors, it will not be able to understand them.VariableColorGREYGreyREDRedGREENGreenYELLOWYellowBLUEBlueMAGENTAMagentaCYANCyanWHITEWhiteNOCOLORTransparentC_BLACKBright BlackC_REDBright RedC_YELLOWBright YellowC_GREENBright GreenC_BLUEBright BlueC_MAGENTABright MagentaC_CYANBright CyanC_WHITEBright WhiteInbuilt background colorsYou can access the css background colors by refferring to the background color variablescss.BGCOLORNAME.Note:The css background colors are 3-bit and 4-bit colors. If your terminal does not support 3-bit and 4-bit colors, it will not be able to understand them.css.BACKROUND_COLORVariableColorBGGREYGreyBGREDRedBGGREENGreenBGYELLOWYellowBGBLUEBlueBGMAGENTAMagentaBGCYANCyanBGWHITEWhiteC_BGBLACKBright BlackC_BGREDBright RedC_BGYELLOWBright YellowC_BGGREENBright GreenC_BGBLUEBright BlueC_BGMAGENTABright MagentaC_BGCYANBright CyanC_BGWHITEBright WhiteUsing external foreground colorsIf you want to use a specific foreground color with your text output, you can
use thepycssinbiult functions to convert hex or RGB color codes to
usable ANSI color codes:hex2color()orrgb2color().Note:The ANSI color code that will be returned by the above functions is 24-bit. If your terminal does not support 24-bit colors, it will not be able to understand them.importcss# Hex codehex_code="#FFF"ansi_color_code=css.hex2color(hex_code)# RGB colorred,green,blue=255,255,255ansi_color_code=css.rgb2color(red,green,blue)# The ansi_color_code can also be used with the color() function.# Scroll down to learn more.Using external background colorsIf you want to use a specific background color with your text output, you can
use thepycssinbiult functions to convert hex or RGB color codes to
usable ANSI color codes:hex2bgcolor()orrgb2bgcolor().Note:The ANSI color code that will be returned by the above functions is 24-bit. If your terminal does not support 24-bit colors, it will not be able to understand them.importcss# Hex codehex_code="#FFF"ansi_color_code=css.hex2bgcolor(hex_code)# RGB colorred,green,blue=255,255,255ansi_color_code=css.rgb2bgcolor(red,green,blue)# The ansi_color_code can also be used with the bgcolor() function.# Scroll down to learn more.Coloring the textTo color text, you use thecolor()function.Parameterstext - The text you want colored.color - Pass in the color you desire. It can either be an inbuilt foreground color or an external foreground color.Returns:Returns a string with your colored text.importcss# Inbuilt color (3-bit and 4-bit color)colored_text=css.color("Hello World!",css.RED)print(colored_text)# External color (24-bit true color)hex_color="#d62828"ansi_color_code=css.hex2color(hex_color)colored_text=css.color("Hello World!",ansi_color_code)print(colored_text)Applying backgroundTo apply background to text, you use thebgcolor()function.Parameterstext - The text you want to be coloredbackground_color - Pass in the background color you desire. It can either be an inbuilt background color or an external background color.Note:You don't use normal strings for background colors eg: "red".USE pycss's background color!Returns:Returns a string with your background-applied text.importcss# Inbuilt color (3-bit and 4-bit color)colored_text=css.bgcolor("Hello World!",css.BGRED)print(colored_text)# External color (24-bit true color)hex_color="#d62828"ansi_color_code=css.hex2color(hex_color)colored_text=css.bgcolor("Hello World!",ansi_color_code)print(colored_text)Making text boldTo make text bold, you use thebold()function.Parametertext - The text you want bold.Returns:It returns a string containing the bolded text.importcssbolded_text=css.bold("Hello World!")print(bolded_text)Underlining textTo underline text, you use theunderline()function.Parametertext - The text you want to be underlinedReturns:It returns a string containing the underlined text.importcssunderlined_text=css.underline("Hello World!")print(underlined_text)Converting color formatspycsshas functions that allows you to convert between Hex color codes and RGB color codes.Hex to RGBTo convert hex color codes into RGB color codes, use thehex2rgb()function.Parameterhex_code - The hex code you want converted.Returns:This function returns a tuple with the format (red, green, blue) to represent RGB.importcss# Convert hex codehex_code="#ffc300"rgb=css.hex2rgb(hex_code)# Extract red, green, and blue from rgbred,green,blue=rgb[0],rgb[1],rgb[2]# Print them outprint("Red:",red)print("Green:",green)print("Blue:",blue)print("RGB color code:",rgb)Red: 255
Green: 195
Blue: 0
RGB color code: (255, 195, 0)RGB to hexTo convert RGB color codes into hex color codes, use thergb2hex()function.Parametersred - The red value in RGB.green - The green value in RGB.blue - The blue value in RGB.Returns:This function returns a string with the converted hex code.importcss# Convert RGB color codered,green,blue=255,195,0hex_code=css.rgb2hex(red,green,blue)# Print the converted hex_code outprint(hex_code)#ffc300