class_docstr: Color palette for palette mapped images
  
  :param mode: The mode to use for the palette. See:
      :ref:`concept-modes`. Defaults to "RGB"
  :param palette: An optional palette. If given, it must be a bytearray,
      an array or a list of ints between 0-255. The list must consist of
      all channels for one color followed by the next color (e.g. RGBRGBRGB).
      Defaults to an empty palette.
  :param size: An optional palette size. If given, an error is raised
      if ``palette`` is not of equal length.
class_docstr_tok: ["Color","palette","for","palette","mapped","images",":","param","mode",":","The","mode","to","use","for","the","palette",".","See",":",":","ref",":","`","concept-modes","`",".","Defaults","to","``","RGB","''",":","param","palette",":","An","optional","palette",".","If","given",",","it","must","be","a","bytearray",",","an","array","or","a","list","of","ints","between","0-255",".","The","list","must","consist","of","all","channels","for","one","color","followed","by","the","next","color","(","e.g",".","RGBRGBRGB",")",".","Defaults","to","an","empty","palette",".",":","param","size",":","An","optional","palette","size",".","If","given",",","an","error","is","raised","if","``","palette","``","is","not","of","equal","length","."]
docstr: Given an rgb tuple, allocate palette entry.
  
  .. warning:: This method is experimental.
docstr_tok: ["Given","an","rgb","tuple",",","allocate","palette","entry",".","..","warning",":",":","This","method","is","experimental","."]
code: def getcolor(self, color, image=None):
      """Given an rgb tuple, allocate palette entry.
  
      .. warning:: This method is experimental.
      """
      if self.rawmode:
          raise ValueError("palette contains raw palette data")
      if isinstance(color, tuple):
          if self.mode == "RGB":
              if len(color) == 4 and color[3] == 255:
                  color = color[:3]
          elif self.mode == "RGBA":
              if len(color) == 3:
                  color += (255,)
          try:
              return self.colors[color]
          except KeyError as e:
              # allocate new color slot
              if not isinstance(self.palette, bytearray):
                  self._palette = bytearray(self.palette)
              index = len(self.palette) // 3
              special_colors = ()
              if image:
                  special_colors = (
                      image.info.get("background"),
                      image.info.get("transparency"),
                  )
              while index in special_colors:
                  index += 1
              if index >= 256:
                  if image:
                      # Search for an unused index
                      for i, count in reversed(
                          list(enumerate(image.histogram()))
                      ):
                          if count == 0 and i not in special_colors:
                              index = i
                              break
                  if index >= 256:
                      raise ValueError(
                          "cannot allocate more than 256 colors"
                      ) from e
              self.colors[color] = index
              if index * 3 < len(self.palette):
                  self._palette = (
                      self.palette[: index * 3]
                      + bytes(color)
                      + self.palette[index * 3 + 3 :]
                  )
              else:
                  self._palette += bytes(color)
              self.dirty = 1
              return index
      else:
          raise ValueError(f"unknown color specifier: {repr(color)}")
  
file_path: pillow-simd/src/PIL/ImagePalette.py
imports: import array
  import warnings
  from .None import GimpGradientFile, GimpPaletteFile, ImageColor, PaletteFile
label_desc: Use image node_id 7 for calling the ImagePalette obj's underlying member method code with example usage: obj.getcolor(color, image) and returns: self, index