Skip to content

Color Extensions

Andrzej Kebab edited this page Feb 2, 2024 · 2 revisions

Unity Runtime Utility Library

Deconstruct

Deconstructs a Color instance into its red, green, and blue components.

Color color = // Your Color instance;
float r, g, b;
color.Deconstruct(out r, out g, out b);

Parameters:

  • color: The Color instance.
  • r: The red component of the Color.
  • g: The green component of the Color.
  • b: The blue component of the Color.

Deconstructs a Color instance into its red, green, blue, and alpha components.

Color color = // Your Color instance;
float r, g, b, a;
color.Deconstruct(out r, out g, out b, out a);

Parameters:

  • color: The Color instance.
  • r: The red component of the Color.
  • g: The green component of the Color.
  • b: The blue component of the Color.
  • a: The alpha component of the Color.

Deconstructs a Color32 instance into its red, green, and blue components.

Color32 color = // Your Color32 instance;
byte r, g, b;
color.Deconstruct(out r, out g, out b);

Parameters:

  • color: The Color32 instance.
  • r: The red component of the Color32.
  • g: The green component of the Color32.
  • b: The blue component of the Color32.

Deconstructs a Color32 instance into its red, green, blue, and alpha components.

Color32 color = // Your Color32 instance;
byte r, g, b, a;
color.Deconstruct(out r, out g, out b, out a);

Parameters:

  • color: The Color32 instance.
  • r: The red component of the Color32.
  • g: The green component of the Color32.
  • b: The blue component of the Color32.
  • a: The alpha component of the Color32.

With

Creates a new Color instance with the specified red, green, blue, and alpha components, or the original components if not specified.

Color color = // Your original Color instance;
float? r = // Your new red component, or null to keep the original red component;
float? g = // Your new green component, or null to keep the original green component;
float? b = // Your new blue component, or null to keep the original blue component;
float? a = // Your new alpha component, or null to keep the original alpha component;
Color result = color.With(r, g, b, a);

Parameters:

  • color: The original Color instance.
  • r: The red component of the new Color. If null, the red component of the original Color is used.
  • g: The green component of the new Color. If null, the green component of the original Color is used.
  • b: The blue component of the new Color. If null, the blue component of the original Color is used.
  • a: The alpha component of the new Color. If null, the alpha component of the original Color is used.

Returns:

  • result: A new Color instance with the specified red, green, blue, and alpha components.

Creates a new Color32 instance with the specified red, green, blue, and alpha components, or the original components if not specified.

Color32 color = // Your original Color32 instance;
byte? r = // Your new red component, or null to keep the original red component;
byte? g = // Your new green component, or null to keep the original green component;
byte? b = // Your new blue component, or null to keep the original blue component;
byte? a = // Your new alpha component, or null to keep the original alpha component;
Color32 result = color.With(r, g, b, a);

Parameters:

  • color: The original Color32 instance.
  • r: The red component of the new Color32. If null, the red component of the original Color32 is used.
  • g: The green component of the new Color32. If null, the green component of the original Color32 is used.
  • b: The blue component of the new Color32. If null, the blue component of the original Color32 is used.
  • a: The alpha component of the new Color32. If null, the alpha component of the original Color32 is used.

Returns:

  • result: A new Color32 instance with the specified red, green, blue, and alpha components.

RGB

Sets the red component of a Color instance.

ref Color color = // Your Color instance;
float r = // Your new red component;
color.SetR(r);

Parameters:

  • color: The Color instance.
  • r: The red component to set.

Returns the Color instance with the new red component.

Sets the green component of a Color instance.

ref Color color = // Your Color instance;
float g = // Your new green component;
color.SetG(g);

Parameters:

  • color: The Color instance.
  • g: The green component to set.

Returns the Color instance with the new green component.

Sets the blue component of a Color instance.

ref Color color = // Your Color instance;
float b = // Your new blue component;
color.SetB(b);

Parameters:

  • color: The Color instance.
  • b: The blue component to set.

Returns the Color instance with the new blue component.

Sets the alpha component of a Color instance.

ref Color color = // Your Color instance;
float a = // Your new alpha component;
color.SetA(a);

Parameters:

  • color: The Color instance.
  • a: The alpha component to set.

Returns the Color instance with the new alpha component.

Gets the RGB value of a Color instance.

Color color = // Your Color instance;
int rgbValue = color.GetRgbValue();

Parameters:

  • color: The Color instance.

