godot::prelude

Struct Color

#[repr(C)]
pub struct Color { pub r: f32, pub g: f32, pub b: f32, pub a: f32, }
Expand description

Color built-in type, in floating-point RGBA format.

Channel values are typically in the range of 0 to 1, but this is not a requirement, and values outside this range are explicitly allowed for e.g. High Dynamic Range (HDR).

To access its HSVA representation, use Color::to_hsv.

§Godot docs

Color (stable)

Fields§

§r: f32

The color’s red component.

§g: f32

The color’s green component.

§b: f32

The color’s blue component.

§a: f32

The color’s alpha component. A value of 0 means that the color is fully transparent. A value of 1 means that the color is fully opaque.

Implementations§

§

impl Color

pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Color

Constructs a new Color with the given components.

pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color

Constructs a new Color with the given color components, and the alpha channel set to 1.

pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Color

Constructs a new Color with the given components as bytes. 0 is mapped to 0.0, 255 is mapped to 1.0.

Godot equivalent: the global Color8 function

pub fn from_rgba16(r: u16, g: u16, b: u16, a: u16) -> Color

Constructs a new Color with the given components as u16 words. 0 is mapped to 0.0, 65535 (0xffff) is mapped to 1.0.

pub fn from_u32_rgba(u: u32, order: ColorChannelOrder) -> Color

Constructs a new Color from a 32-bits value with the given channel order.

Godot equivalent: Color.hex, if ColorChannelOrder::Rgba is used

pub fn from_u64_rgba(u: u64, order: ColorChannelOrder) -> Color

Constructs a new Color from a 64-bits value with the given channel order.

Godot equivalent: Color.hex64, if ColorChannelOrder::Rgba is used

pub fn from_html<S>(html: S) -> Option<Color>
where S: AsArg<GString>,

Constructs a Color from an HTML color code string. Valid values for the string are:

  • #RRGGBBAA and RRGGBBAA where each of RR, GG, BB and AA stands for two hex digits (case insensitive).
  • #RRGGBB and RRGGBB. Equivalent to #RRGGBBff.
  • #RGBA and RGBA where each of R, G, B and A stands for a single hex digit. Equivalent to #RRGGBBAA, i.e. each digit is repeated twice.
  • #RGB and RGB. Equivalent to #RRGGBBff.

Returns None if the format is invalid.

pub fn from_string(string: impl AsArg<GString>) -> Option<Color>

Constructs a Color from a string, which can be either:

  • An HTML color code as accepted by Color::from_html.
  • The name of a built-in color constant, such as BLUE or lawn-green. Matching is case-insensitive and hyphens can be used interchangeably with underscores. See the list of color constants in the Godot API documentation, or the visual cheat sheet for the full list.

Returns None if the string is neither a valid HTML color code nor an existing color name.

Most color constants have an alpha of 1; use Color::with_alpha to change it.

pub fn from_hsv(h: f64, s: f64, v: f64) -> Color

Constructs a Color from an HSV profile using Godot’s builtin method. The hue (h), saturation (s), and value (v) are typically between 0.0 and 1.0. Alpha is set to 1; use Color::with_alpha to change it.

See also: ColorHsv::to_rgb for fast conversion on Rust side.

pub fn from_ok_hsl(h: f64, s: f64, l: f64) -> Color

Constructs a Color from an OK HSL profile. The hue (h), saturation (s), and lightness (l) are typically between 0.0 and 1.0. Alpha is set to 1; use Color::with_alpha to change it.

pub fn from_rgbe9995(rgbe: u32) -> Color

Constructs a Color from an RGBE9995 format integer. This is a special OpenGL texture format where the three color components have 9 bits of precision and all three share a single 5-bit exponent.

pub fn with_alpha(self, a: f32) -> Color

Returns a copy of this color with the given alpha value. Useful for chaining with constructors like Color::from_string and Color::from_hsv.

pub fn r8(self) -> u8

Returns the red channel value as a byte. If self.r is outside the range from 0 to 1, the returned byte is clamped.

pub fn g8(self) -> u8

Returns the green channel value as a byte. If self.g is outside the range from 0 to 1, the returned byte is clamped.

pub fn b8(self) -> u8

Returns the blue channel value as a byte. If self.b is outside the range from 0 to 1, the returned byte is clamped.

pub fn a8(self) -> u8

Returns the alpha channel value as a byte. If self.a is outside the range from 0 to 1, the returned byte is clamped.

pub fn set_r8(&mut self, r: u8)

Sets the red channel value as a byte, mapped to the range from 0 to 1.

pub fn set_g8(&mut self, g: u8)

