25afea776cc9c1fe8125db024c6296d5d9ccc56e
3 """ Convert values between RGB hex codes and xterm-256 color codes.
5 Nice long listing of all 256 colors and their codes. Useful for
6 developing console color themes, or even script output schemes.
9 * http://en.wikipedia.org/wiki/8-bit_color
10 * http://en.wikipedia.org/wiki/ANSI_escape_code
11 * /usr/share/X11/rgb.txt
13 I'm not sure where this script was inspired from. I think I must have
14 written it from scratch, though it's been several years now.
17 __author__
= 'Micah Elliott http://MicahElliott.com'
19 __copyright__
= 'Copyright (C) 2011 Micah Elliott. All rights reserved.'
20 __license__
= 'WTFPL http://sam.zoy.org/wtfpl/'
22 #---------------------------------------------------------------------
26 CLUT
= [ # color look-up table
29 # Primary 3-bit (8 colors). Unique representation!
39 # Equivalent "bright" versions of original 8 colors.
294 def _str2hex(hexstr
):
295 return int(hexstr
, 16)
297 def _strip_hash(rgb
):
298 # Strip leading `#` if exists.
299 if rgb
.startswith('#'):
300 rgb
= rgb
.lstrip('#')
304 short2rgb_dict
= dict(CLUT
)
306 for k
, v
in short2rgb_dict
.items():
307 rgb2short_dict
[v
] = k
308 return rgb2short_dict
, short2rgb_dict
310 def short2rgb(short
):
311 return SHORT2RGB_DICT
[short
]
314 """ Print all 256 xterm color codes.
316 for short
, rgb
in CLUT
:
317 sys
.stdout
.write('\033[48;5;%sm%s:%s' % (short
, short
, rgb
))
318 sys
.stdout
.write("\033[0m ")
319 sys
.stdout
.write('\033[38;5;%sm%s:%s' % (short
, short
, rgb
))
320 sys
.stdout
.write("\033[0m\n")
321 print "Printed all codes."
322 print "You can translate a hex or 0-255 code by providing an argument."
324 def hex_to_rgb(value
):
325 value
= value
.lstrip('#')
327 return tuple(int(value
[i
:i
+lv
/3], 16) for i
in range(0, lv
, lv
/3))
330 return '#%02x%02x%02x' % rgb
333 """ Find the closest xterm-256 approximation to the given RGB value.
334 @param rgb: Hex code representing an RGB value, eg, 'abcdef'
335 @returns: String between 0 and 255, compatible with xterm.
336 >>> rgb2short('123456')
338 >>> rgb2short('ffffff')
340 >>> rgb2short('0DADD6') # vimeo logo
343 rgb
= _strip_hash(rgb
)
344 incs
= (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)
345 # Break 6-char RGB code into 3 integer vals.
346 parts
= [ int(h
, 16) for h
in re
.split(r
'(..)(..)(..)', rgb
)[1:4] ]
350 while i
< len(incs
)-1:
351 s
, b
= incs
[i
], incs
[i
+1] # smaller, bigger
355 if s1
< b1
: closest
= s
361 res
= ''.join([ ('%02.x' % i
) for i
in res
])
362 equiv
= RGB2SHORT_DICT
[ res
]
363 #print '***', res, equiv
366 RGB2SHORT_DICT
, SHORT2RGB_DICT
= _create_dicts()
368 def imgge_to_display(path
):
372 #---------------------------------------------------------------------
374 if __name__
== '__main__':
377 if len(sys
.argv
) == 1:
381 if len(arg
) < 4 and int(arg
) < 256:
383 sys
.stdout
.write('xterm color \033[38;5;%sm%s\033[0m -> RGB exact \033[38;5;%sm%s\033[0m' % (arg
, arg
, arg
, rgb
))
384 sys
.stdout
.write("\033[0m\n")
386 short
, rgb
= rgb2short(arg
)
387 sys
.stdout
.write('RGB %s -> xterm color approx \033[38;5;%sm%s (%s)' % (arg
, short
, short
, rgb
))
388 sys
.stdout
.write("\033[0m\n")