The PHP imagecolorat() function can be used to get the RGB values of an image pixel, as demonstrated in the documentation:
$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
I don’t understand the last three lines at all; I know that they return the correct values, but I cannot figure out how the >> and & operators work together to do so. Could someone please explain?
For reference, when $rgb = 9750526, the RGB values turn out to be(148,199,254).
Thanks for any responses.
& and >> are two different bitwise operators (Bitwise operators in PHP). They work with the bit representation of the values, which gives us a few useful (and fast!) possibilities.
>> is right shift. If you right shift a value, you’re moving all the bits to the right in the representation. If you have “11100” and right shift it by two places, you’re left with “111” (the bits shifted off on the right end disappears). The number on the right side of >> indicates the number of bits to shift.
& is bitwise and. If you have two values represented as 111000 and 101011, and decide to bitwise and them, you’ll end up with 101000 (the bits where the same bits in both values are set will be 1, otherwise 0).
We now have what we need to make sense of the code above. $rgb contains an integer value (the value is not interesting, but the bit pattern is), which represents 24 bits (32 bits for RGBA, but we’ll ignore that here). The value consists of 8 bits for red, 8 bits for green and 8 bits for blue. We’re however not interested in what number those 24 bits represent together, but what the separate R, G and B values are.
Our value represent the following bits for the colors:
If we shift this bitmask with >> 16, we’ll get:
This is what >> 16 does, and leaves us with just the bits representing the red color. As we’ve now moved the bits all the way down, the value represents a number in [0, 255]. We then add & 255 (which here is written as 0xFF in hexadecimal notation) to remove any stray bits above the 8 we’re interested in. We’ll get to how that happens now, when we look at how we get the G value:
Our value still bravely represents the different colors:
If we shift this bitmask 8 places with >> 8, we’ll get:
But wait! This is not just the bits that represents g, but also the bits that represent r! This is not the value we’re after (as we just want g’s). We were lucky in the previous example since we didn’t have any bits set above r, but this time we’ve really got our work cut out for us. But we know about &, and it’s time to see it actually to it’s stuff.
We now have:
And then we apply & 255 (0xFF in your code), which is representing in a bit value as:
Since & only keeps the bits which are set in both operands, we’ll end up with just the g values:
The bits that represented red in the shifted value are now 0, and we’re left with only the bits representing the green of the original color. As they’ve been shifter into the same 8 bits that represent [0, 255], we’ve got our value yet again.
The last value is easier, as it’s already located where we want it: in the bits representing [0, 255]. We can just do the & operation here, and get out on the other side:
Which ends up with:
And we’ve got our blue value as well.
Hope that explains it!