Hexadecimal colors in my script? (and DB)
#1

Hello...

I can trying to save my color into the database, then retrieve it again when i login...this is how i load the Color..

The problem is that the color is saving like "FF0000" in the Database, but then wen i use "SetPlayerColor(playerid, orgStats[orgID][color]" The FF0000 which should be RED in Hex...is green...

I think it might either be becuase of the strings or the field type :S anyone can help?

pawn Код:
mysql_fetch_field_row(orgStats[i][fColor], "Color");
This is how i save it:
pawn Код:
format(query, sizeof(query), "update organisation SET Color='%s' WHERE ID='%d'",orgStats[i][fColor], i);
        mysql_query(query);
This is how i set it:

pawn Код:
if(sscanf(params, "dh", org, color)) return SendClientMessage(playerid, COLOR_RED, "[USAGE] - /setcolor [orgID] [color in HEX]");
   
    orgStats[org][fColor] = color;
The Data type in my Mysql Database is a string...
Reply
#2

Color is a number (integer), not a string.
Reply
#3

Yes, but when you are using HEX, i KNOW its a numeric System, but wasnt sure if Hex would be allowed as an Int..
Reply
#4

As stated above, colors are integers, that are usually represented in hex form.

Solving your problem depends on how you are setting the color variables, be it from a string retrieved from a command, or a #define, or whatever.

you can convert a hex string to integer using sscanf2
https://sampforum.blast.hk/showthread.php?tid=120356
pawn Код:
CMD:testcolor(playerid,params[])
{
     new color;
     if(!sscanf(params,"h",color))
     {
           ....
     }
}
I'm not sure if sscanf2 checks for the correct samp hex color format ( I presume it doesn't ) so if you are retrieving the colors from a command, I would check the format with something like this.
pawn Код:
if(strlen(params) == 10 && !strcmp(tmp,"0x",true,2)) //success
[edit] oops I just saw you already knew this, oh well good luck.
Reply
#5

If i convert Hex (IE FF0000) to a Int. (16711680) would i be able to use 16711680 like:

Код:
SendClientMessage(playerid, 16711680, "I am f**king awesome ");
EDIT:
Nevermind, it doesnt
Reply
#6

Bump

-I still cant get this to work
Reply
#7

This will help you
pawn Код:
forward RGBAToHex(r, g, b, a); //By Betamaster
forward HexToRGBA(colour, &r, &g, &b, &a); //By Betamaster

public RGBAToHex(r, g, b, a) //By Betamaster
{
    return (r<<24 | g<<16 | b<<8 | a);
}

public HexToRGBA(colour, &r, &g, &b, &a) //By Betamaster
{
    r = (colour >> 24) & 0xFF;
    g = (colour >> 16) & 0xFF;
    b = (colour >> 8) & 0xFF;
    a = colour & 0xFF;
}
or
pawn Код:
stock hexstr(string[]) // By ******
{
    new
        ret,
        val,
        i;
    if (string[0] == '0' && (string[1] == 'x' || string[1] == '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;
}
Reply
#8

I do think you can actually save the color as an int and use that number the same you would with a hex value. Its worked for me on a few scripts.
Reply
#9

Quote:
Originally Posted by Voldemort
Посмотреть сообщение
This will help you
pawn Код:
forward RGBAToHex(r, g, b, a); //By Betamaster
forward HexToRGBA(colour, &r, &g, &b, &a); //By Betamaster

public RGBAToHex(r, g, b, a) //By Betamaster
{
    return (r<<24 | g<<16 | b<<8 | a);
}

public HexToRGBA(colour, &r, &g, &b, &a) //By Betamaster
{
    r = (colour >> 24) & 0xFF;
    g = (colour >> 16) & 0xFF;
    b = (colour >> 8) & 0xFF;
    a = colour & 0xFF;
}
or
pawn Код:
stock hexstr(string[]) // By ******
{
    new
        ret,
        val,
        i;
    if (string[0] == '0' && (string[1] == 'x' || string[1] == '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;
}
Yeah, didnt realise ****** had that..

How would i store it in my database? As a string, then use this before i use it??

Also, do i include "0x" or "#" at the start of the string? so like:

0xFF0000
or
#FF0000

for Red
Reply
#10

Quote:
Originally Posted by DarrenReeder
Посмотреть сообщение
If i convert Hex (IE FF0000) to a Int. (16711680) would i be able to use 16711680 like:

Код:
SendClientMessage(playerid, 16711680, "I am f**king awesome ");
EDIT:
Nevermind, it doesnt
This will work, but red in sa-mp is FF000000 (4278190080) not FF0000 (16711680)

If you use 4278190080, you will get a message with red color.

pawn Код:
SendClientMessage(playerid, 4278190080, "I am f**king awesome ");
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)