Returns:

  • rgbValue: The RGB value of the Color instance

.

HSV

Gets the hue of a Color instance.

Color color = // Your Color instance;
float hue = color.GetH();

Parameters:

  • color: The Color instance.

Returns:

  • hue: The hue of the Color instance.

Gets the saturation of a Color instance.

Color color = // Your Color instance;
float saturation = color.GetS();

Parameters:

  • color: The Color instance.

Returns:

  • saturation: The saturation of the Color instance.

Gets the value of a Color instance.

Color color = // Your Color instance;
float value = color.GetV();

Parameters:

  • color: The Color instance.

Returns:

  • value: The value of the Color instance.

Sets the hue of a Color instance.

ref Color color = // Your Color instance;
float hue = // Your new hue;
color.SetH(hue);

Parameters:

  • color: The Color instance.
  • hue: The hue to set.

Returns the Color instance with the new hue.

Sets the saturation of a Color instance.

ref Color color = // Your Color instance;
float saturation = // Your new saturation;
color.SetS(saturation);

Parameters:

  • color: The Color instance.
  • saturation: The saturation to set.

Returns the Color instance with the new saturation.

Sets the value of a Color instance.

ref Color color = // Your Color instance;
float value = // Your new value;
color.SetV(value);

Parameters:

  • color: The Color instance.
  • value: The value to set.

Returns the Color instance with the new value.

Sets the hue, saturation, and value of a Color instance.

ref Color color = // Your Color instance;
float hue = // Your new hue;
float saturation = // Your new saturation;
float value = // Your new value;
color.SetHsv(hue, saturation, value);

Parameters:

  • color: The Color instance.
  • hue: The hue to set.
  • saturation: The saturation to set.
  • value: The value to set.

Returns the Color instance with the new hue, saturation, and value.

Color SDR

Gets the complementary color of a Color instance.

Color color = // Your Color instance;
bool inverseAlpha = // Whether to inverse the alpha component or not;
Color complementaryColor = color.ComplementaryColor(inverseAlpha);

Parameters:

  • color: The Color instance.
  • inverseAlpha: Whether to inverse the alpha component or not. Default is false.

Returns:

  • complementaryColor: The complementary color of the Color instance.

Gets the gray level of a Color instance.

Color color = // Your Color instance;
float grayLevel = color.GrayLevel();

Parameters:

  • color: The Color instance.

Returns:

  • grayLevel: The gray level of the Color instance.

Gets the perceived brightness of a Color instance.

Color color = // Your Color instance;
float perceivedBrightness = color.GetPerceivedBrightness();

Parameters:

  • color: The Color instance.

Returns:

  • perceivedBrightness: The perceived brightness of the Color instance.

ARGB32

Converts a Color instance to a 32-bit ARGB value.

Color color = // Your Color instance;
uint argb32 = color.ToArgb32();

Parameters:

  • color: The Color instance.

Returns:

  • argb32: A 32-bit unsigned integer representing the ARGB value of the Color instance.

Converts a 32-bit ARGB value to a Color instance.

ref Color color = // Your Color instance;
uint argb32 = // Your 32-bit ARGB value;
color.FromArgb32(argb32);

Parameters:

  • color: The Color instance.
  • argb32: The 32-bit ARGB value.

Returns:

  • color: A Color instance representing the ARGB value.

Normalize

Normalizes the RGB components of a Color instance.

ref Color color = // Your Color instance;
color.Normalize();

Parameters:

  • color: The Color instance.

Returns:

  • color: The Color instance with normalized RGB components.

Clamp

Clamps the RGBA components of a Color instance between specified minimum and maximum values.

ref Color color = // Your Color instance;
float min = // Your minimum value for the RGBA components;
float max = // Your maximum value for the RGBA components;
color.Clamp(min, max);

Parameters:

  • color: The Color instance.
  • min: The minimum value for the RGBA components.
  • max: The maximum value for the RGBA components.

Returns:

  • color: The Color instance with clamped RGBA components.

Clamps the RGBA components of a Color instance between specified minimum and maximum Color values.

ref Color color = // Your Color instance;
Color min = // Your minimum Color value for the RGBA components;
Color max = // Your maximum Color value for the RGBA components;
color.Clamp(min, max);

Parameters:

  • color: The Color instance.
  • min: The minimum Color value for the RGBA components.
  • max: The maximum Color value for the RGBA components.

Returns:

  • color: The Color instance with clamped RGBA components.

Clone this wiki locally