Struct Color
- Namespace
- Godot
- Assembly
- GodotSharp.dll
A color represented by red, green, blue, and alpha (RGBA) components. The alpha component is often used for transparency. Values are in floating-point and usually range from 0 to 1. Some properties (such as Modulate) may accept values greater than 1 (overbright or HDR colors).
If you want to supply values in a range of 0 to 255, you should use
Color8(byte, byte, byte, byte) and the r8
/g8
/b8
/a8
properties.
public struct Color : IEquatable<Color>
- Implements
- Inherited Members
Constructors
Color(Color, float)
Constructs a Color from an existing color and an alpha value.
public Color(Color c, float a = 1)
Parameters
c
ColorThe color to construct from. Only its RGB values are used.
a
floatThe color's alpha (transparency) value, typically on the range of 0 to 1. Default: 1.
Color(float, float, float, float)
Constructs a Color from RGBA values, typically on the range of 0 to 1.
public Color(float r, float g, float b, float a = 1)
Parameters
r
floatThe color's red component, typically on the range of 0 to 1.
g
floatThe color's green component, typically on the range of 0 to 1.
b
floatThe color's blue component, typically on the range of 0 to 1.
a
floatThe color's alpha (transparency) value, typically on the range of 0 to 1. Default: 1.
Color(string)
Constructs a Color either from an HTML color code or from a standardized color name. Supported color names are the same as the Colors constants.
public Color(string code)
Parameters
code
stringThe HTML color code or color name to construct from.
Exceptions
- ArgumentOutOfRangeException
A color cannot be inferred from the given
code
. It was invalid HTML and a color with that name was not found.
Color(string, float)
Constructs a Color either from an HTML color code or from a
standardized color name, with alpha
on the range of 0 to 1. Supported
color names are the same as the Colors constants.
public Color(string code, float alpha)
Parameters
code
stringThe HTML color code or color name to construct from.
alpha
floatThe alpha (transparency) value, typically on the range of 0 to 1.
Color(uint)
Constructs a Color from an unsigned 32-bit integer in RGBA format (each byte represents a color channel).
public Color(uint rgba)
Parameters
Color(ulong)
Constructs a Color from an unsigned 64-bit integer in RGBA format (each word represents a color channel).
public Color(ulong rgba)
Parameters
Fields
A
The color's alpha (transparency) component, typically on the range of 0 to 1.
public float A
Field Value
B
The color's blue component, typically on the range of 0 to 1.
public float B
Field Value
G
The color's green component, typically on the range of 0 to 1.
public float G
Field Value
R
The color's red component, typically on the range of 0 to 1.
public float R
Field Value
Properties
A8
Wrapper for A that uses the range 0 to 255 instead of 0 to 1.
public int A8 { readonly get; set; }
Property Value
- int
Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
B8
Wrapper for B that uses the range 0 to 255 instead of 0 to 1.
public int B8 { readonly get; set; }
Property Value
- int
Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
G8
Wrapper for G that uses the range 0 to 255 instead of 0 to 1.
public int G8 { readonly get; set; }
Property Value
- int
Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
H
The HSV hue of this color, on the range 0 to 1.
public float H { readonly get; set; }
Property Value
- float
Getting is a long process, refer to the source code for details. Setting uses FromHsv(float, float, float, float).
this[int]
Access color components using their index.
public float this[int index] { readonly get; set; }
Parameters
index
int
Property Value
- float
[0]
is equivalent to R,[1]
is equivalent to G,[2]
is equivalent to B,[3]
is equivalent to A.
Luminance
Returns the light intensity of the color, as a value between 0.0 and 1.0 (inclusive). This is useful when determining light or dark color. Colors with a luminance smaller than 0.5 can be generally considered dark. Note: Luminance relies on the color being in the linear color space to return an accurate relative luminance value. If the color is in the sRGB color space use SrgbToLinear() to convert it to the linear color space first.
public readonly float Luminance { get; }
Property Value
R8
Wrapper for R that uses the range 0 to 255 instead of 0 to 1.
public int R8 { readonly get; set; }
Property Value
- int
Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
S
The HSV saturation of this color, on the range 0 to 1.
public float S { readonly get; set; }
Property Value
- float
Getting is equivalent to the ratio between the min and max RGB value. Setting uses FromHsv(float, float, float, float).
V
The HSV value (brightness) of this color, on the range 0 to 1.
public float V { readonly get; set; }
Property Value
- float
Getting is equivalent to using Max(float, float) on the RGB components. Setting uses FromHsv(float, float, float, float).
Methods
Blend(Color)
Returns a new color resulting from blending this color over another. If the color is opaque, the result is also opaque. The second color may have a range of alpha values.
public readonly Color Blend(Color over)
Parameters
over
ColorThe color to blend over.
Returns
- Color
This color blended over
over
.
Clamp(Color?, Color?)
Returns a new color with all components clamped between the
components of min
and max
using Clamp(float, float, float).
public readonly Color Clamp(Color? min = null, Color? max = null)
Parameters
Returns
- Color
The color with all components clamped.
Color8(byte, byte, byte, byte)
Returns a color constructed from integer red, green, blue, and alpha channels. Each channel should have 8 bits of information ranging from 0 to 255.
public static Color Color8(byte r8, byte g8, byte b8, byte a8 = 255)
Parameters
r8
byteThe red component represented on the range of 0 to 255.
g8
byteThe green component represented on the range of 0 to 255.
b8
byteThe blue component represented on the range of 0 to 255.
a8
byteThe alpha (transparency) component represented on the range of 0 to 255.
Returns
- Color
The constructed color.
Darkened(float)
Returns a new color resulting from making this color darker by the specified ratio (on the range of 0 to 1).
public readonly Color Darkened(float amount)
Parameters
amount
floatThe ratio to darken by.
Returns
- Color
The darkened color.
Equals(Color)
Returns true if the colors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Color) instead, which is more reliable.
public readonly bool Equals(Color other)
Parameters
other
ColorThe other color.
Returns
- bool
Whether or not the colors are equal.
Equals(object)
Returns true if this color and obj
are equal.
public override readonly bool Equals(object obj)
Parameters
obj
objectThe other object to compare.
Returns
- bool
Whether or not the color and the other object are equal.
FromHsv(float, float, float, float)
Constructs a color from an HSV profile. The hue
,
saturation
, and value
are typically
between 0.0 and 1.0.
public static Color FromHsv(float hue, float saturation, float value, float alpha = 1)
Parameters
hue
floatThe HSV hue, typically on the range of 0 to 1.
saturation
floatThe HSV saturation, typically on the range of 0 to 1.
value
floatThe HSV value (brightness), typically on the range of 0 to 1.
alpha
floatThe alpha (transparency) value, typically on the range of 0 to 1.
Returns
- Color
The constructed color.
FromHtml(ReadOnlySpan<char>)
Constructs a Color from the HTML hexadecimal color string in RGBA format.
public static Color FromHtml(ReadOnlySpan<char> rgba)
Parameters
rgba
ReadOnlySpan<char>A string for the HTML hexadecimal representation of this color.
Returns
FromOkHsl(float, float, float, float)
Constructs a color from an OK HSL profile. The hue
,
saturation
, and lightness
are typically
between 0.0 and 1.0.
public static Color FromOkHsl(float hue, float saturation, float lightness, float alpha = 1)
Parameters
hue
floatThe OK HSL hue, typically on the range of 0 to 1.
saturation
floatThe OK HSL saturation, typically on the range of 0 to 1.
lightness
floatThe OK HSL lightness, typically on the range of 0 to 1.
alpha
floatThe alpha (transparency) value, typically on the range of 0 to 1.
Returns
- Color
The constructed color.
FromRgbe9995(uint)
public static Color FromRgbe9995(uint rgbe)
Parameters
rgbe
uintThe RGBE9995 encoded color.
Returns
- Color
The constructed color.
FromString(string, Color)
Constructs a color from the given string, which can be either an HTML color
code or a named color. Returns default
if the color cannot
be inferred from the string. Supported color names are the same as the
Colors constants.
public static Color FromString(string str, Color @default)
Parameters
str
stringThe HTML color code or color name.
default
ColorThe fallback color to return if the color cannot be inferred.
Returns
- Color
The constructed color.
GetHashCode()
Serves as the hash function for Color.
public override readonly int GetHashCode()
Returns
- int
A hash code for this color.
HtmlIsValid(ReadOnlySpan<char>)
Returns true if color
is a valid HTML hexadecimal
color string. The string must be a hexadecimal value (case-insensitive) of either 3,
4, 6 or 8 digits, and may be prefixed by a hash sign (#
). This method is
identical to IsValidHtmlColor(string).
public static bool HtmlIsValid(ReadOnlySpan<char> color)
Parameters
color
ReadOnlySpan<char>The HTML hexadecimal color string.
Returns
- bool
Whether or not the string was a valid HTML hexadecimal color string.
Inverted()
Returns the inverted color: (1 - r, 1 - g, 1 - b, a)
.
public readonly Color Inverted()
Returns
- Color
The inverted color.
IsEqualApprox(Color)
Returns true if this color and other
are approximately equal,
by running IsEqualApprox(float, float) on each component.
public readonly bool IsEqualApprox(Color other)
Parameters
other
ColorThe other color to compare.
Returns
- bool
Whether or not the colors are approximately equal.
Lerp(Color, float)
Returns the result of the linear interpolation between
this color and to
by amount weight
.
public readonly Color Lerp(Color to, float weight)
Parameters
to
ColorThe destination color for interpolation.
weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Color
The resulting color of the interpolation.
Lightened(float)
Returns a new color resulting from making this color lighter by the specified ratio (on the range of 0 to 1).
public readonly Color Lightened(float amount)
Parameters
amount
floatThe ratio to lighten by.
Returns
- Color
The lightened color.
LinearToSrgb()
Returns the color converted to the sRGB color space. This method assumes the original color is in the linear color space. See also SrgbToLinear() which performs the opposite operation.
public readonly Color LinearToSrgb()
Returns
- Color
The sRGB color.
SrgbToLinear()
Returns the color converted to linear color space. This method assumes the original color already is in sRGB color space. See also LinearToSrgb() which performs the opposite operation.
public readonly Color SrgbToLinear()
Returns
- Color
The color in linear color space.
ToAbgr32()
Returns the color converted to an unsigned 32-bit integer in ABGR format (each byte represents a color channel). ABGR is the reversed version of the default format.
public readonly uint ToAbgr32()
Returns
ToAbgr64()
Returns the color converted to an unsigned 64-bit integer in ABGR format (each word represents a color channel). ABGR is the reversed version of the default format.
public readonly ulong ToAbgr64()
Returns
ToArgb32()
Returns the color converted to an unsigned 32-bit integer in ARGB format (each byte represents a color channel). ARGB is more compatible with DirectX, but not used much in Godot.
public readonly uint ToArgb32()
Returns
ToArgb64()
Returns the color converted to an unsigned 64-bit integer in ARGB format (each word represents a color channel). ARGB is more compatible with DirectX, but not used much in Godot.
public readonly ulong ToArgb64()
Returns
ToHsv(out float, out float, out float)
Converts a color to HSV values. This is equivalent to using each of
the h
/s
/v
properties, but much more efficient.
public readonly void ToHsv(out float hue, out float saturation, out float value)
Parameters
hue
floatOutput parameter for the HSV hue.
saturation
floatOutput parameter for the HSV saturation.
value
floatOutput parameter for the HSV value.
ToHtml(bool)
Returns the color's HTML hexadecimal color string in RGBA format.
public readonly string ToHtml(bool includeAlpha = true)
Parameters
Returns
- string
A string for the HTML hexadecimal representation of this color.
ToRgba32()
Returns the color converted to an unsigned 32-bit integer in RGBA format (each byte represents a color channel). RGBA is Godot's default and recommended format.
public readonly uint ToRgba32()
Returns
ToRgba64()
Returns the color converted to an unsigned 64-bit integer in RGBA format (each word represents a color channel). RGBA is Godot's default and recommended format.
public readonly ulong ToRgba64()
Returns
ToString()
Converts this Color to a string.
public override readonly string ToString()
Returns
- string
A string representation of this color.
ToString(string)
Converts this Color to a string with the given format
.
public readonly string ToString(string format)
Parameters
format
string
Returns
- string
A string representation of this color.
Operators
operator +(Color, Color)
public static Color operator +(Color left, Color right)
Parameters
Returns
- Color
The added color.
operator /(Color, Color)
public static Color operator /(Color left, Color right)
Parameters
Returns
- Color
The divided color.
operator /(Color, float)
public static Color operator /(Color color, float scale)
Parameters
Returns
- Color
The divided color.
operator ==(Color, Color)
Returns true if the colors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Color) instead, which is more reliable.
public static bool operator ==(Color left, Color right)
Parameters
Returns
- bool
Whether or not the colors are equal.
operator >(Color, Color)
Compares two Colors by first checking if
the red value of the left
color is greater than
the red value of the right
color.
If the red values are exactly equal, then it repeats this check
with the green values of the two colors, then with the blue values,
and then with the alpha value.
This operator is useful for sorting colors.
public static bool operator >(Color left, Color right)
Parameters
Returns
- bool
Whether or not the left is greater than the right.
operator >=(Color, Color)
Compares two Colors by first checking if
the red value of the left
color is greater than
or equal to the red value of the right
color.
If the red values are exactly equal, then it repeats this check
with the green values of the two colors, then with the blue values,
and then with the alpha value.
This operator is useful for sorting colors.
public static bool operator >=(Color left, Color right)
Parameters
Returns
- bool
Whether or not the left is greater than or equal to the right.
operator !=(Color, Color)
Returns true if the colors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Color) instead, which is more reliable.
public static bool operator !=(Color left, Color right)
Parameters
Returns
- bool
Whether or not the colors are equal.
operator <(Color, Color)
Compares two Colors by first checking if
the red value of the left
color is less than
the red value of the right
color.
If the red values are exactly equal, then it repeats this check
with the green values of the two colors, then with the blue values,
and then with the alpha value.
This operator is useful for sorting colors.
public static bool operator <(Color left, Color right)
Parameters
Returns
- bool
Whether or not the left is less than the right.
operator <=(Color, Color)
Compares two Colors by first checking if
the red value of the left
color is less than
or equal to the red value of the right
color.
If the red values are exactly equal, then it repeats this check
with the green values of the two colors, then with the blue values,
and then with the alpha value.
This operator is useful for sorting colors.
public static bool operator <=(Color left, Color right)
Parameters
Returns
- bool
Whether or not the left is less than or equal to the right.
operator *(Color, Color)
public static Color operator *(Color left, Color right)
Parameters
Returns
- Color
The multiplied color.
operator *(Color, float)
public static Color operator *(Color color, float scale)
Parameters
Returns
- Color
The multiplied color.
operator *(float, Color)
public static Color operator *(float scale, Color color)
Parameters
Returns
- Color
The multiplied color.
operator -(Color, Color)
public static Color operator -(Color left, Color right)
Parameters
Returns
- Color
The subtracted color.
operator -(Color)
Inverts the given color. This is equivalent to
Colors.White - c
or
new Color(1 - c.R, 1 - c.G, 1 - c.B, 1 - c.A)
.
public static Color operator -(Color color)
Parameters
color
ColorThe color to invert.
Returns
- Color
The inverted color.