Since color for that pixel can come in a variety of bit formats we have to make sure Ernesto at least declared his format type.
I'm not sure how much you know about bit operations so don't be angry if I explain the basics first.
We know the maximum storage is a 32-bits since he uses an int (uint32 actually).
So that's why the mask looks like 0xff000000 because one hex digit is 4 bits so we have 8 digits to represent the total 32-bits.
It looks like Ernesto wants a byte per color and the opacity. A byte is an 8-bit hex so you can have values from [0,1,2,3..255] or if written in hex [0x00, 0x01,0x02,..0xff].
Wait a minute...
[No really! You should wait exactly 1 full minute before reading below]
Hey that's exactly like ccColor4B!
We know how to use ccColor4B. Let's hack at that a while to understand these bits.
// full opacity
ccColor4B c = {0,0,0,255};
// We can do this all in one statement but let's make it less confusing.
// we cast the color to uint32 and use some print flags to print hex in NSLog.
uint32_t *ptrToColor = (uint32_t*)(&c);
NSLog(@"%08x",*ptrToColor);
Output for 255 opacity: ff000000
Output for 128 opacity: 80000000
Output for 1 opacity : 01000000
This is great the value for alpha is right where Ernesto said the mask should capture the value.
Let's check out red.
Output for 255 r: 000000ff
Output for 128 r: 00000080
Output for 1 r : 00000001
Awesome.. so red seems to also be in the end of the mask
Hmm... now green
Output for 255 g: 0000ff00
Output for 128 g: 00008000
Output for 1 g : 00000100
I think it's starting to make sense. I'm going to assume blue is right before green since it's the last sport left :).
But the next question is "why is it reversed"?
Well it's about endianness of the byte and our machine is little endian.
http://en.wikipedia.org/wiki/Endianness
So I want to grab out the color value huh...
So let's say i have green at full opacity and want that color value (should be 255).
We mask it yeah! (hope you understood all the info above to use the right mask).
but we get....65280!
That's over 255!
I can't store that into a byte!
Well now what?...
There must be some way to move the bits residing in the green mask 0x0000ff00 to the range [0..255] (which is 0x000000ff).
I'll leave that for a later question since this post is getting too long.
I think that's it. Well I hope this cleared up at least something for you or at the very least it enabled you to dig further and understand bit operations more.
I was dealing with all these headaches too when I created my set/get pixel operations on textures. I posted about it at:
http://www.cocos2d-iphone.org/forum/topic/2449