1a165cf47ebf2672d296fa0a33004fc81ea935fa
4 """ Convert values between RGB hex codes and xterm-256 color codes.
6 Nice long listing of all 256 colors and their codes. Useful for
7 developing console color themes, or even script output schemes.
10 * http://en.wikipedia.org/wiki/8-bit_color
11 * http://en.wikipedia.org/wiki/ANSI_escape_code
12 * /usr/share/X11/rgb.txt
14 I'm not sure where this script was inspired from. I think I must have
15 written it from scratch, though it's been several years now.
18 __author__
= 'Micah Elliott http://MicahElliott.com'
20 __copyright__
= 'Copyright (C) 2011 Micah Elliott. All rights reserved.'
21 __license__
= 'WTFPL http://sam.zoy.org/wtfpl/'
23 #---------------------------------------------------------------------
27 CLUT
= [ # color look-up table
30 # Primary 3-bit (8 colors). Unique representation!
40 # Equivalent "bright" versions of original 8 colors.
295 def _strip_hash(rgb
):
296 # Strip leading `#` if exists.
297 if rgb
.startswith('#'):
298 rgb
= rgb
.lstrip('#')
302 short2rgb_dict
= dict(CLUT
)
304 for k
, v
in short2rgb_dict
.items():
305 rgb2short_dict
[v
] = k
306 return rgb2short_dict
, short2rgb_dict
308 def short2rgb(short
):
309 return SHORT2RGB_DICT
[short
]
311 def pixel_print(ansicolor
):
312 sys
.stdout
.write('\033[48;5;%sm \033[0m' % (ansicolor
))
315 """ Print all 256 xterm color codes.
317 for short
, rgb
in CLUT
:
318 sys
.stdout
.write('\033[48;5;%sm%s:%s' % (short
, short
, rgb
))
319 sys
.stdout
.write("\033[0m ")
320 sys
.stdout
.write('\033[38;5;%sm%s:%s' % (short
, short
, rgb
))
321 sys
.stdout
.write("\033[0m\n")
322 print "Printed all codes."
323 print "You can translate a hex or 0-255 code by providing an argument."
325 def hex_to_rgb(value
):
326 value
= value
.lstrip('#')
328 return tuple(int(value
[i
:i
+lv
/3], 16) for i
in range(0, lv
, lv
/3))
331 return '#%02x%02x%02x' % rgb
334 """ Find the closest xterm-256 approximation to the given RGB value.
335 @param rgb: Hex code representing an RGB value, eg, 'abcdef'
336 @returns: String between 0 and 255, compatible with xterm.
338 rgb
= _strip_hash(rgb
)
343 min_distance
= 1000000000
344 ary
= [hex_to_rgb(hex) for hex in RGB2SHORT_DICT
]
346 d
= (ri
-r
)**2 + (gi
-g
)**2 + (bi
-b
)**2
349 match
= rgb_to_hex((ri
,gi
,bi
))
350 return RGB2SHORT_DICT
[_strip_hash(match
)]
352 # incs = (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)
353 # parts = [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgb)[1:4] ]
357 # while i < len(incs)-1:
358 # s, b = incs[i], incs[i+1] # smaller, bigger
362 # if s1 < b1: closest = s
364 # res.append(closest)
368 # res = ''.join([ ('%02.x' % i) for i in res ])
369 # equiv = RGB2SHORT_DICT[ res ]
370 # #print '***', res, equiv
373 RGB2SHORT_DICT
, SHORT2RGB_DICT
= _create_dicts()
375 def image_to_display(path
):
377 i
= i
.convert('RGBA')
380 rows
, columns
= os
.popen('stty size', 'r').read().split()
381 width
= min(w
, int(columns
)-2*6)
382 height
= int(float(h
) * (float(width
) / float(w
)))
384 i
= i
.resize((width
, height
), Image
.BICUBIC
)
386 for y
in xrange(height
):
388 for x
in xrange(width
):
389 p
= i
.getpixel((x
,y
))
391 hex = rgb_to_hex((r
,g
,b
))
392 pixel_print(rgb2short(hex))
395 #---------------------------------------------------------------------
397 if __name__
== '__main__':
399 image_to_display(path
)
402 # if len(sys.argv) == 1:
406 # if len(arg) < 4 and int(arg) < 256:
407 # rgb = short2rgb(arg)
408 # 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))
409 # sys.stdout.write("\033[0m\n")
411 # short = rgb2short(arg)
412 # sys.stdout.write('RGB %s -> xterm color approx \033[38;5;%sm%s ' % (arg, short, short ))
413 # sys.stdout.write("\033[0m\n")