It just gets to all the pixels in the image and store their color value as a key in a hash map. The value is then the number of pixels of the same color encountered. However, this gives poor results so here are the improvements I wrote:
I treat at most 5000 pixels so if the image is bigger I start undersampling. This is an order of magnitude, not a certain rule.
To avoid noise due to flat areas (text, borders, ...) I start by right shifting all the RGB values by 6. So all the values are now between 0 and 3, allowing to encompass large areas of almost-the-same color. Then I perform another pass of the same algorithm (with a shift of 4) on all the pixels falling in the previous winning color group. And so on until I reach zero.
When trying to find the group that has the most pixels, I use a callback to weight the score. This allows customization and tell the algorithm you wand a rather dark color, you wan to exclude black, you want only highly saturated color excluding the greys or anything else you might think of.
Install-Package ColorFinder -Version 1.1.0
dotnet add package ColorFinder --version 1.1.0
<PackageReference Include="ColorFinder" Version="1.1.0" />
paket add ColorFinder --version 1.1.0
This package has no dependencies.