SHARE
TWEET

Untitled

a guest Jun 23rd, 2011 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # List of all the letter colors.
  3. colorletters = 'drgybpcw'
  4.  
  5. # ANSI Color Replacements
  6. extcolors = []
  7.  
  8. # ANSI Color Codes
  9. colors = { 'n' : '0',
  10.             'd' : '30',
  11.             'r' : '31',
  12.             'g' : '32',
  13.             'y' : '33',
  14.             'b' : '34',
  15.             'p' : '35',
  16.             'c' : '36',
  17.             'w' : '37'}
  18.            
  19. # Values of the 6x6x6 Cube
  20. values = [0, 95, 135, 175, 215, 255]
  21.  
  22. def rgb2hsv(r, g, b):
  23.     '''Convert an RGB color to HSV.'''
  24.     r,g,b = r / 255.0, g / 255.0, b / 255.0
  25.    
  26.     mn = min(r,g,b) # Minimum Value
  27.     mx = max(r,g,b) # Maximum Value
  28.     dm = mx - mn    # Delta Value
  29.     v = mx
  30.    
  31.     if dm == 0: return 0,0,v # This is a gray, no chroma.
  32.     else: s = dm / mx
  33.    
  34.     dr = ((( mx - r ) / 6) + (dm / 2) ) / dm
  35.     dg = ((( mx - g ) / 6) + (dm / 2) ) / dm
  36.     db = ((( mx - b ) / 6) + (dm / 2) ) / dm
  37.    
  38.     if   r == mx: h = db - dg
  39.     elif g == mx: h = (1/3.0) + dr - db
  40.     elif b == mx: h = (2/3.0) + dg - dr
  41.     if h < 0: h += 1
  42.     if h > 1: h -= 1
  43.     return h,s,v
  44.  
  45. def simplecolor(c):
  46.     '''Simplify an xterm 256 color code down to ANSI 16. It isn't perfect,
  47.       but it works a whole lot better than stripping all color, or letting
  48.       people with poor client choices from seeing a screen of garbled mess.
  49.    '''
  50.    
  51.     if c < 16:
  52.         # This is one of the 16 ANSI colors.
  53.         if c > 7:
  54.             return '1;%s' % colors[colorletters[c-8]]
  55.         return '0;%s' % colors[colorletters[c]]
  56.    
  57.     elif c > 231:
  58.         # Grayscale.
  59.         if c > 250:
  60.             return '1;%s' % colors['w']
  61.         elif c > 244:
  62.             return '0;%s' % colors['w']
  63.         elif c > 233:
  64.             return '1;%s' % colors['d']
  65.         return '0;%s' % colors['d']
  66.    
  67.     # From the 6x6x6 Grid
  68.    
  69.     r = values[((c-16)/36) % 6]
  70.     g = values[((c-16)/6) % 6]
  71.     b = values[(c-16) % 6]
  72.    
  73.     h,s,v = rgb2hsv(r,g,b)
  74.    
  75.     # If the saturation is very low, treat it as gray.
  76.     if s < 0.25:
  77.         if v > 0.75:
  78.             return '1;%s' % colors['w']
  79.         elif v > 0.50:
  80.             return '0;%s' % colors['w']
  81.         elif v > 0.25:
  82.             return '1;%s' % colors['d']
  83.         return '0;%s' % colors['d']
  84.    
  85.     if h < 3/36.0 or h > 33/36.0:
  86.         # Red
  87.         if v > 0.75:
  88.             return '1;%s' % colors['r']
  89.         return '0;%s' % colors['r']
  90.        
  91.     elif h < 9/36.0:
  92.         # Yellow
  93.         if v > 0.75:
  94.             return '1;%s' % colors['y']
  95.         return '0;%s' % colors['y']
  96.    
  97.     elif h < 15/36.0:
  98.         # Green
  99.         if v > 0.75:
  100.             return '1;%s' % colors['g']
  101.         return '0;%s' % colors['g']
  102.    
  103.     elif h < 21/36.0:
  104.         # Cyan
  105.         if v > 0.75:
  106.             return '1;%s' % colors['c']
  107.         return '0;%s' % colors['c']
  108.    
  109.     elif h < 27/36.0:
  110.         # Blue
  111.         if v > 0.75:
  112.             return '1;%s' % colors['b']
  113.         return '0;%s' % colors['b']
  114.    
  115.     else:
  116.         # Purple
  117.         if v > 0.75:
  118.             return '1;%s' % colors['p']
  119.         return '0;%s' % colors['p']
  120.  
  121. if __name__ == '__main__':
  122.     buf = ''
  123.     buf2 = ''
  124.     ct = 0
  125.     ln = 0
  126.     for i in xrange(16, 232):
  127.         buf += '\x1B[38;5;%dm%03d\x1B[0m ' % (i,i)
  128.         buf2 += '\x1B[%sm%03d\x1B[0m ' % (simplecolor(i), i)
  129.         ct += 1
  130.         if ct > 5:
  131.             print '%s  %s' % (buf, buf2)
  132.             buf = ''
  133.             buf2 = ''
  134.             ct = 0
  135.             ln += 1
  136.             if ln > 5:
  137.                 print ''
  138.                 ln = 0
  139.    
  140.     buf = ''
  141.     buf2 = ''
  142.     ct = 0
  143.     for i in xrange(232, 256):
  144.         buf += '\x1B[38;5;%dm%03d\x1B[0m ' % (i,i)
  145.         buf2 += '\x1B[%sm%03d\x1B[0m ' % (simplecolor(i), i)
  146.         ct += 1
  147.         if ct > 5:
  148.             print '%s  %s' % (buf, buf2)
  149.             buf = ''
  150.             buf2 = ''
  151.             ct = 0
  152.    
  153.     if buf:
  154.         print '%s  %s' % (buf, buf2)
  155.    
  156.    
  157.     print ''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top