SetObjectMaterial Color problem..
#1

Hello,

I just cannot get it working, I tried many different converts but everything fails.
Right now I have the following;

I wan't players to enter a Hex color, and my script should convert it to a ARGB format for SetObjectMaterial.
So this is what I do,
I have this functions:
Код:
stock ARGB( alpha, red, green, blue)
{
	return alpha + (red * 16777216) + (green * 65536) + (blue * 256);
}
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;
}
Then i use the following line to convert the color to this ARGB format:
Код:
ARGB(HexToInt(alpha), HexToInt(red), HexToInt(green), HexToInt(blue))
I will show you some print lines;
The users input is: 0xFF0000FF (which is red)
The following code give me this result: FFFF0000
Код:
printf("%s%s%s%s",alpha,red,green,blue);
Now the ARGB converter gives me the following result: -16776961
And the material becomes blue instead of red.
So it seems i have to switch blue as red.
I tried that on different ways but it always becomes blue for some reason.
Someone know how to deal with this?
Reply
#2

I reckon this should convert RGBA to ARGB. As for validating the input , try sscanf.
pawn Код:
ARGB(color_rgba) //0xaa99ccff
{
    new alpha = (color_rgba & 0xff) << 24;
    new rgb = color_rgba >>> 8;
   
    return alpha | rgb;
}
Reply
#3

Ye ok, but not sure what I have to do with this, Because i allow users to insert different formats, those formats get converted to A R G B, and then I have to convert this A R G B into a integer, so I can save it.
Now the problem is that it only works for RGBA but for osme reason it doesnt work when I switch it to ARGB...
I think it has something to do with the ARGB stock that i showed in the first post, maybe those intergers are not correct?
Reply
#4

I re-read your threads many times but I couldn't understand what exactly you wanted to do.

Let's say a player types 0xFF0000FF like your example which is red, then you get the integer value of that hex (from the string) when you use HexToInt function.

However, if you want to get the A R G B values from the hex (for example: A R G B - 255 255 0 0), I edited the function you used in order to get R G B A values from the hex as a string and stores the values on them by reference. Then you can call ARGB with the values and get the integer value of it.

pawn Код:
public OnFilterScriptInit( )
{
    new
        red,
        green,
        blue,
        alpha,
        color
    ;
    HexStrToRGBA( "0xFF0000FF", red, green, blue, alpha );
    printf( "R: %d, G: %d, B: %d, A: %d", red, green, blue, alpha );
    color = ARGB( alpha, red, green, blue );
    printf( "color: %d", color );
    return 1;
}

stock HexStrToRGBA( string[ ], &red, &green, &blue, &alpha )
{
    if( !strlen( string ) ) return 0;
   
    new
        cur,
        res,
        str[ 4 ][ 3 ]
    ;
   
    strmid( str[ 0 ], string, 2, 5, 3 );
    strmid( str[ 1 ], string, 4, 7, 3 );
    strmid( str[ 2 ], string, 6, 9, 3 );
    strmid( str[ 3 ], string, 8, 11, 3 );
   
    for( new j = 0; j < 4; j++ )
    {
        cur = 1;
        res = 0;
           
        for( new i = 2; i > 0; i-- )
        {
            if( str[ j ][ i - 1 ] < 58 ) res = res + cur * ( str[ j ][ i - 1 ] - 48 );
            else res = res + cur * ( str[ j ][ i - 1 ] - 65 + 10 );
            cur = cur * 16;
        }
       
        switch( j )
        {
            case 0: red = res;
            case 1: green = res;
            case 2: blue = res;
            case 3: alpha = res;
        }
    }
    return 1;
}

stock ARGB( alpha, red, green, blue)
{
    return alpha + ( red * 16777216 ) + ( green * 65536 ) + ( blue * 256 );
}
pawn Код:
// Inserting: 0xFF0000FF

// Output from the above:
R: 255, G: 0, B: 0, A: 255
color: -16776961

// Output from HexToInt:
-16776961
If you do not want to get the values of RGBA, then use HexToInt instead.
Reply
#5

Thanks, and yes that is working, but thats not all I need.
For some reason I just can't get it work.
To get clear I need to following;
ARGB - For Material Color
RGBA - For Textdraw Box Color
And I got the following data:
Alpha, Red, Green, Blue (in integers).
Now I need to put those 4 intergers into two integers, one ARGB and one RGBA.
When I read over it like this it seems easy, but i Just can't get it done, everytime one of the two isn't working....
Reply
#6

It says that if you use a color for materials and you use hex, it needs to be in ARGB format; however, if you use it as an integer is the exactly the same thing as RGBA. It just adds the R G B A values. So doing R+G+B+A is the same as A+R+G+B, nothing changes.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)