code: private static int toAnsi(long color)
      {
          if (color >= 0) { // an rgb value encoded as in Color.getRGB
              return toAnsi(getRed(color), getGreen(color), getBlue(color));
          }
          else {
              return (int) (-color - 1);
          }
      }
docs: Convert the given color (rgb or system) to an ansi-compatible index (for use with ESC[38;5;<value>m)