AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [STK] CvarToColor (https://forums.alliedmods.net/showthread.php?t=95489)

xPaw 06-24-2009 04:48

[STK] CvarToColor
 
2 Attachment(s)
CvarToColor

.: 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 :.
PHP Code:

    new szColorCvar10 ], iColor];
    
get_pcvar_stringg_Cvar_mycolorsszColorCvarcharsmaxszColorCvar ) );
    
    
CvarToColorszColorCvariColor );

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

.: Example :.
PHP Code:

Cvar        Stock
255103204   
255103204
255 0 100   
2550100
000 020 255 
020255 

.: Stock :.
PHP Code:

/*******
 *  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 CvarToColorszColor13 ], iColorsOut] ) {
    if( 
ContainSpacesszColor ) ) {
        new 
szRGB][ ];
        
parseszColorszRGB], 3szRGB], 3szRGB], );
        
        
iColorsOut] = clampstr_to_numszRGB] ), 0255 );
        
iColorsOut] = clampstr_to_numszRGB] ), 0255 );
        
iColorsOut] = clampstr_to_numszRGB] ), 0255 );
        
        return 
1;
    } else {
        new 
iColor str_to_numszColor );
        
        
// Credits goes to jim_yang
        
iColorsOut] = clamp( ( iColor 1000000 ), 0255 );
        
iColor %= 1000000;
        
iColorsOut] = clamp( ( iColor 1000 ), 0255 );
        
iColorsOut] = clamp( ( iColor 1000 ), 0255 );
        
        return 
1;
    }
    
    return 
0;
}

bool:ContainSpacesszString13 ] ) {
    new 
iLen strlenszString );
    
    for( new 
0iLeni++ )
        if( 
szString] == ' ' )
            return 
true;
    
    return 
false;



zacky 06-24-2009 04:58

Re: CvarToColor( )
 
Nice xpaw. +karma

alan_el_more 06-24-2009 08:35

Re: CvarToColor( )
 
Awesome Work xPaw
I need this so much!
+karma :D

ConnorMcLeod 06-24-2009 11:43

Re: CvarToColor( )
 
I would stick with jim_yang 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.
PHP Code:

stock clamp_byteiByte )
{
    if( 
iByte )
    {
        return 
0
    
}
    else if( 
iByte 0xFF )
    {
        return 
0xFF
    
}
    return 
iByte
}

stock CvarPointerToColor(pPointer)
{
    static 
szColors[10], iTempiColor[3]
    
get_pcvar_string(pPointerszColorscharsmax(szColors))
    
iTemp str_to_num(szColors)

    
iColor[0] = clamp_byteiTemp 1000000 )
    
iTemp %= 1000000
    iColor
[1] = clamp_byteiTemp 1000 )
    
iColor[Blue] = clamp_byteiTemp 1000 )

    return 
iColor


Then you use it easily :
PHP Code:

new g_pCvarColor

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

public 
function_using_color()
{
    new 
iColors[3] = CvarPointerToColorg_pCvarColor )



xPaw 06-24-2009 11:54

Re: CvarToColor( )
 
Connor is right what jim's way is calls less natives, but with spaces its more friendly for server owners ;)

Arkshine 06-24-2009 12:13

Re: CvarToColor( )
 
Credit is for jim_yang not Connor. Also what is the advantage to use 0xFF instead of 255 ? 255 is more readable and 'understable'.

ConnorMcLeod 06-24-2009 12:26

Re: CvarToColor( )
 
Quote:

Originally Posted by arkshine (Post 855935)
Credit is for jim_yang not Connor.

I have never taken any credit for this, copied it from grenade trail plugin.


Quote:

Originally Posted by arkshine (Post 855935)
Also what is the advantage to use 0xFF instead of 255 ? 255 is more readable and 'understable'.

Your point.

Arkshine 06-24-2009 12:41

Re: CvarToColor( )
 
Quote:

I have never taken any credit for this, copied it from grenade trail plugin.
I was not talking to you but xPawn so to correct it in its code above.

Quote:

Your point.
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 )

ConnorMcLeod 06-24-2009 13:11

Re: CvarToColor( )
 
clamp byte was from me, but everyone could have made it :P

fysiks 06-24-2009 18:39

Re: CvarToColor( )
 
I think that I would make it more like the get_(p)cvar_* commands (for continuity :)).

Code:

get_cvar_color(pointer, iColor[3])
and
Code:

get_pcvar_color(pointer, iColor[3])


All times are GMT -4. The time now is 05:14.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.