Mostly Harmless

User:Eliot/ColorTags Operators

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
ColorTags Operators
Provides operators and functions to convert a color struct to a color tag.


Author Eliot
Compatible Unreal Engine 2


When using any of the provided source code please credit the listed Author in your source code! e.g. "ColorTags Operators utils written by Eliot.".
View more utils at UnrealScript Utils


Because i hate to call static functions of epic games or self-written ones to generate a color tag from a color on run-time i have made those operations so that i have to write less and so my long ClientMessage strings aren't so ugly to read.

Note: I haven't tested all of these operators!, if you find a bug or something else feel free to correct the code here on this article!.

Creating color tags

/** Returns int A as a color tag. */
static final preoperator string $( int A )
{
	return (Chr( 0x1B ) $ (Chr( Max( A & 0xFF0000, 1 )  ) $ Chr( Max( A & 0x00FF00, 1 ) ) $ Chr( Max( A & 0x0000FF, 1 ) )));
}

Example

local string redcolortag;
 
redcolortag = $0xFF0000;

Returns a red color tag based on the specified hexadecimal.

/** Returns color A as a color tag. */
static final preoperator string $( Color A )
{
	return (Chr( 0x1B ) $ (Chr( Max( A.R, 1 )  ) $ Chr( Max( A.G, 1 ) ) $ Chr( Max( A.B, 1 ) )));
}

Example

local string redcolortag;
 
redcolortag = $Class'HUD'.default.RedColor;
Log( redcolortag $ "blabla this is colored red by using a cached color tag" );

Returns a red color tag based on the specified color struct.

Adding color tags

/** Adds B as a color tag to the end of A. */
static final operator(40) string $( coerce string A, Color B )
{
	return A $ $B;
}
 
/** Adds A as a color tag to the begin of B. */
static final operator(40) string $( Color A, coerce string B )
{
	return $A $ B;
}
 
/** Adds B as a color tag to the end of A with a space in between. */
static final operator(40) string @( coerce string A, Color B )
{
	return A @ $B;
}
 
/** Adds A as a color tag to the begin of B with a space in between. */
static final operator(40) string @( Color A, coerce string B )
{
	return $A @ B;
}
 
/** Adds B as a color tag to the end of A. */
static final operator(44) string $=( out string A, Color B )
{
	return A $ $B;
}
 
/** Adds B as a color tag to the end of A with a space inbetween. */
static final operator(44) string @=( out string A, Color B )
{
	return A @ $B;
}

Example

local PlayerController PC;
 
PC = Level.GetLocalPlayerController();
if( PC == none )
{
    return;
}
 
PC.ClientMessage( Class'HUD'.default.BlueColor $ "Welcome" $ Class'HUD'.default.WhiteColor @ PC.GetHumanReadableName() @ Class'HUD'.default.BlueColor $ "to our server!." );

This will print a colored message to the local client saying "Welcome" in blue + "PlayerName" in white + "to our server!." in blue, obviously it would be nicer if you don't use the default color variables from HUD and use self declared colors of your class.


Conditional operators

/**
 * Tests if A contains color tag B.
 *
 * @return		TRUE if A contains color tag B, FALSE if A does not contain color tag B.
 */
static final operator(24) bool ~=( coerce string A, Color B )
{
	return InStr( A, $B ) != -1;
}

Removing/Replacing color tags

/** Strips all color B tags from A. */
static final operator(45) string -=( out string A, Color B )
{
	return A -= $B;
}
 
/** Strips all color tags from A. */
static final preoperator string %( string A )
{
	local int i;
 
        while( true )
	{
		i = InStr( A, Chr( 0x1B ) );
		if( i != -1 )
		{
			A = Left( A, i ) $ Mid( A, i + 4 );
			continue;
		}
		break;
	}
	return A;
}
 
/** Replaces all color B tags in A with color C tags. */
static final function string ReplaceColorTag( string A, Color B, Color C )
{
	return Repl( A, $B, $C );
}

Converting color tags

/** Converts a color tag from A to a color struct into B. */
static final function ColorTagToColor( string A, out Color B )
{
        A = Mid( A, 1 );
	B.R = byte(Asc( Left( A, 1 ) ));
	A = Mid( A, 1 );
	B.G = byte(Asc( Left( A, 1 ) ));
	A = Mid( A, 1 );
	B.B = byte(Asc( Left( A, 1 ) ));
        B.A = 0xFF; // Max(255)
}

Note: I'm pretty sure this one is very slow xD! but anyway might still be handy if for example you want to convert user inputted(or from a player's name) color tags back to a color struct.

To use these operations just add them in your class above your functions and use them as you can see in the examples and also please credit me when using any of these operations :).