code: def _get_closest_ansi_color(
      r: int, g: int, b: int, exclude: Sequence[str] = ()
  ) -> str:
      """
      Find closest ANSI color. Return it by name.
  
      :param r: Red (Between 0 and 255.)
      :param g: Green (Between 0 and 255.)
      :param b: Blue (Between 0 and 255.)
      :param exclude: A tuple of color names to exclude. (E.g. ``('ansired', )``.)
      """
      exclude = list(exclude)
  
      # When we have a bit of saturation, avoid the gray-like colors, otherwise,
      # too often the distance to the gray color is less.
      saturation = (
          abs(r - g) + abs(g - b) + abs(b - r)
      )  # Between 0..510
  
      if saturation > 30:
          exclude.extend(
              [
                  "ansilightgray",
                  "ansidarkgray",
                  "ansiwhite",
                  "ansiblack",
              ]
          )
  
      # Take the closest color.
      # (Thanks to Pygments for this part.)
      distance = (
          257 * 257 * 3
      )  # "infinity" (>distance from #000000 to #ffffff)
      match = "ansidefault"
  
      for name, (r2, g2, b2) in ANSI_COLORS_TO_RGB.items():
          if name != "ansidefault" and name not in exclude:
              d = (r - r2) ** 2 + (g - g2) ** 2 + (b - b2) ** 2
  
              if d < distance:
                  match = name
                  distance = d
  
      return match
  
imports: from __future__ import annotations
  import io
  import os
  import sys
  from typing import Callable, Dict, Hashable, Iterable, Sequence, TextIO, Tuple
  from prompt_toolkit.cursor_shapes import CursorShape
  from prompt_toolkit.data_structures import Size
  from prompt_toolkit.output import Output
  from prompt_toolkit.styles import ANSI_COLOR_NAMES, Attrs
  from prompt_toolkit.utils import is_dumb_terminal
  from .color_depth import ColorDepth
  from .flush_stdout import flush_stdout
label_desc: Use image node_id 1 for calling a global function with example usage: _get_closest_ansi_color(r, g, b, exclude) and returns: match