ANSI colors for Python
ANSI colors for Python
Add ANSI colors and decorations to your strings.
from __future__ import print_function # accomodate Python 2 from colors import * print(color('my string', fg='blue')) print(color('some text', fg='red', bg='yellow', style='underline'))
The strings returned by color will have embedded ANSI code sequences stipulating text colors and styles. For example, the above code will print the strings:
'\x1b[34mmy string\x1b[0m' '\x1b[31;43;4msome text\x1b[0m'
You can choose the foreground (text) color with the fg parameter, the background color with bg, and the style with style.
You can choose one of the 8 basic ANSI colors: black, red, green, yellow, blue, magenta, cyan, and white, plus a special default which is display-specific, but usually a rational “no special color” setting.
There are other ways to specify colors. Many devices support an idiosyncratic 256-color scheme developed as an extension to the original ANSI codes for the xterm terminal emulator. Colors (or grays) from this larger palette can be specified via int value (0-255).
To see them all:
from __future__ import print_function from colors import color for i in range(256): print(color('Color #%d' % i, fg=i))
The included show_colors.py program is a much-expanded version of this idea that can be used to explore available color and style combinations on your terminal or output device.
24-bit Color and CSS Compatibility
Modern terminals go even further than the xterm 256, often supporting a full 24-bit RGB color scheme. You can provide a full RGB value several ways:
- with a 3-element tuple or list of int, each valued 0 to 255 (e.g. (255, 218, 185)),
- a string containing a CSS-compatible color name (e.g. 'peachpuff'),
- a string containing a CSS-style hex value (e.g. '#aaa' or '#8a2be2')
- a string containing a CSS-style RGB notation (e.g. 'rgb(102,51,153)')
These forms can be mixed and matched at will:
print(color('orange on gray', 'orange', 'gray')) print(color('nice color', 'white', '#8a2be2'))
Note that any color name defined in the basic ANSI color set takes primacy over the CSS color names. Combined with the fact that terminals do not always agree which precise tone of blue should qualify as ANSI blue, there can be some ambiguity regarding the named colors. If you need full precision, specify the RGB color exactly. The parse_rgb function can be used to identify the correct definition according to the CSS standards.
Unfortunately there is no guarantee that every terminal will support all the colors and styles ANSI ostensibly defines. In fact, most implement a rather small subset. Colors are better supported than styles, for which you might get one or two of the most popular such as bold or underline. Might.
Whatever colors and styles are supported, there is no guarantee they will be accurately rendered. Even at this late date, over fifty years after the codes began to be standardized, support from terminals and output devices is limited, fragemented, and piecemeal.
ANSI codes evolved in an entirely different historical context from today’s. Both the Web and the idea of broad standardization were decades in the future. Display technology was low-resolution, colors were limited on the rare occasions they were present, and color/style fidelity was not a major consideration. Vendors thought little or nothing of creating their own proprietary codes, implementing functions differently from other vendors, and/or co-opting codes previously in use for something else. Practical ANSI reference materials include many phrases such as ‘hardly ever supported’ and ‘non-standard.’
We still use ANSI codes today not because they’re especially good, but because they’re the best, most-standard approach that pre-Web displays even remotely agreed upon. Even deep into the Web era, text output endures as an important means of human-computer interaction. The good news, such is it is: ANSI’s color and style specifications (“SGR” or “Select Graphic Rendition” in ANSI terminology) are the most-used and best-adhered-to portion of the whole ANSI show.
# use some partial functions from __future__ import print_function # so works on Python 2 and 3 alike from colors import red, green, blue print(red('This is red')) print(green('This is green')) print(blue('This is blue'))
Optionally you can add a background color and/or styles.:
print(red('red on blue', bg='blue')) print(green('green on black', bg='black', style='underline'))
You can use multiple styles at once. Separate them with a +.:
print(red('very important', style='bold+underline'))
You can additionally specify one of the supported styles: none, bold, faint, italic, underline, blink, blink2, negative, concealed, crossed. While most devices support only a few styles, unsupported styles are generally ignored, so the only harm done is your text is less pretty and/or formatted than you might like. A good general rule is to enjoy the formatting if you get it, but don’t depend on it–especially don’t depend on styles like blink (e.g. to highlight critical data) or concealed (e.g. to hide data). Most likely, they won’t.
If you use a style often, you may want to create your own named style:
from functools import partial from colors import color important = partial(color, fg='red', style='bold+underline')) print(important('this is very important!'))
In deailing with ANSI-styled text, it can be necessary to determine the “equivalent” text minus the styling. The function strip_color(s) does that, removing ANSI codes from s, returning its “plain text equivalent.”
You may also wish to determine the effective length of a string. If it contains ANSI codes, the builtin len() function will return the length including those codes, even though they are logically 0-length. So plain len(s) is probably not what you need. ansilen(s) in contrast returns the “effective” length of the string, including only the non-ANSI characters. ansilen(s) is equivalent to len(strip_color(s)),
colors is licensed under the ISC license.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for ansicolors-1.1.8-py2.py3-none-any.whl