Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Color

struct in Color

Description

(code of this item is picked from Godot Engine in compliance with MIT license).

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 CanvasItem.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 and the r8 / g8 / b8 / a8 properties.

Constructors

SignatureDescription
( float r, float g, float b, float a )Constructs a Color from RGBA values, typically on the range of 0 to 1.
( Color c, float a )Constructs a Color from an existing color and an alpha value.
( uint rgba )Constructs a Color from an unsigned 32-bit integer in RGBA format (each byte represents a color channel).
( ulong rgba )Constructs a Color from an unsigned 64-bit integer in RGBA format (each word represents a color channel).
( string code )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.
( string code, float alpha )Constructs a Color either from an HTML color code or from a standardized color name, with name on the range of 0 to 1. Supported color names are the same as the Colors constants.

Properties

NameTypeAccessDescription
R8byteget / setWrapper for R that uses the range 0 to 255 instead of 0 to 1. Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
G8byteget / setWrapper for G that uses the range 0 to 255 instead of 0 to 1. Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
B8byteget / setWrapper for B that uses the range 0 to 255 instead of 0 to 1. Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
A8byteget / setWrapper for A that uses the range 0 to 255 instead of 0 to 1. Getting is equivalent to multiplying by 255 and rounding. Setting is equivalent to dividing by 255.
Hfloatget / setThe HSV hue of this color, on the range 0 to 1. Getting is a long process, refer to the source code for details. Setting uses FromHsv .
Sfloatget / setThe HSV saturation of this color, on the range 0 to 1. Getting is equivalent to the ratio between the min and max RGB value. Setting uses FromHsv .
Vfloatget / setThe HSV value (brightness) of this color, on the range 0 to 1. Getting is equivalent to using Math.Max(float, float) on the RGB components. Setting uses FromHsv .
LuminancefloatgetReturns 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.

Methods

Return TypeSignatureDescription
ColorBlend ( Color over )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.
ColorClamp ( Color? min, Color? max )Returns a new color with all components clamped between the components of name and name using Mathf.Clamp(float, float, float) .
ColorDarkened ( float amount )Returns a new color resulting from making this color darker by the specified ratio (on the range of 0 to 1).
ColorInverted ( )Returns the inverted color: (1 - r, 1 - g, 1 - b, a) .
ColorLightened ( float amount )Returns a new color resulting from making this color lighter by the specified ratio (on the range of 0 to 1).
ColorLerp ( Color to, float weight )Returns the result of the linear interpolation between this color and name by amount name .
ColorLinearToSrgb ( )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.
ColorSrgbToLinear ( )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.
uintToAbgr32 ( )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.
ulongToAbgr64 ( )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.
uintToArgb32 ( )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.
ulongToArgb64 ( )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.
uintToRgba32 ( )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.
ulongToRgba64 ( )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.
stringToHtml ( bool includeAlpha )Returns the color's HTML hexadecimal color string in RGBA format.
voidToHsv ( float hue, float saturation, float value )Converts a color to HSV values. This is equivalent to using each of the h / s / v properties, but much more efficient.
boolEquals ( object? obj )Returns true if this color and name are equal.
boolEquals ( Color other )Returns true if the colors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
boolIsEqualApprox ( Color other )Returns true if this color and name are approximately equal, by running Mathf.IsEqualApprox(float, float) on each component.
intGetHashCode ( )Serves as the hash function for Color .
stringToString ( )Converts this Color to a string.
stringToString ( string? format )Converts this Color to a string with the given name .

Static Methods

Return TypeSignatureDescription
ColorFromHtml ( ReadOnlySpan rgba )Constructs a Color from the HTML hexadecimal color string in RGBA format.
ColorColor8 ( byte r8, byte g8, byte b8, byte a8 )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.
ColorFromHsv ( float hue, float saturation, float value, float alpha )Constructs a color from an HSV profile. The name , name , and name are typically between 0.0 and 1.0.
ColorFromRgbe9995 ( uint rgbe )Encodes a Color from a RGBE9995 format integer. See Image.Format.Rgbe9995 .
ColorFromString ( string str, Color @default )Constructs a color from the given string, which can be either an HTML color code or a named color. Returns name if the color cannot be inferred from the string. Supported color names are the same as the Colors constants.
boolHtmlIsValid ( ReadOnlySpan color )Returns true if name 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 StringExtensions.IsValidHtmlColor(string) .

Operators

Return TypeSignatureDescription
Color+ ( Color left, Color right )Adds each component of the Color with the components of the given Color .
Color- ( Color left, Color right )Subtracts each component of the Color by the components of the given Color .
Color- ( Color 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) .
Color* ( Color color, float scale )Multiplies each component of the Color by the given float .
Color* ( float scale, Color color )Multiplies each component of the Color by the given float .
Color* ( Color left, Color right )Multiplies each component of the Color by the components of the given Color .
Color/ ( Color color, float scale )Divides each component of the Color by the given float .
Color/ ( Color left, Color right )Divides each component of the Color by the components of the given Color .
bool== ( Color left, Color right )Returns true if the colors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
bool!= ( Color left, Color right )Returns true if the colors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
bool< ( Color left, Color right )Compares two Color s by first checking if the red value of the name color is less than the red value of the name 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.
bool> ( Color left, Color right )Compares two Color s by first checking if the red value of the name color is greater than the red value of the name 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.
bool<= ( Color left, Color right )Compares two Color s by first checking if the red value of the name color is less than or equal to the red value of the name 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.
bool>= ( Color left, Color right )Compares two Color s by first checking if the red value of the name color is greater than or equal to the red value of the name 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.