Sets the green channel value as a byte, mapped to the range from 0 to 1.

pub fn set_b8(&mut self, b: u8)

Sets the blue channel value as a byte, mapped to the range from 0 to 1.

pub fn set_a8(&mut self, a: u8)

Sets the alpha channel value as a byte, mapped to the range from 0 to 1.

pub fn luminance(self) -> f64

Returns the light intensity of the color, as a value between 0.0 and 1.0 (inclusive). This is useful when determining whether a color is light or dark. 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 Color::srgb_to_linear to convert it to the linear color space first.

pub fn blend(self, over: Color) -> Color

Blends the given color on top of this color, taking its alpha into account.

pub fn lerp(self, to: Color, weight: f64) -> Color

Returns the linear interpolation between self’s components and to’s components. The interpolation factor weight should be between 0.0 and 1.0 (inclusive).

pub fn clamp(self, min: Color, max: Color) -> Color

Returns a new color with all components clamped between the components of min and max.

pub fn darkened(self, amount: f64) -> Color

Creates a new color resulting by making this color darker by the specified amount (ratio from 0.0 to 1.0). See also lightened.

pub fn lightened(self, amount: f64) -> Color

Creates a new color resulting by making this color lighter by the specified amount, which should be a ratio from 0.0 to 1.0. See also darkened.

pub fn inverted(self) -> Color

Returns the color with its r, g, and b components inverted: Color::from_rgba(1 - r, 1 - g, 1 - b, a).

pub fn linear_to_srgb(self) -> Color

Returns the color converted to the sRGB color space. This method assumes the original color is in the linear color space. See also Color::srgb_to_linear which performs the opposite operation.

pub fn srgb_to_linear(self) -> Color

Returns the color converted to the linear color space. This method assumes the original color is in the sRGB color space. See also Color::linear_to_srgb which performs the opposite operation.

pub fn to_html(self) -> GString

Returns the HTML color code representation of this color, as 8 lowercase hex digits in the order RRGGBBAA, without the # prefix.

pub fn to_html_without_alpha(self) -> GString

Returns the HTML color code representation of this color, as 6 lowercase hex digits in the order RRGGBB, without the # prefix. The alpha channel is ignored.

pub fn to_u32(self, order: ColorChannelOrder) -> u32

Returns the color converted to a 32-bit integer (each component is 8 bits) with the given order of channels (from most to least significant byte).

pub fn to_u64(self, order: ColorChannelOrder) -> u64

Returns the color converted to a 64-bit integer (each component is 16 bits) with the given order of channels (from most to least significant word).

pub fn to_hsv(self) -> ColorHsv

⚠️ Convert Color into ColorHsv.

§Panics

Method will panic if the RGBA values are outside the valid range 0.0..=1.0. You can use Color::normalized to ensure that they are in range, or use Color::try_to_hsv.

pub fn try_to_hsv(self) -> Result<ColorHsv, String>

Fallible Color conversion into ColorHsv. See also Color::to_hsv.

pub fn normalized(self) -> Color

Clamps all components to a usually valid range 0.0..=1.0.

Useful for transformations between different color representations.

§

impl Color

Godot’s predefined colors.

This visual cheat sheet shows how the colors look.

pub const TRANSPARENT_BLACK: Color

Transparent black.

This color is not provided by Godot, so Color::from_string("TRANSPARENT_BLACK") will be None.

pub const TRANSPARENT_WHITE: Color

Transparent white.

This color is not provided by Godot, so Color::from_string("TRANSPARENT_WHITE") will be None. Use Color::from_string("TRANSPARENT") instead.

Godot equivalent: Color.TRANSPARENT

pub const BLACK: Color

Black color. This is the default value.

pub const WHITE: Color

pub const ALICE_BLUE: Color

pub const ANTIQUE_WHITE: Color

pub const AQUA: Color

pub const AQUAMARINE: Color

pub const AZURE: Color

pub const BEIGE: Color

pub const BISQUE: Color

pub const BLANCHED_ALMOND: Color

pub const BLUE: Color

pub const BLUE_VIOLET: Color

pub const BROWN: Color

pub const BURLYWOOD: Color

pub const CADET_BLUE: Color

pub const CHARTREUSE: Color

pub const CHOCOLATE: Color

pub const CORAL: Color

pub const CORNFLOWER_BLUE: Color

pub const CORNSILK: Color

pub const CRIMSON: Color

pub const CYAN: Color

pub const DARK_BLUE: Color

pub const DARK_CYAN: Color

pub const DARK_GOLDENROD: Color

pub const DARK_GRAY: Color

pub const DARK_GREEN: Color

