def rgb_to_256(rgb):
 rgb = rgb.lstrip('#')
 if (len(rgb) == 0):
  return ('0', '000000')
 incs = (0, 95, 135, 175, 215, 255)
 parts = rgb_to_ints(rgb)
 res = []
 for part in parts:
  i = 0
  while (i < (len(incs) - 1)):
   (s, b) = (incs[i], incs[(i + 1)])
   if (s <= part <= b):
    s1 = abs((s - part))
    b1 = abs((b - part))
    if (s1 < b1):
     closest = s
    else:
     closest = b
    res.append(closest)
    break
   i += 1
 res = ''.join([('%02.x' % i) for i in res])
 equiv = RGB_256[res]
 return (equiv, res)

def rgb_to_256 ( rgb ) : rgb = rgb . lstrip ( '#' ) if ( len ( rgb ) == 0 ) : return ( '0' , '000000' ) incs = ( 0 , 95 , 135 , 175 , 215 , 255 ) parts = rgb_to_ints ( rgb ) res = [ ] for part in parts : i = 0 while ( i < ( len ( incs ) - 1 ) ) : ( s , b ) = ( incs [ i ] , incs [ ( i + 1 ) ] ) if ( s <= part <= b ) : s1 = abs ( ( s - part ) ) b1 = abs ( ( b - part ) ) if ( s1 < b1 ) : closest = s else : closest = b res . append ( closest ) break i += 1 res = '' . join ( [ ( '%02.x' % i ) for i in res ] ) equiv = RGB_256 [ res ] return ( equiv , res )
find the closest ansi 256 approximation to the given rgb value .