code: def rgb2ansi(r, g, b):
      
  
      # Thanks to
      # https://github.com/tehmaze/ansi/blob/master/ansi/colour/rgb.py
  
      grayscale = False
      poss = True
      step = 2.5
  
      while poss:
          if min(r, g, b) < step:
              grayscale = max(r, g, b) < step
              poss = False
  
          step += 42.5
  
      if grayscale:
          return 232 + int(float(sum((r, g, b)) / 33.0))
  
      m = ((r, 36), (g, 6), (b, 1))
      return 16 + sum(int(6 * float(val) / 256) * mod for val, mod in m)
docs: Convert an RGB color to 256 ansi graphics.