Search code examples
colorsrgbpalette

Programatically step through 24 bit color spectrum, get x amount of average colors


Yes, I AM trying to re-invent the wheel. :-) . I am doing my own image compression, (testing some ideas for transmitting parts of images over tcp). Anyway... I am trying to step through 24 bit rgb color, get a complete linear range, and (step through) that range at x intervals.

I am trying to get the average 99 colors over the complete spectrum. (24bit / 99) = 167488.6363636364 , so at 16K interval I want to pic a color for my 99 color palette.

I am having trouble understanding how RGB really works... It seems the is NO linear range..., or is there...?

I am currently doing the following:

var_interval = (255 * 255 * 255) / 99

 For r = 0 To 255
     For g = 0 To 255
          For b = 0 To 255

                    If var_counter = var_interval Then
                    objWriter.Write(r & "," & g & "," & b)

                    End If

                    var_counter += 1
           Next
     Next
 Next

I am getting my colors, but this step does not generate "scaling" colors if you will.

Any ideas?


Solution

  • There certainly is a way to iterate through the color spectrum, but whether you go this approach is your own choice :). You can make use of the HSL color space (Hue, saturation, and lightness) instead of RGB (red, green, blue). The hue represents which color (ranging from 0 to 360), the saturation is how much of that color your want (0 to 100), and the lightness is how bright you want it.

    So, to answer your question, you can take 360/99 as your sampling rate from the hue, choose a consistent value and intensity that you'd want and then convert that to RGB space to display it. For the conversion see: http://web2.clarkson.edu/class/image_process/RGB_to_HSI.pdf

    and for information on HSL color space see: http://en.wikipedia.org/wiki/HSL_and_HSV