Class Hex
A static utility class for converting between hexadecimal color strings and UnityEngine.Color objects, and for normalizing color component values.
public static class Hex
- Inheritance
-
Hex
- Inherited Members
Methods
ColorNormalize(float, float, float)
Normalizes the RGB components of a color, assuming an alpha of 1.0f (255). See ColorNormalize(Color) for normalization details.
public static Color ColorNormalize(float r, float g, float b)
Parameters
rfloatThe red component (expected range 0-255 or greater).
gfloatThe green component (expected range 0-255 or greater).
bfloatThe blue component (expected range 0-255 or greater).
Returns
- Color
A new UnityEngine.Color with components normalized to the 0-1 range.
ColorNormalize(float, float, float, float)
Normalizes the RGBA components of a color. If any component (R, G, B, or A) is greater than 255, all components are scaled down such that the largest component becomes 255, and then divided by 255.0f to fit the 0-1 range expected by UnityEngine.Color. This is useful when converting 0-255 based integer color components to Unity's 0-1 float color space, especially if values might exceed 255 due to bitwise operations or other calculations.
public static Color ColorNormalize(float r, float g, float b, float a)
Parameters
rfloatThe red component (expected range 0-255 or greater).
gfloatThe green component (expected range 0-255 or greater).
bfloatThe blue component (expected range 0-255 or greater).
afloatThe alpha component (expected range 0-255 or greater).
Returns
- Color
A new UnityEngine.Color with components normalized to the 0-1 range.
ColorNormalize(Color)
Normalizes the RGBA components of a UnityEngine.Color struct. If any component (R, G, B, or A) is greater than 255, all components are scaled down such that the largest component becomes 255, and then divided by 255.0f to fit the 0-1 range expected by UnityEngine.Color. This is useful when converting 0-255 based integer color components to Unity's 0-1 float color space, especially if values might exceed 255 due to bitwise operations or other calculations.
public static Color ColorNormalize(Color color)
Parameters
colorColorThe color to normalize.
Returns
- Color
A new UnityEngine.Color with components normalized to the 0-1 range.
ToColor(string)
Converts a hexadecimal color string (e.g., "RRGGBB" or "RRGGBBAA") to a UnityEngine.Color object. If the string is shorter than 6 characters (e.g., "RRGGBB" format), it returns UnityEngine.Color.white. If the string is 6 characters, it's parsed as "RRGGBB" with full opacity (alpha 255). If the string is 8 characters, it's parsed as "RRGGBBAA". Color components are normalized from 0-255 to 0-1 range using ColorNormalize(float, float, float, float).
public static Color ToColor(string s)
Parameters
sstringThe hexadecimal color string (e.g., "FF0000" for red, "00FF00FF" for green with full alpha).
Returns
- Color
A UnityEngine.Color object parsed from the hex string, or UnityEngine.Color.white on invalid input length.
ToHex(int)
Converts an integer value to its hexadecimal string representation. For example, 255 converts to "FF", 10 converts to "A". The "X" format specifier ensures uppercase hexadecimal output.
public static string ToHex(this int value)
Parameters
valueintThe integer value to convert.
Returns
- string
The hexadecimal string representation of the integer.
ToHex(Color)
Converts a UnityEngine.Color object to its 8-character hexadecimal string representation (RRGGBBAA). Each color component (R, G, B, A) is scaled from its 0-1 float range to 0-255 integer, then formatted as a two-digit uppercase hexadecimal number.
public static string ToHex(this Color color)
Parameters
colorColorThe UnityEngine.Color object to convert.
Returns
- string
An 8-character hexadecimal string representing the color (e.g., "FF0000FF" for red).
ToInt(string)
Converts a hexadecimal string representation to an integer. For example, "FF" converts to 255, "0A" converts to 10. The parsing is invariant culture-specific and case-insensitive. If parsing fails (e.g., invalid hex characters), it returns 0.
public static int ToInt(string value)
Parameters
valuestringThe hexadecimal string to convert.
Returns
- int
The integer representation of the hex string, or 0 if parsing fails.