pub const DARK_KHAKI: Color

pub const DARK_MAGENTA: Color

pub const DARK_OLIVE_GREEN: Color

pub const DARK_ORANGE: Color

pub const DARK_ORCHID: Color

pub const DARK_RED: Color

pub const DARK_SALMON: Color

pub const DARK_SEA_GREEN: Color

pub const DARK_SLATE_BLUE: Color

pub const DARK_SLATE_GRAY: Color

pub const DARK_TURQUOISE: Color

pub const DARK_VIOLET: Color

pub const DEEP_PINK: Color

pub const DEEP_SKY_BLUE: Color

pub const DIM_GRAY: Color

pub const DODGER_BLUE: Color

pub const FIREBRICK: Color

pub const FLORAL_WHITE: Color

pub const FOREST_GREEN: Color

pub const FUCHSIA: Color

pub const GAINSBORO: Color

pub const GHOST_WHITE: Color

pub const GOLD: Color

pub const GOLDENROD: Color

pub const GRAY: Color

pub const GREEN: Color

pub const GREEN_YELLOW: Color

pub const HONEYDEW: Color

pub const HOT_PINK: Color

pub const INDIAN_RED: Color

pub const INDIGO: Color

pub const IVORY: Color

pub const KHAKI: Color

pub const LAVENDER: Color

pub const LAVENDER_BLUSH: Color

pub const LAWN_GREEN: Color

pub const LEMON_CHIFFON: Color

pub const LIGHT_BLUE: Color

pub const LIGHT_CORAL: Color

pub const LIGHT_CYAN: Color

pub const LIGHT_GOLDENROD: Color

pub const LIGHT_GRAY: Color

pub const LIGHT_GREEN: Color

pub const LIGHT_PINK: Color

pub const LIGHT_SALMON: Color

pub const LIGHT_SEA_GREEN: Color

pub const LIGHT_SKY_BLUE: Color

pub const LIGHT_SLATE_GRAY: Color

pub const LIGHT_STEEL_BLUE: Color

pub const LIGHT_YELLOW: Color

pub const LIME: Color

pub const LIME_GREEN: Color

pub const LINEN: Color

pub const MAGENTA: Color

pub const MAROON: Color

pub const MEDIUM_AQUAMARINE: Color

pub const MEDIUM_BLUE: Color

pub const MEDIUM_ORCHID: Color

pub const MEDIUM_PURPLE: Color

pub const MEDIUM_SEA_GREEN: Color

pub const MEDIUM_SLATE_BLUE: Color

pub const MEDIUM_SPRING_GREEN: Color

pub const MEDIUM_TURQUOISE: Color

pub const MEDIUM_VIOLET_RED: Color

pub const MIDNIGHT_BLUE: Color

pub const MINT_CREAM: Color

pub const MISTY_ROSE: Color

pub const MOCCASIN: Color

pub const NAVAJO_WHITE: Color

pub const NAVY_BLUE: Color

pub const OLD_LACE: Color

pub const OLIVE: Color

pub const OLIVE_DRAB: Color

pub const ORANGE: Color

pub const ORANGE_RED: Color

pub const ORCHID: Color

pub const PALE_GOLDENROD: Color

pub const PALE_GREEN: Color

pub const PALE_TURQUOISE: Color

pub const PALE_VIOLET_RED: Color

pub const PAPAYA_WHIP: Color

pub const PEACH_PUFF: Color

pub const PERU: Color

pub const PINK: Color

pub const PLUM: Color

pub const POWDER_BLUE: Color

pub const PURPLE: Color

pub const REBECCA_PURPLE: Color

pub const RED: Color

pub const ROSY_BROWN: Color

pub const ROYAL_BLUE: Color

pub const SADDLE_BROWN: Color

pub const SALMON: Color

pub const SANDY_BROWN: Color

pub const SEA_GREEN: Color

pub const SEASHELL: Color

pub const SIENNA: Color

pub const SILVER: Color

pub const SKY_BLUE: Color

pub const SLATE_BLUE: Color

pub const SLATE_GRAY: Color

pub const SNOW: Color

pub const SPRING_GREEN: Color

pub const STEEL_BLUE: Color

pub const TAN: Color

pub const TEAL: Color

pub const THISTLE: Color

pub const TOMATO: Color

pub const TURQUOISE: Color

pub const VIOLET: Color

pub const WEB_GRAY: Color

pub const WEB_GREEN: Color

pub const WEB_MAROON: Color

pub const WEB_PURPLE: Color

pub const WHEAT: Color

pub const WHITE_SMOKE: Color

pub const YELLOW: Color

