Struct godot::builtin::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.

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: Into<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<S>(string: S) -> Option<Color>
where S: Into<GString>,

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 of 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 an 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 default_export_info() -> 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>

Performs the conversion.
§

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

⚠️ Performs the conversion. Read more
§

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

Performs the conversion from a Variant.
§

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 PartialEq for Color

§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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

§

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

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

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

Converts this type to the Godot type. Read more
§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
§

impl TypeStringHint for Color

§

fn type_string() -> String

Returns the representation of this type as a type string. Read more
§

impl Var for Color

§

impl ArrayElement for Color

§

impl Copy for Color

§

impl GodotType 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> 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,

§

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§

default 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>,

§

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>,

§

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.