-
Recent Posts
Categories
@jasonbejot
- @allworknosleep fuuukk, i gotta get some!
- RT @phenomblue: Phenomblue launches OTC Halloween Fun http://post.ly/vG4O
- @allworknosleep where was it at? i haven't seen it in years!
- @k_to_the_t man bubble, fantastic
- i've just traded soreness for numbness, aarrrgg
- hooray for dental work!
Color Programming in Flash
When I first thought about trying to wrap code around color values in Actionscript, years ago, I was super intimidated. I conjured up ideas that it was a world full of complex algorithms and crack-brained theories. That’s really about half true.
If your goal is some crazy results then you’re probably going to be dealing with some crazy algorithms. But otherwise the basics of it are pretty straightforward.
Note that I will not be talking about pixel bender in this post.
Color Theory
I’m not really going to talk about color theory at all. Basicaly because I don’t understand enough of it (yet) to talk in any real depth. Just not that color theory is a huge topic and thankfully you’ll only have to deal with a very tiny little part of it.
I have found that the Colour Space Conversions article can come in handy as it explains just about everything you might want to know about color.
Color in Memory
The foundation of color programming is understanding how a color is represented in code.
A color is made of 3 or more channels, each channel being 8 bits in size representing values from 0 to 255. In memory a color is stored as a single unsigned integer (uint) by arranging the bits of each channel side-by-side. So an RGB color would have a total of 24 bits (3 channels) and look something like this:
No, it’s not a power meter. As you can see the 8 red channel bits are the furthest to the left, then the 8 green bits and finally the 8 blue bits. And no, the bits themselves aren’t red, green or blue, they just represent those colors.
Since the bits are stored side-by-side then the color also has a number value. If all bits in a color are off (zero) then the number value is 0 (which is used to represent black). If all bits are on (one) then the number value depends on how many channels the color has (i.e. 3 channels = 16777215). This max value is used to represent white.
There are two different color spaces in Flash/Flex: RGB and ARGB
RGB is just like the example above with a red channel, green channel and blue channel. This is called a 24 bit color and is pretty standard.
ARGB is like RGB but has an alpha channel. This is called a 32 bit color (because of the extra channel).
Color in Syntax
The most common way for defining a color in code is by using the hex syntax:
Just like a hex code in HTML except the hash (#) is replaces by a 0x. Notice that a 32 bit color has two extra values for the alpha channel.
Since a color is also an integer value you could use integers to define your colors:
Color in Code
Now that we know how colors are stored, lets talk about how to tear a color apart to extract it’s different channels. Since the channels are stored side-by-side in an unsigned integer we have to do some bitwise operations to get their values.
If you don’t know what bitwise operations are I would highly advise looking into them. They’re kinda confusing at first but they can be a huge boost to your code.
Here’s the code for grabbing channel values from a color:
The method is this:
The resulting channel values will be between 0 and 255.
Now that we can get channel values, how do we stitch them back together to form a cohesive color? More bitwise action!
The method:
Conclusion
That’s about it for the basics. There’s a bajillion things you can do once you know how to tear apart and restitch colors.