[Include] i_colors - Color operations, processing, and formatting
#1

i_colors
Download
This is a little include I made to make working with colors easier. It includes basic functions to operate with colors on the component level, as well as color blending and conversions. It also introduces a new way to use colors in SA-MP messages, allowing nested colors.

Functions
Color components
pawn Code:
stock CreateColorComponentsArray(color)
Creates a char array from the color and returns it. You can index it like {0}, {1}, {2}, and {3} and it will return the appropriate color components (from R to A).

pawn Code:
stock GetColorComponentsArray(color, components[4 char])
Initializes a 4-byte array (one cell) with the color components. The layout is the same as in CreateColorComponentsArray.

pawn Code:
stock CreateColor(components[4 char])
Converts a component array from the previous functions to the color it represents.

pawn Code:
stock GetColorComponents(color, &r=0, &g=0, &b=0, &a=0)
Retrieves the color components from a color to variables.

pawn Code:
stock CreateColorRGBA(r, g, b, a=0xFF)
Create a colors from its components.

Hex tools
pawn Code:
stock ishex(c)
Returns true if the character is a hexadecimal digit.

pawn Code:
stock hexval(string[])
Parses a string containing a hexadecimal number.

pawn Code:
stock hexchar(i)
Converts a number to a hexadecimal digit.

Color strings
pawn Code:
stock InsertColorString(string[], col)
Inserts a 6-digit hexadecimal string at the beginning of the string.

pawn Code:
stock GetColorCode(col)
Returns a color code ({RRGGBB}) from a color.

pawn Code:
stock ReformatColorTags(color, string[], size = sizeof(string))
Parses nested color codes in a string, for more details see below. "color" is the initial color of the string.

pawn Code:
stock RemoveRedundantColors(color, string[])
Removes all redundant color codes in a string. "color" is the initial color of the string.

pawn Code:
stock ReplaceColorParentheses(string[], left='[', right=']')
Replaces specified parentheses with {} around color codes. Useful for allowing color codes in chat.

pawn Code:
stock IsColorCode(string[])
Checks if a string starts with 6 hexadecimal digits.

Color operations

pawn Code:
stock RGBAtoARGB(col)
Converts a color in RGBA to ARGB.

pawn Code:
stock ARGBtoRGBA(col)
Converts a color in ARGB to RGBA.

pawn Code:
stock CompareColorsRGB(col1, col2)
Compares two colors not taking into account their alpha components.

pawn Code:
stock BlendColors(col1, col2, Float:t)
Brightness-accurate color blending. t is a value from 0.0 to 1.0 specifying the point between the two colors. 0.0 returns the first color, 1.0 the second, 0.5 mixes the colors equally.

Nested color codes
ReformatColorTags parses a string looking for all color codes in the format "{RRGGBB{", remembering the current color of the string. After it finds the color code, it looks for a "}}" and replaces it with the original color code.
Example:
pawn Code:
new str[144] = "This is a {FFFF00{highlighted}} value.";
ReformatColorTags(0xFFFFFF00, str);
This converts the string to "This is a {FFFF00}highlighted{FFFFFF} value.", making it ready for displaying.
Multiple color levels are also allowed, like "White {FF0000{ Red {00FF00{ Green {0000FF{ Blue }} Green }} Red }} White".
Reply
#2

You have some usefull funcions here.

Do you know some way to get/have an list of colours for textdraws? ( not the list of Wiki anothers colors )
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)