PDA

View Full Version : setting samp hex color


Tanush123
01/01/2012, 08:38 AM
Fixed...

fangoth1
01/01/2012, 08:43 AM
well hex codes loook like this in a string


SendClientMessageToAll(COLOR_GREEN, {0000FF}Welcome to the server);//welcome will be a different color the rest will be green

Tanush123
01/01/2012, 08:47 AM
I mean for sa-mp hex colors, i am making dynamic faction so i wanna set a samp hex color in game but when i do for example
/setocol [orgid] 0xFF0000FF
On my mysql it shows

THE_KNOWN
01/01/2012, 10:56 AM
if(sscanf(params,"ix",orgid,ohex)) return SendClientMessage(playerid,grey,"USAGE: /setocol [orgid] [SA-MP HEX]");

Tanush123
01/01/2012, 09:18 PM
That doesn't do it, is it problem with my mysql because for Hex i put Char

LiamM
01/01/2012, 10:45 PM
I made a post today too, having problems with MySQL and the hex my self bro, its a pain in the arse.

Tanush123
01/01/2012, 11:12 PM
Liam aren't you from IvB?

Tanush123
02/01/2012, 08:41 AM
bump

Dark_Kostas
02/01/2012, 08:58 AM
That doesn't do it, is it problem with my mysql because for Hex i put Char

Do you mean that the column is defined as Char? Then yes.

LiamM
02/01/2012, 10:04 AM
Liam aren't you from IvB?

Cant be bro, I dont even know what IvP is :P

Tanush123
04/01/2012, 05:51 AM
bump

coole210
04/01/2012, 06:16 AM
I've saved hex colors to DB before, let me check how I did it.

Ok, I've figured it out, hex IS a string, but you need these functions:

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;
}

stock IntToHex(number)
{
new m=1;
new depth=0;
while (number>=m) {
m = m*16;
depth++;
}
depth--;
new str[256];
for (new i = depth; i >= 0; i--)
{
str[i] = ( number & 0x0F) + 0x30; // + (tmp > 9 ? 0x07 : 0x00)
str[i] += (str[i] > '9') ? 0x07 : 0x00;
number >>= 4;
}
if(strlen(str) == 0)strins(str,"00",0);
else
if(strlen(str) == 1)strins(str,"0",0);
str[8] = '\0';
return str;
}

Use appropriately.

Steven82
04/01/2012, 06:20 AM
I've saved hex colors to DB before, let me check how I did it.

Ok, I've figured it out, hex IS a string, but you need these functions:

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;
}

stock IntToHex(number)
{
new m=1;
new depth=0;
while (number>=m) {
m = m*16;
depth++;
}
depth--;
new str[256];
for (new i = depth; i >= 0; i--)
{
str[i] = ( number & 0x0F) + 0x30; // + (tmp > 9 ? 0x07 : 0x00)
str[i] += (str[i] > '9') ? 0x07 : 0x00;
number >>= 4;
}
if(strlen(str) == 0)strins(str,"00",0);
else
if(strlen(str) == 1)strins(str,"0",0);
str[8] = '\0';
return str;
}

Use appropriately.

Are those in the useful functions section? If not, they need to be :P

AKA_Cross
04/01/2012, 06:41 AM
Your best bet is to store it on the mysql database as a string then read it with hexstr

stock hexstr(string[])
{
new
ret,
val,
i;
if (string[0] == '0' && string[1] | 0x20 == 'x') i = 2;
while (string[i])
{
ret <<= 4;
val = string[i++] - '0';
if (val > 0x09) val -= 0x07;
if (val > 0x0F) val -= 0x20;
if (val < 0x01) continue;
if (val < 0x10) ret += val;
}
return ret;
}

Thank ****** for that from y_utils.inc

edit: coole210 got it didn't notice his post

BigETI
04/01/2012, 06:46 AM
Why don't you simply save the hex values as integer (as a non string value)?

Tanush123
04/01/2012, 04:25 PM
Your best bet is to store it on the mysql database as a string then read it with hexstr

stock hexstr(string[])
{
new
ret,
val,
i;
if (string[0] == '0' && string[1] | 0x20 == 'x') i = 2;
while (string[i])
{
ret <<= 4;
val = string[i++] - '0';
if (val > 0x09) val -= 0x07;
if (val > 0x0F) val -= 0x20;
if (val < 0x01) continue;
if (val < 0x10) ret += val;
}
return ret;
}

Thank ****** for that from y_utils.inc

edit: coole210 got it didn't notice his post

i got 3 same errors, this is one of the line
if(sscanf(params,"is",orgid,hexstr))
errorrD:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5595) : error 076: syntax error in the expression, or invalid function call

Vince
04/01/2012, 04:36 PM
Why don't you simply save the hex values as integer (as a non string value)?
This. Since hex values are still just numbers, they can be saved as integer in MySQL.

