Quote:
Originally Posted by DarK TeaM PT
you sure it is made by you?...
heres a code that i found on my server
pawn Код:
stock RGB(red, green, blue, alpha) { /* Combines a color and returns it, so it can be used in functions. @red: Amount of red color. @green: Amount of green color. @blue: Amount of blue color. @alpha: Amount of alpha transparency.
-Returns: A integer with the combined color. */ return (red * 16777216) + (green * 65536) + (blue * 256) + alpha; }
i know its slower and all but it calculates the same method as you so...
|
Yes i am sure, however i know im not the first by any means. There are tons of implementations floating around. Just so you dont think i am trolling, i will explain it. First, i would like to explain that the RGBAToInt1 is pretty much the same as RGBAToInt2 (all of them are really), the only difference is it doesnt use the binary operators.
Lets take a look at both
Код:
R << 24
G << 16
B << 8
A
and
Код:
R * 16777216
G * 65536
B * 256
A
Binary arithmetic shifts are the same as multiplying by the power of the shift. So:
Код:
R << 24
=
R * 2^24
=
R * 16777216
//and
G << 16
=
G * 2^16
=
G * 65536
//and
B << 8
=
B * 2^8
=
256
From this you can see how different implementations are possible. Why are we even doing this though, and why are all the powers always incremented by 8? Well, the reason we're doing this is because we want to pretty much acheive the same thing as
THIS (Second code block) we're storing four 8-bit integers (0-255) inside of this single variable (we can fit them all because pawn has 32 bits available for storage, and guess what 8*4 is :P). This is why we are multiplying and/or shifting our number over 8 bits. lets take the color "100 0 255" as an example and see how the function handles it/break it down into binary.
Код:
(100 * 16777216) + (0 * 65536) + (255 * 256) + 0 //alpha is out of the picture
for now
//which equals
1677721600 + 0 + 65280 + 0
//which equals
1677786880 //which is correct
Now lets take a look at the binary side (its a LOT clearer)
Код:
0b01100100000000000000000000000000 //31,30, and 27 turned on (1677721600)
+
0
+
0b00000000000000001111111100000000 //9-16 bits turned on (65280)
+
0
=
0b01100100000000001111111100000000 //which is (1677786880)
Notice how each value (RGBA) has 8bits? lets look at the number 1677786880 with colors
01100100000000001111111100000000
if you convert those binary values, you'll get the original results (red=100,green=0,blue=255,alpha=0).Thats pretty much all there is to this really. If your wondering "why 8bit?" its because the color depth we are using is 8bit. There are higher options for color depth, but we cant store them all in pawn (work out r=255 g=255 b=255 a=255 and you'll see why).
I can go into a bit more detail, but i think its best to keep it simple (dont want a mile long post here).
Quote:
Originally Posted by Nero_3D
good but even DracoBlue released a HexToInt function in his DUtils (ofc it contains other useful functions)
But with his function you can insert strings
pawn Код:
/** * Return the value of an hex-string * @param string */ stock HexToInt(string[]) { if (string[0]==0) return 0; new i; new cur=1; new res=0; for (i=strlen(string);i>0;i--) { if (string[i-1]<58) res=res+cur*(string[i-1]-48); else res=res+cur*(string[i-1]-65+10); cur=cur*16; } return res; }
|
That has nothing to do with my defines at all lol, i guess you dont get it :\.