Count Unqiue Colors Quickly


Author: Anonymous

Version Compatibility: Visual Basic 6

More information:
My UniqueColors function grabs the DIB bits of an image, converting to 24-bit as needed. It loops through the pixels and checks a "registry" to see if the bit for that color is set. If not, it sets it and adds '1' to the count. This method tracks 16M colors by utilizing a 2MB bytearray, each byte tracking 8 colors (1 color per bit). So, as the code loops through the pixels, it divides the long value of the color by eight to get the appropriate byte in the array and uses 2 to the power of the modulus for the appropriate bit. The alternative would be to use a Boolean array, which would require 16MB. Also, I have used type characters ('&' and '%') instead of full declarations (' As Long' and 'As Integer', respectively) since the latter requires VB to constantly convert the variable from a variant to the declared type. Type symbols are native and, therefore, notably faster.

Instructions: Copy the declarations and code below and paste directly into your VB project.