PDA

View Full Version : [STK] CvarToColor


xPaw
06-24-2009, 04:48
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 :.
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 :.
Cvar | Stock
255103204 | 255, 103, 204
255 0 100 | 255, 0, 100
000 020 255 | 0, 20, 255

.: Stock :.
/*******
* 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;
}

zacky
06-24-2009, 04:58
Nice xpaw. +karma

alan_el_more
06-24-2009, 08:35
Awesome Work xPaw
I need this so much!
+karma :D

ConnorMcLeod
06-24-2009, 11:43
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.
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 :
new g_pCvarColor

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

public function_using_color()
{
new iColors[3] = CvarPointerToColor( g_pCvarColor )
}

xPaw
06-24-2009, 11:54
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
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
Credit is for jim_yang not Connor.

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


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
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.

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
clamp byte was from me, but everyone could have made it :P

fysiks
06-24-2009, 18:39
I think that I would make it more like the get_(p)cvar_* commands (for continuity :)).

get_cvar_color(pointer, iColor[3])
and
get_pcvar_color(pointer, iColor[3])

[ --<-@ ] Black Rose
06-26-2009, 09:31
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 )
Cuz it's so much cooler... -_-'

I don't even see a reason for the function. What's wrong with clamp(num, 0, 0xFF); / clamp(num, 0, 255); ?

ConnorMcLeod
06-26-2009, 11:30
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.

[ --<-@ ] Black Rose
06-26-2009, 12:33
... You earned 0.01 seconds by using an ugly stock. You are teh win. Here's your nobelnothing price.

Prove it.

Hey... let's just rewrite the whole AMXX in one mm plugin without any custom plugins. That would speed things up.

stupok
06-26-2009, 12:55
code
#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
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

Arkshine
06-26-2009, 13:13
I was going to do it because I was sure that the difference would be unsignificant. Though I did not thought clamp() would be slightly more faster.

DA
06-26-2009, 13:17
Plugin looks well. Do you think it's possible to add Hex colorcode?
Most programmer use Hexcode instead of decimal colorcode.

[ --<-@ ] Black Rose
06-26-2009, 13:22
Here's a simple decimal to binary/hex converter.
itoa works with any radix. atoi works only with Hex and Binary.

#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;
}

Arkshine
06-26-2009, 14:27
If the purpose is to convert Hex value to RGB or RGB to Hex, it doesn't work as expected.

[ --<-@ ] Black Rose
06-26-2009, 17:11
It sure as hell does.

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

ConnorMcLeod
06-26-2009, 17:23
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

Wooaaa, can't belive it :mrgreen:
With some values clamp is lower, but you are right, stock is useless.