Support for RGB hexadecimal triplets colors notations

This commit is contained in:
Johann Dreo 2013-05-30 14:14:09 +02:00
commit 8832a41afe
2 changed files with 21 additions and 4 deletions

View file

@ -20,7 +20,8 @@ If you ask for less colors, the last one will be duplicated across remaining
groups.
Available colors are: blue, black, yellow, cyan, green, magenta, white, red,
rainbow, random, Random, scale, none or any number between 0 and 255.
rainbow, random, Random, scale, none, an RGB hexadecimal triplet or any number
between 0 and 255.
Available styles are: normal, bold, faint, italic, underline, blink,
rapid_blink, reverse, conceal or random (some styles may have no effect, depending
@ -38,9 +39,11 @@ below, [0-100] by default).
If the python-pygments library is installed, you can use the name of a
syntax-coloring "lexer" as a color (for example: "Cpp", "ruby", "xml+django", etc.).
If GIMP palettes files (*.gpl) are available, you can also use their names
as a colormap. Note that the RGB colors will be converted to their nearest ANSI
256-colors mode equivalents (see the `-P` switch below).
If GIMP palettes files (*.gpl) are available, you can also use their names as a
colormap (see the `-P` switch below).
Note that the RGB colors (either the hex triplets or the palettes's colors) will
be converted to their nearest ANSI 256-colors mode equivalents.
When not specified, a *COLOR* defaults to _red_ and a *STYLE* defaults to _bold_.

View file

@ -99,6 +99,13 @@ def rgb_to_ansi( red, green, blue ):
return int(val)
def hex_to_rgb(h):
assert( h[0] == "#" )
h = h.lstrip('#')
lh = len(h)
return tuple( int(h[i:i+lh//3], 16) for i in range(0, lh, lh//3) )
###############################################################################
# Global variables
###############################################################################
@ -339,6 +346,13 @@ def colorin(text, color="red", style="normal"):
mode = 8
color_code = str(30 + colors[color])
# hexadecimal color
elif color[0] == "#":
mode = 256
color_nb = rgb_to_ansi(*hex_to_rgb(color))
assert(0 <= color_nb <= 255)
color_code = str(color_nb)
# 256 colors mode
elif color.isdigit():
mode = 256