Raised This Month: $51 Target: $400
 12% 

[STK] CvarToColor


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 06-24-2009 , 04:48   [STK] CvarToColor
Reply With Quote #1

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;

Attached Files
File Type: sma Get Plugin or Get Source (CvarToColor_EXAMPLE.sma - 1154 views - 2.1 KB)
__________________

Last edited by xPaw; 05-07-2012 at 06:00.
xPaw is offline
zacky
Senior Member
Join Date: Mar 2008
Location: Sweden
Old 06-24-2009 , 04:58   Re: CvarToColor( )
Reply With Quote #2

Nice xpaw. +karma
zacky is offline
Send a message via Skype™ to zacky
alan_el_more
Veteran Member
Join Date: Jul 2008
Location: amxmodx-es.com
Old 06-24-2009 , 08:35   Re: CvarToColor( )
Reply With Quote #3

Awesome Work xPaw
I need this so much!
+karma
__________________
alan_el_more is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-24-2009 , 11:43   Re: CvarToColor( )
Reply With Quote #4

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 )

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 06-24-2009 , 11:54   Re: CvarToColor( )
Reply With Quote #5

Connor is right what jim's way is calls less natives, but with spaces its more friendly for server owners ;)
__________________
xPaw is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-24-2009 , 12:13   Re: CvarToColor( )
Reply With Quote #6

Credit is for jim_yang not Connor. Also what is the advantage to use 0xFF instead of 255 ? 255 is more readable and 'understable'.
__________________
Arkshine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-24-2009 , 12:26   Re: CvarToColor( )
Reply With Quote #7

Quote:
Originally Posted by arkshine View Post
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 View Post
Also what is the advantage to use 0xFF instead of 255 ? 255 is more readable and 'understable'.
Your point.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-24-2009 , 12:41   Re: CvarToColor( )
Reply With Quote #8

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 )
__________________

Last edited by Arkshine; 06-24-2009 at 12:47.
Arkshine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-24-2009 , 13:11   Re: CvarToColor( )
Reply With Quote #9

clamp byte was from me, but everyone could have made it
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-24-2009 , 18:39   Re: CvarToColor( )
Reply With Quote #10

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])
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:51.


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