Game.UI.ColorExtensions
Assembly: Assembly-CSharp
Namespace: Game.UI
Type: static class
Base: System.Object
Summary:
Extension methods for UnityEngine.Color that provide convenient conversions to hexadecimal color string representations. The primary method formats color components (red, green, blue, and optionally alpha) as two-digit uppercase hex values prefixed with '#'. Note: the implementation multiplies component floats by 255 and casts to int without clamping, so values outside the 0..1 range may produce unexpected hex output.
Fields
- None.
This static class defines no instance or static fields.
Properties
- None.
This static class exposes no properties.
Constructors
- None.
Being a static class, it cannot be instantiated and has no public constructors.
Methods
public static System.String ToHexCode(this UnityEngine.Color color, System.Boolean ignoreAlpha = false)
Converts a UnityEngine.Color to a hexadecimal string. Returns a string in the form "#RRGGBB" when ignoreAlpha is true, or "#RRGGBBAA" when ignoreAlpha is false (default). Each component is computed as (int)(component * 255f) and formatted as two uppercase hex digits (X2). The string begins with a leading '#'. The method does not clamp color component values, so inputs outside the 0..1 range may yield out-of-range byte values in the result.
Usage Example
using UnityEngine;
using Game.UI;
Color col = new Color(0.1f, 0.5f, 0.25f, 0.8f);
string hexWithAlpha = col.ToHexCode(); // e.g. "#1A803FCC"
string hexNoAlpha = col.ToHexCode(true); // e.g. "#1A803F"
Additional notes: - Hex letters are uppercase (A-F) due to the "X2" format specifier. - If you want clamped byte values, clamp the color components to [0,1] before calling ToHexCode:
Color clamped = new Color(
Mathf.Clamp01(col.r),
Mathf.Clamp01(col.g),
Mathf.Clamp01(col.b),
Mathf.Clamp01(col.a)
);
string safeHex = clamped.ToHexCode();