Veteran Member
Join Date: Jan 2010
Location: mom's basement
07-07-2013
, 05:43
Re: RGB, RGBA, Hex
#13
Quote:
Originally Posted by
StrikerMan780
Could one explain how to do this if the Hex color is a string?
I want to convert... say... String:Color [7]="FF0000";
to
r = 255;
g = 0;
b = 0;
StringToIntEx
This work with hex-triplet (FF0000) , but give wrong results with hex-quadruple (FF000000)...
PHP Code:
new String : ColorString []= "FF0000" ;
new ColorHex , r , g , b ;
StringToIntEx ( ColorString , ColorHex , 16 ); // <- Numerical base "16", hexadecimal
r = (( ColorHex >> 16 ) & 255 );
g = (( ColorHex >> 8 ) & 255 );
b = (( ColorHex >> 0 ) & 255 );
PrintToServer ( "%s\n%02X\nr = %i, g = %i, b = %i" , ColorString , ColorHex , r , g , b );
And remember, lower case letters give different results than upper case letters, ff != FF *false info
some crappy thing
PHP Code:
public OnPluginStart ()
{
RegConsoleCmd ( "sm_test" , test );
}
public Action : test ( client , args )
{
new String : Color []= "00FF00AA" ;
new ColorHex , r , g , b , a ;
if( ! HexStringToRGBA ( Color , ColorHex , r , g , b , a ) )
{
LogError ( "Something were wrong" )
return Plugin_Handled ;
}
PrintToServer ( "%s, %08X\nr = %i, g = %i, b = %i, a = %i" , Color , ColorHex , r , g , b , a );
// Use either ways
HexStringToRGBA ( Color , _ , r , g , b )
PrintToServer ( "\n%s, r = %i, g = %i, b = %i" , Color , r , g , b );
HexStringToRGBA ( Color , ColorHex )
PrintToServer ( "\n%s, %08X" , Color , ColorHex );
return Plugin_Handled ;
}
bool : HexStringToRGBA (const String : ColorString [], & ColorHex = 0 , & r = 0 , & g = 0 , & b = 0 , & a = 0 )
{
new length = strlen ( ColorString );
if( !( length == 6 || length == 8 ) )
{
return false ;
}
for(new i = 0 ; i < length ; i ++)
{
if( IsCharAlpha ( ColorString [ i ]))
{
if( !( 65 <= ColorString [ i ] <= 70 || 97 <= ColorString [ i ] <= 102 ) ) // Only letters ABCDEF
{
return false ;
}
}
}
if( length == 8 ) // StringToIntEx can't handle this big hexadecimal, need chop it
{
new String : temp [ 7 ];
Format ( temp , sizeof ( temp ), "%s" , ColorString );
StringToIntEx ( temp , ColorHex , 16 );
StringToIntEx ( ColorString [ 6 ], a , 16 );
}
else
{
StringToIntEx ( ColorString , ColorHex , 16 );
}
r = (( ColorHex >> 16 ) & 255 );
g = (( ColorHex >> 8 ) & 255 );
b = (( ColorHex >> 0 ) & 255 );
a = a ;
if( length == 8 ) // re-calculate hex color RGBA
{
ColorHex = 0 ;
ColorHex |= ( ( r & 255 ) << 24 );
ColorHex |= ( ( g & 255 ) << 16 );
ColorHex |= ( ( b & 255 ) << 8 );
ColorHex |= ( ( a & 255 ) << 0 );
}
//PrintToServer(">>%s\n%08X\nr = %i, g = %i, b = %i, a = %i", ColorString, ColorHex, r, g, b, a);
return true ;
}
__________________
Do not Private Message @me
Last edited by Bacardi; 07-07-2013 at 08:45 .