Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # List of all the letter colors.
- colorletters = 'drgybpcw'
- # ANSI Color Replacements
- extcolors = []
- # ANSI Color Codes
- colors = { 'n' : '0',
- 'd' : '30',
- 'r' : '31',
- 'g' : '32',
- 'y' : '33',
- 'b' : '34',
- 'p' : '35',
- 'c' : '36',
- 'w' : '37'}
- # Values of the 6x6x6 Cube
- values = [0, 95, 135, 175, 215, 255]
- def rgb2hsv(r, g, b):
- '''Convert an RGB color to HSV.'''
- r,g,b = r / 255.0, g / 255.0, b / 255.0
- mn = min(r,g,b) # Minimum Value
- mx = max(r,g,b) # Maximum Value
- dm = mx - mn # Delta Value
- v = mx
- if dm == 0: return 0,0,v # This is a gray, no chroma.
- else: s = dm / mx
- dr = ((( mx - r ) / 6) + (dm / 2) ) / dm
- dg = ((( mx - g ) / 6) + (dm / 2) ) / dm
- db = ((( mx - b ) / 6) + (dm / 2) ) / dm
- if r == mx: h = db - dg
- elif g == mx: h = (1/3.0) + dr - db
- elif b == mx: h = (2/3.0) + dg - dr
- if h < 0: h += 1
- if h > 1: h -= 1
- return h,s,v
- def simplecolor(c):
- '''Simplify an xterm 256 color code down to ANSI 16. It isn't perfect,
- but it works a whole lot better than stripping all color, or letting
- people with poor client choices from seeing a screen of garbled mess.
- '''
- if c < 16:
- # This is one of the 16 ANSI colors.
- if c > 7:
- return '1;%s' % colors[colorletters[c-8]]
- return '0;%s' % colors[colorletters[c]]
- elif c > 231:
- # Grayscale.
- if c > 250:
- return '1;%s' % colors['w']
- elif c > 244:
- return '0;%s' % colors['w']
- elif c > 233:
- return '1;%s' % colors['d']
- return '0;%s' % colors['d']
- # From the 6x6x6 Grid
- r = values[((c-16)/36) % 6]
- g = values[((c-16)/6) % 6]
- b = values[(c-16) % 6]
- h,s,v = rgb2hsv(r,g,b)
- # If the saturation is very low, treat it as gray.
- if s < 0.25:
- if v > 0.75:
- return '1;%s' % colors['w']
- elif v > 0.50:
- return '0;%s' % colors['w']
- elif v > 0.25:
- return '1;%s' % colors['d']
- return '0;%s' % colors['d']
- if h < 3/36.0 or h > 33/36.0:
- # Red
- if v > 0.75:
- return '1;%s' % colors['r']
- return '0;%s' % colors['r']
- elif h < 9/36.0:
- # Yellow
- if v > 0.75:
- return '1;%s' % colors['y']
- return '0;%s' % colors['y']
- elif h < 15/36.0:
- # Green
- if v > 0.75:
- return '1;%s' % colors['g']
- return '0;%s' % colors['g']
- elif h < 21/36.0:
- # Cyan
- if v > 0.75:
- return '1;%s' % colors['c']
- return '0;%s' % colors['c']
- elif h < 27/36.0:
- # Blue
- if v > 0.75:
- return '1;%s' % colors['b']
- return '0;%s' % colors['b']
- else:
- # Purple
- if v > 0.75:
- return '1;%s' % colors['p']
- return '0;%s' % colors['p']
- if __name__ == '__main__':
- buf = ''
- buf2 = ''
- ct = 0
- ln = 0
- for i in xrange(16, 232):
- buf += '\x1B[38;5;%dm%03d\x1B[0m ' % (i,i)
- buf2 += '\x1B[%sm%03d\x1B[0m ' % (simplecolor(i), i)
- ct += 1
- if ct > 5:
- print '%s %s' % (buf, buf2)
- buf = ''
- buf2 = ''
- ct = 0
- ln += 1
- if ln > 5:
- print ''
- ln = 0
- buf = ''
- buf2 = ''
- ct = 0
- for i in xrange(232, 256):
- buf += '\x1B[38;5;%dm%03d\x1B[0m ' % (i,i)
- buf2 += '\x1B[%sm%03d\x1B[0m ' % (simplecolor(i), i)
- ct += 1
- if ct > 5:
- print '%s %s' % (buf, buf2)
- buf = ''
- buf2 = ''
- ct = 0
- if buf:
- print '%s %s' % (buf, buf2)
- print ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement