Reputation: 763
I'm searching for a single function to convert RGB tuple (255, 255, 255
) or HTML hex (#FFFFFF
) to X11 color (0...255 int
) to manage colors in Unix terminal. I've looked over the internet but I didn't find anything like that, so I'm asking for code or link to a website that contains code or information about how I can do this in Python.
Upvotes: 3
Views: 2633
Reputation: 2050
Fabulous can map arbitrary RGB colors to their closest xterm 256 color.
Upvotes: 0
Reputation: 88278
As both @chepner and @Shawabawa have mentioned, there isn't a mapping from the 3-tuple of a RGB code or a 6-digit hex to a single number with 8-bits (0-255). Indeed if there was, some information would have to be lost as the mapping is many to one. What you could do however, is use the (BASH) color codes as a starting point for building a Python script. For example, with the following variables:
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
You can put it into a script, i.e.
cmd = "export PS1='%s'"
prompt = "%s foo %s bar : "
prompt = prompt % (undred, txtblu)
print cmd % prompt
When I run the output, I get something like:
t@hydrogen:~/test/color$ python test.py
export PS1='\e[4;31m foo \e[0;34m bar : '
t@hydrogen:~/test/color$ export PS1='\e[4;31m foo \e[0;34m bar : '
foo bar :
And now the foo is red and underlined, and bar is in blue.
Upvotes: 1
Reputation: 532528
If I understand your question properly, I don't think you can do this. The RGB tuple (and its representation as a 6-digit hex number for HTML) represents a specific color in the RGB colorspace. The single integer in the range 0 to 255 you mention for the Unix terminal isn't a specific color, but rather an index into a color table. As an example, color 15 on one terminal may be #ff0000, but #33ff33 on another. There really isn't a conversion between the two.
Upvotes: 1
Reputation: 2597
You can't convert to x11 colors automatically.
x11 colors are basically just names mapped to arbitrary RGB values, so the only way to get from RGB to x11 is to reverse the map.
If you want to convert an RGB color that isn't in x11 to the closest match in x11 that would be pretty difficult.
Upvotes: 1