we know that the true color map contains up to 2^24 color, how to choose 256 colors, but also make the color distortion is relatively small, this is a more complex problem. A simple practice is to r:g:b in 3:3:2, that is, take r,g high 3 bits, b high two bits, composed of a byte, so it can represent 256 colors, but it is not difficult to imagine that this method of distortion is certainly very serious.
The algorithm we introduce below is a good way to realize the conversion of true color graph to 256 color graph. The idea is to prepare an array of length 4096, representing 4096 colors. For each pixel in the graph, take the highest four bits of R, G, B, and spell a 12-bit integer with the corresponding array element plus 1. When all the statistics are finished, the frequency of use of these 4096 colors is obtained. There may be some colors that are not used once, that is, the corresponding array element is 0 (assuming that there are palcounts of array elements that are not zero). Clear these zero array elements so that the first palcounts elements are nonzero. The number of palcounts is arranged in order from large to small (here we use bubble sort). This way, the top 256 colors are the most used, and they will be the 256 colors on the palette. For the remaining PalCounts-256 color is not simply discarded, but instead of using one of the first 256 colors, instead of the principle is to find the least square error of the one. Again to each pixel in the diagram, take R, G, B of the highest four bits, to spell a 12-bit integer, if the corresponding value in the first 256 colors, the index value is directly filled in the bitmap data, if it is in the latter PalCounts-256 color, then replace the color of the index value into the bitmap data.
True Color map to 256 color chart