have scene this in programs a fair bit with using colours to do effects with in for loops.
I know that it is used for limiting.
I wouldnt mind knowing is what is doing, and what would be an equivalent to do it the long way ( i guess ) with if's.
a few examples.
x and 255
y and 255
col and $ff
col and $ff00
col and $ffff00
col=pal( texture(x,y)) and 255
whilst im online, say I have a colour palette that is 640. is it as simple as:
col=pal( image(x,y)) and 639?
thanks tons,
St0rm Tr0oper.
"
x AND
mask" keeps all bits of x where mask is 1 and clears the others.
In case of '255', or 'FF' in hex, or '11111111' in binary, the lower 8 bits are kept, all other become zero.
It's the same as a "modulo 256" but is magnitudes faster because the modulo-operation requires a division.
In image-processing you'll usually have the r,g,b,a-values of a pixel stored in a single integer:
AAAAAAAA:RRRRRRRR:GGGGGGGG:BBBBBBBBTo extract a single color-component you can remove the others by anding with $000000FF, $0000FF00, $00FF0000 or $FF000000.
(but keep in mind that your value is still "shifted up").
Quotesay I have a colour palette that is 640. is it as simple as: col=pal( image(x,y)) and 639?
No, it just works with powers of two (you can only keep "whole" bits).
So just make your palette 512 or 1024 entries long and it works again.
thankyou very much for that.
I want to extract the red green and blue in values of 0-255 for each.
Im not sure on which 'SHR w/ AND value' to use. :(
color=src_buffer[ index ]
r=((col shr 16 ) and &hff)
g=((col shr 8 ) and &hff00)
b=((col shr 0 ) and &hffff00)
r_bytes[ index ]=r
..ect.
Thanksyou.
Each color-component occupies one byte.
You're now shifting it down to be in the lowest byte so all you need to do is to clear the upper bytes:
r=(col shr 16 ) and &hff
g=(col shr 8 ) and &hff
b=col and &hff
The other way round works, too:
r=(col and &hff0000) shr 16
g=(col and &hff00) shr 8
b=col and &hff
Let's say your 32 bit integer looks like this, as chunks of 8 bits:
A:R:G:B
Each of those 8 bits looks like this in binary
11111111000000000000000000000000
00000000111111110000000000000000
00000000000000001111111100000000
00000000000000000000000011111111
I'm calling these 'masks'
In hex, those numbers are
0xff000000
0x00ff0000
0x0000ff00
0x000000ff
So, you can isolate the colour components by ANDing those binary masks with the original integer.
The truth table for AND is:
0 AND 0 is 0
0 AND 1 is 0
1 AND 0 is 0
1 AND 1 is 1
For any bits in the mask which are 1 you will see the original bits from the colour, and any place where the mask is 0, all the original bits will end up 0.
So, there are 2 ways to get the numbers you want:
You can mask using the above masks to isolate the bits you want, then shift the 0s off the end:
a=(pix&0xff000000)>>24;
r=(pix&0x00ff0000)>>16;
g=(pix&0x0000ff00)>>8;
b=(pix&0x000000ff);
Or you can shift out all the bits you don't want, and just mask out the lower 8 bits:
a=(pix>>24)&0x000000ff;
r=(pix>>16)&0x000000ff;
g=(pix>>8)&0x000000ff;
b=(pix)&0x000000ff;
The results are both the same.
Jim
<edit: apologies for all the edits, something inside smf has broken badly and some text can't be posted >
It's not a problem with SMF Jim, it's a problem with software on the server.
Mod_Security thinks that something malicious is being posted, even though it isn't.
The solution aint pretty. Xilo are very security conscious and would probably not remove it, the thought of moving the site to a new and unproven host scares the bejesus out of me.
It's OK, I had to use 'is' instead of '=' after the AND. It must think I'm trying to inject some SQL :)
Jim
thanks all for the feedback and your wisdom.
not to do with this topic, but Im not able to give karma.
You need to make 25 posts before you can give Karma points.
Jim
maybe a bit silly, but when dealing with colours even if not going to dabble with the alpha channel, still use argb / 0xff001122
FWIW, I don't think that's silly at all - even if you're not using the alpha channel right now, the best default for it is 0xff (solid).
Jim