# File lib/gd2/image.rb, line 671
    def to_indexed_color(colors = MAX_COLORS, dither = true)
      obj = IndexedColor.allocate
      ptr = SYM[:gdImageCreatePaletteFromTrueColor].call(
        to_true_color.image_ptr, dither ? 1 : 0, colors)[0]
      raise LibraryError unless ptr

      obj.init_with_image(ptr)

      # fix for gd bug where image->open[] is not properly initialized
      (0...ptr[:colorsTotal]).each do |i|
        ptr["open[#{i}]""open[#{i}]"] = 0
      end

      obj
    end