class_docstr: Cache which maps (r, g, b) tuples to 256 colors.
class_docstr_tok: ["Cache","which","maps","(","r",",","g",",","b",")","tuples","to","256","colors","."]
code: def __missing__(self, value: tuple[int, int, int]) -> int:
      r, g, b = value
  
      # Find closest color.
      # (Thanks to Pygments for this!)
      distance = (
          257 * 257 * 3
      )  # "infinity" (>distance from #000000 to #ffffff)
      match = 0
  
      for i, (r2, g2, b2) in enumerate(self.colors):
          if (
              i >= 16
          ):  # XXX: We ignore the 16 ANSI colors when mapping RGB
              # to the 256 colors, because these highly depend on
              # the color scheme of the terminal.
              d = (r - r2) ** 2 + (g - g2) ** 2 + (b - b2) ** 2
  
              if d < distance:
                  match = i
                  distance = d
  
      # Turn color name into code.
      self[value] = match
      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 2 for calling the _256ColorCache obj's underlying member method code with example usage: obj.__missing__(value) and returns: match