[STK] CvarToColor

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

[STK] CvarToColor

Post by Archive-Poster »

.: Description :.
I saw many plugins where coders getting colors from cvar, but some coders dont know how to do that, so i dedicated to make this simple stock
.: Usage :.

Code: Select all

    new szColorCvar[ 10 ], iColor[ 3 ];
    get_pcvar_string( g_Cvar_mycolors, szColorCvar, charsmax( szColorCvar ) );
    
    CvarToColor( szColorCvar, iColor );

    /*******
     *  Output:
     *     
     *     iColor[ 0 ] - RED
     *     iColor[ 1 ] - GREEN
     *     iColor[ 2 ] - BLUE
     *******/ 
.: Example :.

Code: Select all

Cvar        | Stock
255103204   | 255, 103, 204
255 0 100   | 255, 0, 100
000 020 255 | 0, 20, 255
.: Stock :.

Code: Select all

/*******
 *  Example usage:
 *
 *     new szColorCvar[ 10 ], iColor[ 3 ];
 *     get_pcvar_string( g_Cvar_mycolors, szColorCvar, charsmax( szColorCvar ) );
 *
 *     CvarToColor( szColorCvar, iColor );
*******
 *  Output:
 *     
 *     iColor[ 0 ] - RED
 *     iColor[ 1 ] - GREEN
 *     iColor[ 2 ] - BLUE
 *******/
stock CvarToColor( szColor[ 13 ], iColorsOut[ 3 ] ) {
    if( ContainSpaces( szColor ) ) {
        new szRGB[ 3 ][ 4 ];
        parse( szColor, szRGB[ 0 ], 3, szRGB[ 1 ], 3, szRGB[ 2 ], 3 );
        
        iColorsOut[ 0 ] = clamp( str_to_num( szRGB[ 0 ] ), 0, 255 );
        iColorsOut[ 1 ] = clamp( str_to_num( szRGB[ 1 ] ), 0, 255 );
        iColorsOut[ 2 ] = clamp( str_to_num( szRGB[ 2 ] ), 0, 255 );
        
        return 1;
    } else {
        new iColor = str_to_num( szColor );
        
        // Credits goes to jim_yang
        iColorsOut[ 0 ] = clamp( ( iColor / 1000000 ), 0, 255 );
        iColor %= 1000000;
        iColorsOut[ 1 ] = clamp( ( iColor / 1000 ), 0, 255 );
        iColorsOut[ 2 ] = clamp( ( iColor % 1000 ), 0, 255 );
        
        return 1;
    }
    
    return 0;
}

bool:ContainSpaces( szString[ 13 ] ) {
    new iLen = strlen( szString );
    
    for( new i = 0; i < iLen; i++ )
        if( szString[ i ] == ' ' )
            return true;
    
    return false;
} 
Attachments
CvarToColor_EXAMPLE.sma.zip
(864 Bytes) Downloaded 14 times
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: [STK] CvarToColor

Post by Archive-Poster »

I would stick with a particular method as it calls less natives than the spaced one.
Also, you could save clamp native call.