These two statements do exactly the same thing:

SetPlayerColor(playerid, 0xFF0000FF);
SetPlayerColor(playerid, -16776961);

Tanush123
04/01/2012, 05:59 PM
This. Since hex values are still just numbers, they can be saved as integer in MySQL.

These two statements do exactly the same thing:

SetPlayerColor(playerid, 0xFF0000FF);
SetPlayerColor(playerid, -16776961);


can you try helping please?

Tanush123
05/01/2012, 06:08 PM
bump

Jefff
05/01/2012, 06:54 PM
Change Your char variable to INT or varchar
http://www.roseindia.net/sql/mysql-alter/mysql-alter-column-datatype.shtml

Tanush123
05/01/2012, 07:09 PM
i changed to varchar, and if i do Int wont that not work because for samp hex colors it has "x" on the second letter

Kyosaur
05/01/2012, 08:32 PM
Like its been said before, there is absolutely no reason to store your value as a string and go through the expensive process of converting it. I suggest instead of using hexadecimal you use RGB values instead. This is MUCH easier for users to work with, and is just as easy to store with my macro:

http://forum.sa-mp.com/showpost.php?p=856837&postcount=1190

Using RGB allows the usage of decimal so its much more natural. Simple have 3 parameters: Red, Green, and Blue that range in value of 0-255. So for the color red in RGB the use would specify 255 0 0 (exactly the same as 0xFF0000) and use my macro to convert the 3 into an integer you can store.

Tanush123
05/01/2012, 08:36 PM
Like its been said before, there is absolutely no reason to store your value as a string and go through the expensive process of converting it. I suggest instead of using hexadecimal you use RGB values instead. This is MUCH easier for users to work with, and is just as easy to store with my macro:

http://forum.sa-mp.com/showpost.php?p=856837&postcount=1190

Using RGB allows the usage of decimal so its much more natural. Simple have 3 parameters: Red, Green, and Blue that range in value of 0-255. So for the color red in RGB the use would specify 255 0 0 (exactly the same as 0xFF0000) and use my macro to convert the 3 into an integer you can store.

Well can you try to edit my /setocol to that?

Kyosaur
05/01/2012, 08:53 PM
Well can you try to edit my /setocol to that?

Just add 3 integer specifiers to your sscanf statement, check if the 3 values are between 0-255, and shove them in my macro. Then you can do what ever you want with the number it gives you.

Why dont you give it a shot, and show me what you come up with.

Tanush123
05/01/2012, 09:03 PM
im soo noob lol, error
D:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5823) : error 017: undefined symbol "RGBAToInt"
D:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5824) : error 017: undefined symbol "RGBAToInt"
D:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5825) : error 017: undefined symbol "RGBAToInt"
on top i added
#define RGBAToInt(%0,%1,%2,%3) ((16777216 * (%0)) + (65536 * (%1)) + (256 * (%2)) + (%3))

and the lines for error are


new rhex1 = RGBAToInt (1);
new rhex2 = RGBAToInt (2);
new rhex3 = RGBAToInt (3);

Jefff
05/01/2012, 09:35 PM
CMD:setocol(playerid,params[])
{
new hex[9];
if(!IsPlayerAdmin(playerid)) return 0;
if(sscanf(params,"is[9]",orgid,hex)) return SendClientMessage(playerid,grey,"USAGE: /setocol [orgid] [SA-MP HEX 0xFF0000FF]");
CheckMySQL();
format(str,sizeof(str),"SELECT * FROM orgs WHERE Orgid = %d",orgid);
mysql_query(str);
mysql_store_result();
if(mysql_num_rows() != 0)
{
mysql_free_result();
format(str,sizeof(str),"UPDATE orgs SET Hex = '%s'",hex);
mysql_query(str);
format(str,sizeof(str),"You have updated orgid %d hex to %s",orgid,hex);
SendClientMessage(playerid,COLOR_YELLOW,str);
}
else
{
SendClientMessage(playerid,red,"ERROR: This Organization ID is not used.");
}
return 1;
}

Kyosaur
05/01/2012, 10:17 PM
im soo noob lol, error
D:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5823) : error 017: undefined symbol "RGBAToInt"
D:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5824) : error 017: undefined symbol "RGBAToInt"
D:\Users\Tanush\Desktop\SA-MP SERVERS\SA-MP Server Stunt\gamemodes\server.pwn(5825) : error 017: undefined symbol "RGBAToInt"
on top i added
#define RGBAToInt(%0,%1,%2,%3) ((16777216 * (%0)) + (65536 * (%1)) + (256 * (%2)) + (%3))

and the lines for error are


new rhex1 = RGBAToInt (1);
new rhex2 = RGBAToInt (2);
new rhex3 = RGBAToInt (3);


Im not sure where those numbers came from (1,2,3) but the function has 4 parameters. I think you're taking the specifiers in the define to serious, so pretend the declaration is like this instead:


RGBAToInt(red,green,blue,alpha);


Remember to do what i said earlier as well, add 3 integer specifiers to your sscanf statement (for the r, g, and b). For the fourth argument (alpha) just put 255. Your resulting usage should looks similar to this:


new color = RGBAToInt(r, g, b, 255);


Then all you have to do is actually USE the color variable.

Tanush123
05/01/2012, 10:28 PM
CMD:setocol(playerid,params[])
{
new hex[9];
if(!IsPlayerAdmin(playerid)) return 0;
if(sscanf(params,"is[9]",orgid,hex)) return SendClientMessage(playerid,grey,"USAGE: /setocol [orgid] [SA-MP HEX 0xFF0000FF]");
CheckMySQL();
format(str,sizeof(str),"SELECT * FROM orgs WHERE Orgid = %d",orgid);
mysql_query(str);
mysql_store_result();
if(mysql_num_rows() != 0)
{
mysql_free_result();
format(str,sizeof(str),"UPDATE orgs SET Hex = '%s'",hex);
mysql_query(str);
format(str,sizeof(str),"You have updated orgid %d hex to %s",orgid,hex);
SendClientMessage(playerid,COLOR_YELLOW,str);
}
else
{
SendClientMessage(playerid,red,"ERROR: This Organization ID is not used.");
}
return 1;
}

doesnt hex have like 10 letters? 0 x F F 9 9 0 0 A A

Tanush123
05/01/2012, 10:43 PM
Im not sure where those numbers came from (1,2,3) but the function has 4 parameters. I think you're taking the specifiers in the define to serious, so pretend the declaration is like this instead:


RGBAToInt(red,green,blue,alpha);


Remember to do what i said earlier as well, add 3 integer specifiers to your sscanf statement (for the r, g, and b). For the fourth argument (alpha) just put 255. Your resulting usage should looks similar to this:


new color = RGBAToInt(r, g, b, 255);


Then all you have to do is actually USE the color variable.

i added
CMD:setocol(playerid,params[])
{
new r,g,b;
new oohex = RGBAToInt(r,g,b,255);
if(!IsPlayerAdmin(playerid)) return 0;
if(sscanf(params,"iiii",orgid,r,g,b)) return SendClientMessage(playerid,grey,"USAGE: /setocol [orgid] [red 0-255] [green 0-255] [blue 0-255]");
CheckMySQL();
format(str,sizeof(str),"SELECT * FROM orgs WHERE Orgid = %d",orgid);
mysql_query(str);
mysql_store_result();
if(mysql_num_rows() != 0)
{
mysql_free_result();
format(str,sizeof(str),"UPDATE orgs SET Hex = %s WHERE Orgid = '%d'",oohex,orgid);
mysql_query(str);
format(str,sizeof(str),"You have updated orgid %d hex to %s",orgid,oohex);
SendClientMessage(playerid,COLOR_YELLOW,str);
}
else
{
SendClientMessage(playerid,red,"ERROR: This Organization ID is not used.");
}
return 1;
} but in my mysql it shows 0

Kyosaur
05/01/2012, 10:46 PM
Thats because it currently is 0, because you put it before the sscanf statement (where the variable get their values). Also replace all your string specifiers (%s) to integers (%d) in your formats since your now saving an integer. If you havent already do the same for the DB too.

Jefff
05/01/2012, 10:46 PM
doesnt hex have like 10 letters? 0 x F F 9 9 0 0 A A

so change to 11 ?

Tanush123
05/01/2012, 10:51 PM
for my DB do i have to do red = %d,green = %d,blue = %d

Kyosaur
05/01/2012, 10:53 PM
No, you're just storing the integer the macro gives you. Then you're done :D.

BigETI
05/01/2012, 10:53 PM
NOT every Hex value has "10 letters".
Let's take an example on what you can show with hex values and why they're good:
printf("%d is the same as %x. Proof: %d %d / %x %x", 15, 15, 15, 15, 15, 15);
printf("%d is the same as %x. Proof: %d %d / %x %x", 0xF, 0xF, 0xF, 0xF, 0xF, 0xF);

will print:
15 is the same as F. Proof: 15 15 / F F
15 is the same as F. Proof: 15 15 / F F

There are some interresting sites about hex/bin numbers where you can learn from.

0x in our script defines that the next parameter is a hex value.

Example of 255: 0xFF
As we can see there is no fixed amount of letters for a hex value.

The point of hex values are that you can easier read from for bit shifting, color embedding, data compressing etc.

In short they're also numbers like integers so JUST STORE THEM AS INTEGER IN YOUR DATABASE lol.