The "dominant color" is complex. What you want to do is compare the distance between each pixel and each other pixel in the color space (Euclidean Distance), and then find the pixel whose color is closest to any other color. This pixel is the dominant color. Medium color will usually be dirt.
I wish I had MathML to show you the Euclidean distance. Google it.
I performed the above execution in RGB color space using PHP / GD here: https://gist.github.com/cf23f8bddb307ad4abd8
It is, however, very expensive. This will crash your system on large images and, of course, crash your browser if you try it on the client. I worked on the reorganization of my execution: - save the results in the search table for future use in iteration for each pixel. - Divide large images into 20px 20px grids for localized dominance. - use the Euclidean distance between x1y1 and x1y2 to find out the distance between x1y1 and x1y3.
Please let me know if you make progress on this front. I would be glad to see that. I will do the same.
Canvas is by far the best way to do this in the client. SVG no, SVG - vector. After I take off the performance, the next thing I want to do is run it on the canvas (perhaps with a webmaster to calculate the total distance on each pixel).
Another thing to think about is that RGB is not a good color space for this, because the Euclidean distance between the colors in the RGB space is not very close to the visual distance. The best color space for this may be LUV, but I did not find a good library or any algorithms for converting RGB to LUV for this.
A completely different approach would be to sort your colors in a rainbow and build a histogram with tolerance to account for different shades of color. I have not tried this because sorting colors in a rainbow is difficult, and so are color histograms. I could try this further. Again, let me know if you are making any progress here.
user304736 Mar 30 2018-10-30T00: 00Z
source share