For a project, I'm looking for an algorithm to convert a large number of images into image palettes that can share the same palettes.
Story
Given:
- A list of images (RGB) that already have the final colors to use.
Result:
- List of images (indicating)
- Palette list
- Multiple RGB images can be converted to a single displayed image using various palettes .
- I want to use the minimum number of images required with the minimum number of palettes needed.
Limitations:
- There are a maximum of n palettes
- The maximum number of colors on the palette
- The maximum number of images u that can be generated as a result
What is my problem:
- I do not know how to build an algorithm so that it can decide if any previous solution was wrong for future problems. (see below).
- I donβt know how to decide to reorder the colors of the palettes and the image data, because reformatting one image can lead to subsequent reordering problems, which can end with an endless brute force battle: D (see below)
Here is the full story
Result
So here should be the result . In this case, one color index table (similar to the displayed image) generates two different RGB images using two different palettes.

The first steps
Since the input files are all RGB images, we first need to convert them to palettes with the corresponding color indices.
In the following figure, you can see how the algorithm can start working with the first three images:
- Convert them to palettes and color indices
- Note that the two images share the same color indices , if so, we can reuse the color indices but create a palette (not necessary!)
- Otherwise, we could add four new colors to the first palette and create a new color index . (Not shown in this image)
- For the third image, we found a palette that already contains all the necessary colors, so we decided to reuse the palette, but change our color indices to match the existing palette.

Let it get complicated!
So far so good. But how can we continue working with the last image? It can share the color indices of the previous one, but then it does not match any existing palette. In fact, this does not match any existing palette.
So, the following image describes the actual problem : how to solve what works best for images? Creating a new palette, creating a new color index, what if everything goes well, if in the past we decided otherwise? How can we find out?

Reordering
Well, these four images are still simple. Suppose the algorithm has already processed many images and created a palette. The next image in our list of input images will find a suitable palette, so it can easily create new color indices and be fine. But: the result should be at least images and palettes, so there may be another way!
Checking all the previous images, we found out that we could use the existing color indices and the previous image, but we need to change the order of the palette. And the permutation requires us to check all previous images if they agree with the redone palette.
As you can see: the color palettes in the last step were rearranged to match the same colors below. But it can be a complicated process of rearranging many other images.

Thank you in advance for any advice on how to implement such an algorithm or what to look for or which existing algorithms generate almost the same result.
Edit
Here's a real-life lifestyle with graphics stolen from an old amiga game:

This panel contains 256 RGB images with 16 * 16 pixels. Each of these images is a fragment that must be processed by the algorithm. The first few images are equal to each other, but later there are several other images. You can split palettes into a maximum of 6 palettes with 16 colors , but with the limitation of always having the first color of a transparent color. (so actually its 15 colors per palette)
In this example, it is easy to reuse the same ColorIndices for 4 color keys, doors, and diamonds.
This is an example of a generated palette: 
Change No. 2
Here is another example that I extracted from an old game:

The palette might look like this:
