First off you should share the code that you are using to collect the first bit of data. That way we can help develop it. Then it might be a good idea to pick up a pen and some paper and try to brainstorm a little. How could you do this manually? If you had a list of all the colors with all the variations, how would you sort them in order to get a good overview? Could you visualize it? How would you move on from there?
Then, when you have an idea of what you want to do or how you want to do it, you will probably have to break the problem down into smaller tasks.
And if you have any questions or have problems solving the tasks we are here to help you.
To clarify, I’m not a programming beginner (though Java is definitely not my cup of tea). I’m looking for a good general solution I can reuse, not help cooking up a personal inefficient one.
I’ve been reading for hours on methods to achieve this, which is how I came to Image Quantization, LAB, Median cut, K-means, etc. This is a (more than) solved problem and any semi-decent image editing application can achieve this via a GUI. As such, I find it hard to believe no one has a ready-made approach they like and stick to. I’d be more than happy to help improve one of those, but I’m not interested in reinventing the wheel, especially when it comes to mathematical problems. Any solution I can come up with will be inferior to an established mathematical algorithm.
I remember an OLD Processing implementation of the neuquant algorithm for neural network color quantization. If what you want is “maximally flexible” (any input, any number of bins, etc) more than “performant” you could check out its results and see if you like them. Will probably need to be updated to Processing 3 – that example was 14 years ago.