Prehistory
A long time ago, when my knowledge of PHP was at the level of operations and output, and I found a very useful class written by Kalpeh Gamit, its site has long been unavailable, so unfortunately I can’t leave contacts. The class is distributed under the name GetImageColor and licensed under the GPL 2.0.
I used this class often. It accepts an image as an input parameter (only in JPEG format, but is supplemented with all the others in a minute), the number of returned colors and the processing step. The script reads the color from the photo with the specified step and enters it into an array of colors. The resulting array is sorted by color entries and only the specified number of colors is returned.
Very handy script for many tasks. Personally, I needed it in the first version of Fotokiosk, with the help of a script I defined monochromatic photos and did not allow them to be uploaded to the server (too dark, illuminated, etc.). Later in LetPrint, I applied it to determine whether the sheet is full and chromatic.
')
Today saw article on Habré:
habrahabr.ru/blogs/web_design/136343
I was interested in this. The image chosen is not just common colors, but dominant or contrasting colors. Remembering what the class would give out, I realized that another solution was needed.

Decision
Actually, a similar script may work well for them, but they add the final color options manually, which is most likely happening, because the presence of the selected colors is not so dominant in the picture. And the colors are too soft. But maybe they are just smoothing similar colors, which I noticed.
The next stage is an attempt to achieve smoothing. We need an algorithm that would process the array with the available colors, select similar colors in it and find the smoothed color, and so on until 5 main ones were selected.
It was not difficult to write such an algorithm, but the search for similar ones slowed me a little. Although I did not understand him for a long time, I still found the handicraft solution easily.
Our color consists of three components: R + G + B. All these values ​​from 0 to 255. The colors 250,100,100 and 240.90.90 are similar, but the second is darker than the first, but they are nonetheless similar. Color 245,100,100 is also similar to the first two, but to a lesser extent. Actually here the error begins. The first two colors are similar, but 255,255,255 and 0,0,0 are similar and it immediately discards black and white photographs. Also, due to the similarity of the first two and third, shades are discarded. But we are ready for such sacrifices.
The number of similarities - | RG |. | RB |. | BG |, also for introducing similar shades, the difference itself is also processed dif = round (round (| RG | / pogr) * pogr). By the pogr coefficient, we introduce an error. For example, if pogr = 10, then the colors 254,180,35 and 234,163,19 are similar. The greater the error, the more similar shades we will combine.
After you must consider the weight of the color, smooth the array into a single color. We have an array of similar colors. Each 2 parameters - color and number of entries. The actually smoothed color will consist of R = SUM (Ri * mi) / SUM (mi), where mi is the number of occurrences of this color, also with the other components of G and B. This means that since we are black and white, as a result of the algorithm, we get a gray, which may not be present in the picture, but which will completely reflect the mixture of black and white.
With the resulting array, we perform these operations again and again until we achieve the result. At some point, the algorithm may simply stall, so the sampling error of similar
pogr increases with each iteration.
Result
As a result, the processing speed of this algorithm is very high. The tones in the majority correspond to those in the figure, but some errors are obvious, which are quite difficult to eliminate in simple ways. By and large, this is pampering, until I figured out where to use it, but it may well be a good thing for designers.

You can try the algorithm in action here:
blog.assorium.ru/jpegMy server is weak, so I will ask you not to push it hard. Restrictions: JPEG images with a weight of no more than 500 KB.
Alternative solutions
The easiest way to solve this problem is a catalog of tones. I already bumped into him somewhere. They offered the most frequently used and pleasant colors. The catalog consisted of several thousand colors, which would be quite enough for this script. The processing basically remains the same, but when smoothed, a color from the catalog is added to the final array, which is similar to the current one and so on until several colors remain. The advantage of the method is obvious, you will get a color that is good in itself and will not cut the eye and at the same time will reflect the essence of the picture.
The second way is to search by contrast. In this case, the number of occurrences of a color is not considered, but the colors themselves are considered directly. By iteration, contrasting shades are selected and smoothed according to their gradients. This method has not yet been considered by me, but I think if I have time, I will take it. Its advantage is in the exact essence of the picture. It will give exactly those colors that are highlighted in the image. Search in contrast in this case will be better than the other two.
UPD_1 There are a lot of naked women among the uploaded photos. Why would anyone find out the tone of the naked body?
UPD_2 4utep found a ready-made solution, but with the help of HSV and a certain set of colors.
linkPush_Ok prompted the “CIE color difference formula”. Perhaps it will improve the definition of similar. Until I determined how much.
linkUPD_3 More than 1500 pictures were loaded per day. Thanks to the habrayusers for the epic pictures of the past three months and a considerable amount of erotica. Among the pictures even caught instances asking for an invite. My hoster made me very happy. The site did not fall down and never slowed down.