Class ColorUtil
Provides comprehensive utility methods and extension methods for working with Unity's UnityEngine.Color and UnityEngine.Color32 types, including hex string parsing and formatting, HSV decomposition and manipulation, per-channel copy-and-modify helpers, RGB multiplication, color transformations (invert, grayscale, luminance), HSV-space interpolation, and procedural random color generation.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class ColorUtil
Remarks
Extension methods are defined on both UnityEngine.Color (float channels, 0-1 range) and UnityEngine.Color32 (byte channels, 0-255 range) so that callers can use whichever type their pipeline operates on without manual conversions.
Hex parsing supports RRGGBB (6-character, opaque) and RRGGBBAA
(8-character) formats with an optional # prefix via
TryParseHex(string, out Color). An ARGB variant (AARRGGBB) is also available via
TryParseARGB(string, out Color), which is used by the ConfigFile
system for color property storage.
Luminance is computed using the ITU-R BT.709 coefficients (R: 0.2126, G: 0.7152, B: 0.0722), which reflect human perceptual sensitivity to each color channel.
Random color generation methods that use UnityEngine.Random must be called
from the main thread.
Methods
ClampToSRGBGamut(float, float, float, float)
Converts an Oklch color to sRGB, first reducing its chroma until it fits inside the gamut.
Declaration
public static Color ClampToSRGBGamut(float L, float C, float h, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | L | Lightness in [0, 1]. |
| float | C | Requested chroma. Reduced when it does not fit. |
| float | h | Hue as a fraction of a full turn, in [0, 1]. |
| float | alpha | Alpha of the resulting color. |
Returns
| Type | Description |
|---|---|
| Color | An in-gamut sRGB color at the requested lightness and hue. |
Remarks
FromOklch(float, float, float, float) clips each channel independently when a color falls outside the gamut, which shifts its hue. Reducing chroma instead keeps the hue and lightness intact and gives up only saturation, which is almost always the preferable trade when deriving a theme color.
Color32ToHex(Color32)
Converts a UnityEngine.Color32 to an uppercase 8-character hex string in
RRGGBBAA format. Always includes the alpha channel.
Declaration
public static string Color32ToHex(Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to convert. |
Returns
| Type | Description |
|---|---|
| string | An 8-character uppercase hex string, e.g. |
Color32ToHex(Color32, bool)
Converts a UnityEngine.Color32 to an uppercase hex string in RRGGBB
(6-character) or RRGGBBAA (8-character) format depending on
includeAlpha.
Declaration
public static string Color32ToHex(Color32 color, bool includeAlpha)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to convert. |
| bool | includeAlpha |
|
Returns
| Type | Description |
|---|---|
| string | An uppercase hex string in the requested format. |
ColorToHex(Color)
Converts a UnityEngine.Color to an uppercase hex string in RRGGBBAA format
by first implicitly converting to UnityEngine.Color32.
Declaration
public static string ColorToHex(Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to convert. |
Returns
| Type | Description |
|---|---|
| string | An 8-character uppercase hex string, e.g. |
FormatARGB(Color)
Converts a UnityEngine.Color to an uppercase 8-character ARGB hex string in
AARRGGBB byte order.
Declaration
public static string FormatARGB(Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to format. Channel values are rounded to the nearest byte. |
Returns
| Type | Description |
|---|---|
| string | An 8-character uppercase hex string in |
Remarks
This format is used by the Scylla ConfigFile system for color property
storage and can be round-tripped via TryParseARGB(string, out Color). For the more
common web/CSS RRGGBBAA order, use ColorToHex(Color) instead.
FromHSL(float, float, float, float)
Creates an RGB UnityEngine.Color from HSL (Hue, Saturation, Lightness) components.
Declaration
public static Color FromHSL(float h, float s, float l, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | h | Hue in [0, 1]. |
| float | s | Saturation in [0, 1]. |
| float | l | Lightness in [0, 1]. |
| float | alpha | Alpha in [0, 1]. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Color | The RGB color. |
FromHSV(Vector3, float)
Constructs a UnityEngine.Color from HSV components packed in a UnityEngine.Vector3 and applies the specified alpha.
Declaration
public static Color FromHSV(Vector3 hsv, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | hsv | A UnityEngine.Vector3 where |
| float | alpha | The alpha channel value to assign to the resulting color in the [0, 1] range.
Defaults to |
Returns
| Type | Description |
|---|---|
| Color | A new UnityEngine.Color constructed from the HSV values and the given alpha. |
FromOklab(float, float, float, float)
Converts Oklab (L, a, b) components back to an sRGB UnityEngine.Color. Alpha is set to 1.
Declaration
public static Color FromOklab(float L, float a, float b, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | L | Oklab lightness. |
| float | a | Oklab green-red axis. |
| float | b | Oklab blue-yellow axis. |
| float | alpha | Alpha channel. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Color | The sRGB color. |
FromOklch(float, float, float, float)
Creates an sRGB UnityEngine.Color from Oklch (Lightness, Chroma, Hue) components.
Declaration
public static Color FromOklch(float L, float C, float H, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | L | Lightness (0-1). |
| float | C | Chroma (distance from neutral). |
| float | H | Hue in [0, 1] (full circle). |
| float | alpha | Alpha channel. Defaults to 1. |
Returns
| Type | Description |
|---|---|
| Color | The sRGB color. |
GetHue(Color)
Extracts and returns the hue component of a UnityEngine.Color via an HSV decomposition. The hue represents the color's position on the color wheel.
Declaration
public static float GetHue(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to decompose. Alpha is ignored. |
Returns
| Type | Description |
|---|---|
| float | The hue in the range [0, 1), where 0/1 = red, 1/3 = green, 2/3 = blue. Returns 0 for achromatic colors (zero saturation). |
GetHue(Color32)
Gets the hue component of a UnityEngine.Color32 (0-1 range).
Declaration
public static float GetHue(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to analyze. |
Returns
| Type | Description |
|---|---|
| float | The hue value in the range 0-1. |
GetLightness(Color)
Gets the HSL lightness component of a color. Lightness is the average of the maximum and minimum RGB components.
Declaration
public static float GetLightness(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color. |
Returns
| Type | Description |
|---|---|
| float | The lightness value in [0, 1]. |
GetLuminance(Color)
Computes the perceived luminance of a UnityEngine.Color using the ITU-R BT.709
linear coefficients: L = R * 0.2126 + G * 0.7152 + B * 0.0722.
Declaration
public static float GetLuminance(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to analyze. Alpha is ignored. |
Returns
| Type | Description |
|---|---|
| float | The luminance in the range [0, 1], where 0 = black and 1 = white. Values above 1 are possible for HDR colors. |
Remarks
The result represents the brightness the human visual system perceives for this color. It is used by Grayscale(Color) and RandomColorWithContrast(Color, float, float) for contrast-aware color selection. Note that these coefficients are defined for linear (non-gamma-corrected) values. When working with gamma-encoded colors, linearize the channel values first.
GetLuminance(Color32)
Calculates the perceived luminance of a UnityEngine.Color32 using ITU-R BT.709 coefficients. This represents how bright the color appears to human eyes.
Declaration
public static float GetLuminance(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to analyze. |
Returns
| Type | Description |
|---|---|
| float | The luminance value in the range 0-1. |
GetSaturation(Color)
Extracts and returns the saturation component of a UnityEngine.Color via an HSV decomposition. Saturation represents the color's intensity from gray to fully vivid.
Declaration
public static float GetSaturation(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to decompose. Alpha is ignored. |
Returns
| Type | Description |
|---|---|
| float | The saturation in the range [0, 1], where 0 = fully desaturated (gray) and 1 = fully saturated (vivid). |
GetSaturation(Color32)
Gets the saturation component of a UnityEngine.Color32 (0-1 range).
Declaration
public static float GetSaturation(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to analyze. |
Returns
| Type | Description |
|---|---|
| float | The saturation value in the range 0-1. |
GetValue(Color)
Extracts and returns the value (brightness) component of a UnityEngine.Color via an HSV decomposition. Value represents the maximum channel intensity of the color.
Declaration
public static float GetValue(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to decompose. Alpha is ignored. |
Returns
| Type | Description |
|---|---|
| float | The value in the range [0, 1], where 0 = black and 1 = the brightest possible shade of the given hue. |
GetValue(Color32)
Gets the value (brightness) component of a UnityEngine.Color32 (0-1 range).
Declaration
public static float GetValue(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to analyze. |
Returns
| Type | Description |
|---|---|
| float | The value in the range 0-1. |
Grayscale(Color)
Converts a UnityEngine.Color to grayscale by computing its perceptual luminance via ITU-R BT.709 coefficients and assigning that value to all three RGB channels. Alpha is preserved.
Declaration
public static Color Grayscale(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to convert. |
Returns
| Type | Description |
|---|---|
| Color | A new UnityEngine.Color with equal R, G, B values representing the perceptual luminance of the input, and the same alpha. |
Remarks
The luminance is computed as
L = R * 0.2126 + G * 0.7152 + B * 0.0722.
The result is a shade of gray that matches the perceived brightness of the original
color, rather than a simple arithmetic mean of the RGB channels.
Grayscale(Color32)
Converts a UnityEngine.Color32 to grayscale using ITU-R BT.709 perceptual luminance weights, preserving alpha.
Declaration
public static Color32 Grayscale(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to convert. |
Returns
| Type | Description |
|---|---|
| Color32 | A grayscale version of the color. |
HexToColor(string)
Parses a hex color string in RRGGBB or RRGGBBAA format (with
optional # prefix) and returns the resulting UnityEngine.Color.
Returns Color.black if the input is invalid.
Declaration
public static Color HexToColor(string hex)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | The hex string to parse, e.g. |
Returns
| Type | Description |
|---|---|
| Color | The parsed UnityEngine.Color, or |
Remarks
This is a non-throwing convenience wrapper around TryParseHex(string, out Color). Prefer TryParseHex(string, out Color) when you need to distinguish between a successfully parsed black color and a parse failure.
HexToColor32(string)
Parses a hex color string in RRGGBB or RRGGBBAA format (with
optional # prefix) and returns the resulting UnityEngine.Color32.
Returns opaque black (0, 0, 0, 255) if the input is invalid.
Declaration
public static Color32 HexToColor32(string hex)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | The hex string to parse, e.g. |
Returns
| Type | Description |
|---|---|
| Color32 | The parsed UnityEngine.Color32, or opaque black on failure. |
Remarks
Convenience wrapper around ToColor32(string). Prefer TryParseHex(string, out Color) followed by manual UnityEngine.Color32 construction when distinguishing between a parsed black color and a parse failure is necessary.
Invert(Color)
Returns a new UnityEngine.Color with each RGB channel negated as
1 - channel, while preserving the original alpha. This produces the
complementary color on the RGB color wheel.
Declaration
public static Color Invert(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to invert. |
Returns
| Type | Description |
|---|---|
| Color | A new UnityEngine.Color where each RGB channel is the complement of the original, and the alpha is unchanged. |
Invert(Color32)
Returns the inverted UnityEngine.Color32 (255 − RGB) while preserving alpha.
Declaration
public static Color32 Invert(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to invert. |
Returns
| Type | Description |
|---|---|
| Color32 | The inverted color. |
IsInSRGBGamut(float, float, float)
Tests whether an Oklch color lies inside the sRGB gamut, meaning it can be displayed without any channel being clipped.
Declaration
public static bool IsInSRGBGamut(float L, float C, float h)
Parameters
| Type | Name | Description |
|---|---|---|
| float | L | Lightness in [0, 1]. |
| float | C | Chroma, typically in [0, 0.4]. |
| float | h | Hue as a fraction of a full turn, in [0, 1]. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
This deliberately repeats the linear-RGB stage of FromOklab(float, float, float, float) rather than calling it. FromOklab(float, float, float, float) finishes by encoding through LinearToSRGB(float), which clamps, and a clamped channel is exactly the out-of-gamut signal this method exists to detect.
A small tolerance is allowed on each channel so that colors sitting exactly on the gamut boundary are reported as inside it.
LerpHSV(Color, Color, float)
Interpolates between two colors in HSV space, taking the shortest hue path around
the color wheel to avoid the harsh transitions that occur with standard RGB
Color.Lerp.
Declaration
public static Color LerpHSV(Color a, Color b, float t)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | a | The start color at |
| Color | b | The end color at |
| float | t | The interpolation factor. Values outside [0, 1] are clamped. |
Returns
| Type | Description |
|---|---|
| Color | The HSV-interpolated color at position |
Remarks
Each component - hue, saturation, value, and alpha - is interpolated separately. Hue wraparound is handled by adjusting the start or end hue value so that the interpolation always traverses the shorter arc of the color wheel:
- If
h2 - h1 > 0.5,h1is increased by 1 so the path goes backward through 0. - If
h2 - h1 < -0.5,h2is increased by 1 so the path goes forward through 1.
The interpolated hue is then wrapped back into [0, 1) via modulo.
For simple cross-fade without a specific hue path preference, use
Color.Lerp which operates in linear RGB space instead.
LerpOklab(Color, Color, float)
Interpolates two colors in Oklab perceptual color space. Produces visually uniform blending with no muddy midtones.
Declaration
public static Color LerpOklab(Color a, Color b, float t)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | a | Start color. |
| Color | b | End color. |
| float | t | Interpolation factor in [0, 1]. |
Returns
| Type | Description |
|---|---|
| Color | The interpolated color. |
LerpOklch(Color, Color, float)
Interpolates two colors in Oklch space using the shortest hue arc. Preserves chroma and lightness uniformity while smoothly transitioning hue.
Declaration
public static Color LerpOklch(Color a, Color b, float t)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | a | Start color. |
| Color | b | End color. |
| float | t | Interpolation factor in [0, 1]. |
Returns
| Type | Description |
|---|---|
| Color | The interpolated color. |
LinearToSRGB(float)
Converts a single linear light component to sRGB.
Declaration
public static float LinearToSRGB(float c)
Parameters
| Type | Name | Description |
|---|---|---|
| float | c | Linear light component. |
Returns
| Type | Description |
|---|---|
| float | sRGB component in [0, 1], clamped. |
MaxOklabChromaAtHue(float, float)
Finds the highest chroma the sRGB gamut can hold at a given Oklab lightness and hue.
Declaration
public static float MaxOklabChromaAtHue(float L, float h)
Parameters
| Type | Name | Description |
|---|---|---|
| float | L | Lightness in [0, 1]. |
| float | h | Hue as a fraction of a full turn, in [0, 1]. |
Returns
| Type | Description |
|---|---|
| float | The maximum in-gamut chroma, or 0 at the lightness extremes. |
Remarks
The sRGB gamut is far from a cylinder in Oklch: a saturated yellow reaches roughly twice the chroma of a saturated blue at the same lightness. Use this to express saturation as a fraction of what a hue can actually hold, so a set of rotated hues looks evenly saturated instead of evenly numbered.
The answer is found by bisection over 18 iterations, which resolves chroma to roughly two parts in a million. Near-black and near-white return zero, since no chroma survives there.
MultiplyRGB(Color32, float)
Returns a copy of the UnityEngine.Color32 with its RGB channels multiplied by a scalar, preserving alpha. Values are clamped to the 0-255 range.
Declaration
public static Color32 MultiplyRGB(this Color32 color, float k)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| float | k | The scalar multiplier. |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with multiplied RGB channels. |
MultiplyRGB(Color32, Color32)
Returns a copy of the UnityEngine.Color32 with its RGB channels multiplied
component-wise by another UnityEngine.Color32, preserving the original alpha.
Each channel is computed as (a * b + 128) / 255 for accurate byte-range
multiplication.
Declaration
public static Color32 MultiplyRGB(this Color32 color, Color32 other)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| Color32 | other | The color to multiply by. |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with component-wise multiplied RGB channels. |
MultiplyRGB(ref Color, float)
Multiplies the R, G, and B channels of a UnityEngine.Color by a scalar while leaving the alpha channel unchanged. Useful for dimming or brightening colors without affecting transparency.
Declaration
public static void MultiplyRGB(this ref Color color, float k)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to modify in place. |
| float | k | The scalar to multiply each RGB channel by. |
Remarks
This is a ref mutating extension method that overwrites the color in place.
Note that channel values are not clamped; supplying a scalar greater than 1 can
produce HDR values above 1 if the context supports them.
MultiplyRGB(ref Color, Color)
Multiplies the R, G, and B channels of a UnityEngine.Color component-wise by the corresponding channels of another UnityEngine.Color, preserving the original alpha. This is equivalent to a modulate blend of the two colors.
Declaration
public static void MultiplyRGB(this ref Color color, Color other)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to modify in place. |
| Color | other | The color whose RGB channels are used as multipliers. |
Remarks
This is a ref mutating extension method. Channel values are not clamped,
so HDR values are preserved if inputs contain HDR data.
RandomColor(float?, float?, float?, float?)
Generates a random UnityEngine.Color in HSV space, allowing individual HSV and alpha components to be locked to specific values while randomizing the rest.
Declaration
public static Color RandomColor(float? hue = null, float? saturation = null, float? value = null, float? alpha = null)
Parameters
| Type | Name | Description |
|---|---|---|
| float? | hue | The fixed hue (0-1), or |
| float? | saturation | The fixed saturation (0-1), or |
| float? | value | The fixed HSV value/brightness (0-1), or |
| float? | alpha | The fixed alpha (0-1), or |
Returns
| Type | Description |
|---|---|
| Color | A UnityEngine.Color with the specified constraints applied. |
Remarks
Each non-null parameter is clamped to [0, 1] before use. Null parameters are
replaced with a fresh UnityEngine.Random.value sample. Alpha defaults to
1 (fully opaque) when null, unlike hue, saturation, and value which
default to fully random.
Must be called from the main thread because it uses UnityEngine.Random.
For off-thread or deterministic generation, use
RandomColorInRange(float, float, float, float, float, float, float) combined with a seeded RNG from
RandomUtil.
RandomColor32(float?, float?, float?, float?)
Generates a random UnityEngine.Color32 in HSV space, allowing individual HSV and alpha components to be locked while randomizing the rest. Delegates to RandomColor(float?, float?, float?, float?) and converts the result.
Declaration
public static Color32 RandomColor32(float? hue = null, float? saturation = null, float? value = null, float? alpha = null)
Parameters
| Type | Name | Description |
|---|---|---|
| float? | hue | The fixed hue (0-1), or |
| float? | saturation | The fixed saturation (0-1), or |
| float? | value | The fixed HSV value/brightness (0-1), or |
| float? | alpha | The fixed alpha (0-1), or |
Returns
| Type | Description |
|---|---|
| Color32 | A UnityEngine.Color32 with the specified constraints applied. |
Remarks
Alpha note: the alpha parameter is expressed in [0, 1] float
range (like the other HSV parameters), and the result is a UnityEngine.Color32
with alpha in [0, 255]. When null, alpha defaults to 1.0f (= 255).
Must be called from the main thread (uses UnityEngine.Random).
RandomColorInRange(float, float, float, float, float, float, float)
Generates a random color within specified HSV ranges.
Supports hue wraparound: if minHue is greater than
maxHue, the hue range wraps through 0 (e.g. 0.8 to 0.2
covers red-orange-yellow). For saturation and value, inverted min/max are
automatically swapped.
Declaration
public static Color RandomColorInRange(float minHue = 0, float maxHue = 1, float minSaturation = 0, float maxSaturation = 1, float minValue = 0, float maxValue = 1, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | minHue | Minimum hue (0-1). |
| float | maxHue | Maximum hue (0-1). |
| float | minSaturation | Minimum saturation (0-1). |
| float | maxSaturation | Maximum saturation (0-1). |
| float | minValue | Minimum value/brightness (0-1). |
| float | maxValue | Maximum value/brightness (0-1). |
| float | alpha | The alpha component (0-1). Default is 1. |
Returns
| Type | Description |
|---|---|
| Color | A random color within the specified HSV ranges. |
Remarks
Must be called from the main thread (uses UnityEngine.Random).
RandomColorWithContrast(Color, float, float)
Generates a random vivid color that contrasts with the specified background color. Uses a retry loop with vivid color candidates, falling back to black or white (whichever contrasts more) if no suitable candidate is found.
Declaration
public static Color RandomColorWithContrast(Color background, float minContrast = 0.3, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | background | The background color to contrast against. |
| float | minContrast | Minimum required luminance difference (0-1). Default is 0.3. |
| float | alpha | The alpha component (0-1). Default is 1. |
Returns
| Type | Description |
|---|---|
| Color | A random color that contrasts with the background. |
Remarks
Must be called from the main thread (uses UnityEngine.Random).
RandomGrayscale(float, float, float)
Generates a random grayscale color within the specified value range.
Declaration
public static Color RandomGrayscale(float minValue = 0, float maxValue = 1, float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | minValue | Minimum brightness (0-1). Default is 0. |
| float | maxValue | Maximum brightness (0-1). Default is 1. |
| float | alpha | The alpha component (0-1). Default is 1. |
Returns
| Type | Description |
|---|---|
| Color | A random grayscale color. |
Remarks
Must be called from the main thread (uses UnityEngine.Random).
RandomPastel(float)
Generates a random pastel UnityEngine.Color with randomized hue, constrained saturation (0.25-0.5) and constrained brightness (0.85-1.0).
Declaration
public static Color RandomPastel(float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | alpha | The alpha component (0-1). Defaults to 1 (fully opaque). |
Returns
| Type | Description |
|---|---|
| Color | A random pastel UnityEngine.Color with the specified alpha. |
Remarks
Pastel colors are characterized by low-to-moderate saturation combined with high
brightness, producing soft, washed-out tones suitable for backgrounds, UI tints,
and decorative accents that should not dominate the visual hierarchy.
Must be called from the main thread (uses UnityEngine.Random).
RandomVivid(float)
Generates a random vivid UnityEngine.Color with randomized hue and both saturation and brightness constrained to the range 0.8-1.0.
Declaration
public static Color RandomVivid(float alpha = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| float | alpha | The alpha component (0-1). Defaults to 1 (fully opaque). |
Returns
| Type | Description |
|---|---|
| Color | A random vivid UnityEngine.Color with the specified alpha. |
Remarks
Vivid colors are fully saturated and bright, producing intense, eye-catching tones
suitable for highlights, indicators, health bars, and other elements that need to
stand out against neutral or dark backgrounds.
Must be called from the main thread (uses UnityEngine.Random).
SRGBToLinear(float)
Converts a single sRGB component to linear light.
Declaration
public static float SRGBToLinear(float c)
Parameters
| Type | Name | Description |
|---|---|---|
| float | c | sRGB component in [0, 1]. |
Returns
| Type | Description |
|---|---|
| float | Linear light component. |
SetBrightness(ref Color, float)
Mutates a UnityEngine.Color by replacing its value (brightness/HSV-V) while keeping its hue, saturation, and alpha unchanged.
Declaration
public static void SetBrightness(this ref Color color, float brightness)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to modify in place. |
| float | brightness | The new brightness (HSV value) in the [0, 1] range. Values outside this range are clamped. A value of 0 produces black; a value of 1 produces the brightest shade of the color's hue at its current saturation. |
Remarks
This is a ref mutating extension method. For a non-mutating copy, use
WithBrightness(Color, float) instead.
SetHue(ref Color, float)
Mutates a UnityEngine.Color by replacing its hue while keeping its saturation, value, and alpha unchanged.
Declaration
public static void SetHue(this ref Color color, float hue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to modify in place. |
| float | hue | The new hue in the [0, 1] range. Values outside this range are clamped. |
Remarks
This is a ref mutating extension method. If you need a new copy rather than
an in-place modification, use WithHue(Color, float) instead.
SetSaturation(ref Color, float)
Mutates a UnityEngine.Color by replacing its saturation while keeping its hue, value, and alpha unchanged.
Declaration
public static void SetSaturation(this ref Color color, float saturation)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to modify in place. |
| float | saturation | The new saturation in the [0, 1] range. Values outside this range are clamped. |
Remarks
This is a ref mutating extension method. For a non-mutating copy, use
WithSaturation(Color, float) instead.
SnapToWebSafe(Color)
Snaps a UnityEngine.Color to the nearest color in the classic "web-safe" 216-color
palette, where each RGB channel is restricted to one of six evenly-spaced values:
0, 51, 102, 153, 204, 255 (the hex steps 00, 33, 66, 99, CC, FF).
Alpha is preserved unchanged.
Declaration
public static Color SnapToWebSafe(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to snap. Alpha is passed through unchanged. |
Returns
| Type | Description |
|---|---|
| Color | A new UnityEngine.Color whose RGB channels lie on the web-safe palette. |
Remarks
Each channel is quantised by mapping its [0, 1] value to the nearest of six
steps: index = round(channel * 5), then snapped = index / 5.
Because 255 / 51 == 5 exactly, this matches the canonical 8-bit web-safe
steps with correct nearest-rounding at the midpoints. The operation is idempotent:
snapping an already web-safe color returns the same color. Web-safe is an RGB
concept, so alpha is never quantised.
SnapToWebSafe(Color32)
Snaps a UnityEngine.Color32 to the nearest color in the web-safe 216-color palette,
where each RGB channel is restricted to one of 0, 51, 102, 153, 204, 255.
Alpha is preserved unchanged.
Declaration
public static Color32 SnapToWebSafe(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to snap. Alpha is passed through unchanged. |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 whose RGB channels lie on the web-safe palette. |
Remarks
Each channel byte is mapped to the nearest multiple of 51 via
round(channel / 51) * 51. This is the byte-domain equivalent of
SnapToWebSafe(Color) and produces matching results. The operation is
idempotent and leaves alpha untouched.
ToColor(string)
Parses a hex color string into a Color via string extension. Supports 6-character (RRGGBB) and 8-character (RRGGBBAA) formats, with optional '#' prefix.
Declaration
public static Color ToColor(this string hex)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | The hex string to parse (e.g. "#FF6600", "FF660080"). |
Returns
| Type | Description |
|---|---|
| Color | The parsed color, or black if parsing fails. |
Remarks
Color orange = "#FF6600".ToColor();
Color semiTransparent = "#FF660080".ToColor();
ToColor32(string)
Parses a hex color string into a Color32 via string extension. Supports 6-character (RRGGBB) and 8-character (RRGGBBAA) formats, with optional '#' prefix.
Declaration
public static Color32 ToColor32(this string hex)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | The hex string to parse (e.g. "#FF6600", "FF660080"). |
Returns
| Type | Description |
|---|---|
| Color32 | The parsed Color32, or black if parsing fails. |
ToHSL(Color)
Converts an RGB UnityEngine.Color to HSL (Hue, Saturation, Lightness) components. All components are returned in the [0, 1] range.
Declaration
public static Vector3 ToHSL(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The RGB color to convert. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 with (H, S, L) in [0, 1]. |
ToHSV(Color)
Decomposes a UnityEngine.Color into its HSV components and returns them as a UnityEngine.Vector3 for convenient single-call decomposition.
Declaration
public static Vector3 ToHSV(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The color to decompose. Alpha is not included in the result. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 where |
ToHSV(Color32)
Gets the HSV components of a UnityEngine.Color32 as a Vector3 (h, s, v).
Declaration
public static Vector3 ToHSV(this Color32 color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The color to convert. |
Returns
| Type | Description |
|---|---|
| Vector3 | A Vector3 containing (hue, saturation, value), all in 0-1 range. |
ToOklab(Color, out float, out float, out float)
Converts an sRGB UnityEngine.Color to Oklab (L, a, b) components. Based on Bjorn Ottosson's public domain Oklab specification.
Declaration
public static void ToOklab(this Color color, out float L, out float a, out float b)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The sRGB color to convert. |
| float | L | Output Oklab lightness (typically 0-1). |
| float | a | Output Oklab green-red axis. |
| float | b | Output Oklab blue-yellow axis. |
ToOklch(Color)
Converts an sRGB UnityEngine.Color to Oklch (Lightness, Chroma, Hue) components. Oklch is the polar-coordinate form of Oklab.
Declaration
public static Vector3 ToOklch(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The sRGB color to convert. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 with (L, C, H) where L is lightness (0-1), C is chroma (distance from neutral), and H is hue in [0, 1] (full circle). |
TryParseARGB(string, out Color)
Attempts to parse a hex color string in ARGB byte order (AARRGGBB) into a
UnityEngine.Color. Also accepts 6-character RRGGBB format with an implicit
alpha of FF. Returns false without throwing on malformed input.
Declaration
public static bool TryParseARGB(string hex, out Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | The hex string to parse, e.g. |
| Color | color | When the method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
ARGB is the byte order used by many Windows APIs, .NET's
System.Drawing.Color.ToArgb(), and the Scylla ConfigFile
color encoding (#AARRGGBB). Use TryParseHex(string, out Color) instead when the
source string is in the more common web/CSS RRGGBBAA order.
On failure, color is set to Color.black.
TryParseHex(string, out Color)
Attempts to parse a hex color string in RRGGBB or RRGGBBAA format
into a UnityEngine.Color. Returns false without throwing on malformed input.
Declaration
public static bool TryParseHex(string hex, out Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| string | hex | The hex string to parse, e.g. |
| Color | color | When the method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The method accepts an optional leading # character which is stripped before
parsing. The remaining string must be exactly 6 (RGB, fully opaque) or 8 (RGBA)
uppercase or lowercase hex characters. Any other length returns false.
On failure, color is set to Color.black.
WithAlpha(Color, float)
Returns a copy of the color with a new alpha channel value.
Declaration
public static Color WithAlpha(this Color color, float alpha)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | alpha | The new alpha value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified alpha channel. |
WithAlpha(Color32, byte)
Returns a copy of the UnityEngine.Color32 with a new alpha channel value.
Declaration
public static Color32 WithAlpha(this Color32 color, byte alpha)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| byte | alpha | The new alpha value (0-255). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified alpha channel. |
WithBlue(Color, float)
Returns a copy of the color with a new blue channel value.
Declaration
public static Color WithBlue(this Color color, float blue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | blue | The new blue value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified blue channel. |
WithBlue(Color32, byte)
Returns a copy of the UnityEngine.Color32 with a new blue channel value.
Declaration
public static Color32 WithBlue(this Color32 color, byte blue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| byte | blue | The new blue value (0-255). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified blue channel. |
WithBrightness(Color, float)
Returns a copy of the color with a new brightness value.
Declaration
public static Color WithBrightness(this Color color, float brightness)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | brightness | The new brightness (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified brightness. |
WithBrightness(Color32, float)
Returns a copy of the UnityEngine.Color32 with a new brightness (value), preserving hue, saturation, and alpha.
Declaration
public static Color32 WithBrightness(this Color32 color, float brightness)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| float | brightness | The new brightness (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified brightness. |
WithGreen(Color, float)
Returns a copy of the color with a new green channel value.
Declaration
public static Color WithGreen(this Color color, float green)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | green | The new green value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified green channel. |
WithGreen(Color32, byte)
Returns a copy of the UnityEngine.Color32 with a new green channel value.
Declaration
public static Color32 WithGreen(this Color32 color, byte green)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| byte | green | The new green value (0-255). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified green channel. |
WithHue(Color, float)
Returns a copy of the color with a new hue value.
Declaration
public static Color WithHue(this Color color, float hue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | hue | The new hue value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified hue. |
WithHue(Color32, float)
Returns a copy of the UnityEngine.Color32 with a new hue value, preserving saturation, value, and alpha.
Declaration
public static Color32 WithHue(this Color32 color, float hue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| float | hue | The new hue value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified hue. |
WithLightness(Color, float)
Returns a copy of the color with the specified HSL lightness.
Declaration
public static Color WithLightness(this Color color, float lightness)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The source color. |
| float | lightness | The target lightness in [0, 1]. |
Returns
| Type | Description |
|---|---|
| Color | A new color with the specified lightness, preserving hue and saturation. |
WithRed(Color, float)
Returns a copy of the color with a new red channel value.
Declaration
public static Color WithRed(this Color color, float red)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | red | The new red value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified red channel. |
WithRed(Color32, byte)
Returns a copy of the UnityEngine.Color32 with a new red channel value.
Declaration
public static Color32 WithRed(this Color32 color, byte red)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| byte | red | The new red value (0-255). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified red channel. |
WithSaturation(Color, float)
Returns a copy of the color with a new saturation value.
Declaration
public static Color WithSaturation(this Color color, float saturation)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | The original color. |
| float | saturation | The new saturation value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color | A new color with the modified saturation. |
WithSaturation(Color32, float)
Returns a copy of the UnityEngine.Color32 with a new saturation value, preserving hue, value, and alpha.
Declaration
public static Color32 WithSaturation(this Color32 color, float saturation)
Parameters
| Type | Name | Description |
|---|---|---|
| Color32 | color | The original color. |
| float | saturation | The new saturation value (0-1 range). |
Returns
| Type | Description |
|---|---|
| Color32 | A new UnityEngine.Color32 with the modified saturation. |