pub const YELLOW_GREEN: Color

Trait Implementations§

§

impl Add for Color

§

type Output = Color

The resulting type after applying the + operator.
§

fn add(self, rhs: Color) -> <Color as Add>::Output

Performs the + operation. Read more
§

impl AddAssign for Color

§

fn add_assign(&mut self, rhs: Color)

Performs the += operation. Read more
§

impl ApproxEq for Color

§

fn approx_eq(&self, other: &Color) -> bool

§

impl Clone for Color

§

fn clone(&self) -> Color

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Color

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Color

Constructs a default Color which is opaque black.

§

fn default() -> Color

Returns the “default value” for a type. Read more
§

impl Display for Color

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats Color to match Godot’s string representation.

§Example
use godot::prelude::*;
let color = Color::from_rgba(1.0, 1.0, 1.0, 1.0);
assert_eq!(format!("{}", color), "(1, 1, 1, 1)");
§

impl Div for Color

§

type Output = Color

The resulting type after applying the / operator.
§

fn div(self, rhs: Color) -> <Color as Div>::Output

Performs the / operation. Read more
§

impl DivAssign for Color

§

fn div_assign(&mut self, rhs: Color)

Performs the /= operation. Read more
§

impl Export for Color

§

fn export_hint() -> PropertyHintInfo

The export info to use for an exported field of this type, if no other export info is specified.
§

impl Extend<Color> for PackedColorArray

Extends aPackedColorArray with the contents of an iterator

§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = Color>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
§

impl FromGodot for Color

§

fn try_from_godot( via: <Color as GodotConvert>::Via, ) -> Result<Color, ConvertError>

Converts the Godot representation to this type, returning Err on failure.
§

fn from_godot(via: Self::Via) -> Self

⚠️ Converts the Godot representation to this type. Read more
§

fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>

Performs the conversion from a Variant, returning Err on failure.
§

fn from_variant(variant: &Variant) -> Self

⚠️ Performs the conversion from a Variant. Read more
§

impl FromIterator<Color> for PackedColorArray

Creates a PackedColorArray from an iterator.

§

fn from_iter<I>(iter: I) -> PackedColorArray
where I: IntoIterator<Item = Color>,

Creates a value from an iterator. Read more
§

impl GodotConvert for Color

§

type Via = Color

The type through which Self is represented in Godot.
§

impl Mul<f32> for Color

§

type Output = Color

The resulting type after applying the * operator.
§

fn mul(self, rhs: f32) -> <Color as Mul<f32>>::Output

Performs the * operation. Read more
§

impl Mul for Color

§

type Output = Color

The resulting type after applying the * operator.
§

fn mul(self, rhs: Color) -> <Color as Mul>::Output

Performs the * operation. Read more
§

impl MulAssign<f32> for Color

§

fn mul_assign(&mut self, f: f32)

Performs the *= operation. Read more
§

impl MulAssign for Color

§

fn mul_assign(&mut self, rhs: Color)

Performs the *= operation. Read more
§

impl Neg for Color

§

type Output = Color

The resulting type after applying the - operator.
§

fn neg(self) -> Color

Performs the unary - operation. Read more
§

impl ParamType for Color

§

fn owned_to_arg<'v>(self) -> <Color as ParamType>::Arg<'v>

Converts an owned value to the canonical argument type, which can be passed to impl AsArg<T>. Read more
§

impl PartialEq for Color

§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl PartialOrd for Color

§

fn partial_cmp(&self, other: &Color) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl Sub for Color

§

type Output = Color

The resulting type after applying the - operator.
§

fn sub(self, rhs: Color) -> <Color as Sub>::Output

Performs the - operation. Read more
§

impl SubAssign for Color

§

fn sub_assign(&mut self, rhs: Color)

Performs the -= operation. Read more
§

impl ToGodot for Color

§

type ToVia<'v> = <Color as GodotConvert>::Via

Target type of to_godot(), which can differ from Via for pass-by-reference types. Read more
§

fn to_godot(&self) -> <Color as ToGodot>::ToVia<'_>

Converts this type to the Godot type by reference, usually by cloning.
§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
§

impl Var for Color

§

fn get_property(&self) -> <Color as GodotConvert>::Via

§

fn set_property(&mut self, value: <Color as GodotConvert>::Via)

§

fn var_hint() -> PropertyHintInfo

Specific property hints, only override if they deviate from GodotType::property_info, e.g. for enums/newtypes.
§

impl ArrayElement for Color

§

impl AsArg<Color> for Color

§

impl Copy for Color

§

impl GodotType for Color

§

impl PackedArrayElement for Color

§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.