True Color map to 256 color chart

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.