Here a way using 1 native instead of 4 in your example (haven't cound get_pcvar since your code doesn't integrate it

Code: Select all

stock clamp_byte( iByte )
{
    if( iByte < 0 )
    {
        return 0
    }
    else if( iByte > 0xFF )
    {
        return 0xFF
    }
    return iByte
}

stock CvarPointerToColor(pPointer)
{
    static szColors[10], iTemp, iColor[3]
    get_pcvar_string(pPointer, szColors, charsmax(szColors))
    iTemp = str_to_num(szColors)

    iColor[0] = clamp_byte( iTemp / 1000000 )
    iTemp %= 1000000
    iColor[1] = clamp_byte( iTemp / 1000 )
    iColor[Blue] = clamp_byte( iTemp % 1000 )

    return iColor
} 
Then you use it easily :

Code: Select all

new g_pCvarColor

public plugin_init()
{
    g_pCvarColor = register_cvar("my_color_cvar", "255180030")
}

public function_using_color()
{
    new iColors[3] = CvarPointerToColor( g_pCvarColor )
} 
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: [STK] CvarToColor

Post by Archive-Poster »

what is the advantage to use 0xFF instead of 255 ? 255 is more readable and 'understable'.
It doesn't anwser to the question to know the advantage of the hex notation instead of classic but well known integer. ( more friendly for a tuto )
I don't even see a reason for the function. What's wrong with clamp(num, 0, 0xFF); / clamp(num, 0, 255); ?
clamp is a native, so it's faster if you don't use it.
I think that I would make it more like the get_(p)cvar_* commands (for continuity ).

Code: Select all

get_cvar_color(pointer, iColor[3])
and

Code: Select all

get_pcvar_color(pointer, iColor[3])
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: [STK] CvarToColor

Post by Archive-Poster »

... You earned 0.01 seconds by using an ugly stock. You are teh win. Here's your nobelnothing price.

Prove it.
code

Code: Select all

#include <amxmodx>

public plugin_init()
{
    register_plugin("clamp profile", "0.1", "stupok")
    
    new iValue = 256
    
    // pawn native function clamp
    for( new i = 0; i < 10000000; i++ )
    {
        clamp( iValue, 0, 255 )
    }
    
    // extra function clamp
    for( new i = 0; i < 10000000; i++ )
    {
        clamp_byte( iValue )
    }
    
    server_cmd("exit")
}

stock clamp_byte( iByte )
{
    if( iByte < 0 )
    {
        return 0
    }
    else if( iByte > 0xFF )
    {
        return 0xFF
    }
    return iByte
} 
3 profiles

Code: Select all

date: Fri Jun 26 11:49:29 2009 map: de_dust2
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000002 / 0.000002 / 0.000002
   n |                            clamp |   10000000 | 0.613344 / 0.000000 / 0.000227
   n |                       server_cmd |          1 | 0.000003 / 0.000003 / 0.000003
   p |                      plugin_init |          1 | 1.542432 / 1.542432 / 1.542432
   f |                       clamp_byte |   10000000 | 0.725329 / 0.000000 / 0.000520
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Fri Jun 26 11:49:41 2009 map: de_dust2
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000001 / 0.000001 / 0.000001
   n |                            clamp |   10000000 | 0.613077 / 0.000000 / 0.000226
   n |                       server_cmd |          1 | 0.000003 / 0.000003 / 0.000003
   p |                      plugin_init |          1 | 1.546259 / 1.546259 / 1.546259
   f |                       clamp_byte |   10000000 | 0.726270 / 0.000000 / 0.000585
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Fri Jun 26 11:49:54 2009 map: de_dust2
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000002 / 0.000002 / 0.000002
   n |                            clamp |   10000000 | 0.614865 / 0.000000 / 0.000599
   n |                       server_cmd |          1 | 0.000003 / 0.000003 / 0.000003
   p |                      plugin_init |          1 | 1.538301 / 1.538301 / 1.538301
   f |                       clamp_byte |   10000000 | 0.738247 / 0.000000 / 0.000571
0 natives, 0 public callbacks, 1 function calls were not executed.
clamp() average time: 0.613762 seconds
clamp_byte() average time: 0.729948 seconds

In this test, clamp() is faster than clamp_byte(), on average, by: 0.116186 seconds
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: [STK] CvarToColor

Post by Archive-Poster »

Here's a simple decimal to binary/hex converter.
itoa works with any radix. atoi works only with Hex and Binary.

Code: Select all

#define Hex  16
#define Binary 2
 
new map[] = "0123456789abcdefghijklmnopqrstuvwxyz"
 
stock itoa(val, out[], len, radix) {
 
 for ( new i = len - 1 ; val && i ; --i, val /= radix )
  out[len-i-1] = map[val % radix];
 
 reverse(out);
}
 
stock reverse(string[]) {
 
 new len = strlen(string);
 new temp;
 
 for ( new i = 0 ; i < len / 2 ; i++ ) {
  temp = string[i];
  string[i] = string[len-i-1];
  string[len-i-1] = temp;
 }
}
 
#define ctod(%0) ('0' <= %0 <= '9' ? %0 - '0' : 'A' <= %0 <= 'Z' ? %0 -'A' + 10 : 'a' <= %0 <= 'z' ? %0 -'a' + 10 : 0)
 
stock atoi(hexstring[], radix) {
 
 new radix_, result, firsttime = false, len = strlen(hexstring);
 
 switch ( radix ) {
  case 16 : radix_ = 4;
  case 2 : radix_ = 1;
  default : radix_ = 0;
 }
 
 for ( new i = 0 ; i < len ; i++ ) {
 
  if ( ! firsttime )
   result <<= radix_;
 
  result += ctod(hexstring[i]);
 
  firsttime = false;
 }
 
 return result;
} 
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: [STK] CvarToColor

Post by Archive-Poster »

If the purpose is to convert Hex value to RGB or RGB to Hex, it doesn't work as expected.
It sure as hell does.

And you can't pass a string like "255 255 255" you have to split them.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest