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
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
impl Color
pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> 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
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
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
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
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
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>
pub fn from_html<S>(html: S) -> Option<Color>
Constructs a Color
from an HTML color code string. Valid values for the string are:
#RRGGBBAA
andRRGGBBAA
where each ofRR
,GG
,BB
andAA
stands for two hex digits (case insensitive).#RRGGBB
andRRGGBB
. Equivalent to#RRGGBBff
.#RGBA
andRGBA
where each ofR
,G
,B
andA
stands for a single hex digit. Equivalent to#RRGGBBAA
, i.e. each digit is repeated twice.#RGB
andRGB
. Equivalent to#RRGGBBff
.
Returns None
if the format is invalid.
pub fn from_string(string: impl AsArg<GString>) -> Option<Color>
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
orlawn-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
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
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
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
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
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
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
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
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)
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)
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)
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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>
pub fn try_to_hsv(self) -> Result<ColorHsv, String>
Fallible Color
conversion into ColorHsv
. See also Color::to_hsv
.
pub fn normalized(self) -> Color
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
impl Color
Godot’s predefined colors.
This visual cheat sheet shows how the colors look.
pub const TRANSPARENT_BLACK: Color = _
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 = _
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 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 Export for Color
impl Export for Color
§fn export_hint() -> PropertyHintInfo
fn export_hint() -> PropertyHintInfo
§impl Extend<Color> for PackedColorArray
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>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Color>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl FromGodot for Color
impl FromGodot for Color
§fn try_from_godot(
via: <Color as GodotConvert>::Via,
) -> Result<Color, ConvertError>
fn try_from_godot( via: <Color as GodotConvert>::Via, ) -> Result<Color, ConvertError>
Err
on failure.§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
, returning Err
on failure.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl FromIterator<Color> for PackedColorArray
impl FromIterator<Color> for PackedColorArray
Creates a PackedColorArray
from an iterator.
§fn from_iter<I>(iter: I) -> PackedColorArraywhere
I: IntoIterator<Item = Color>,
fn from_iter<I>(iter: I) -> PackedColorArraywhere
I: IntoIterator<Item = Color>,
§impl ParamType for Color
impl ParamType for Color
§fn owned_to_arg<'v>(self) -> <Color as ParamType>::Arg<'v>
fn owned_to_arg<'v>(self) -> <Color as ParamType>::Arg<'v>
impl AsArg<T>
. Read more§impl PartialOrd for Color
impl PartialOrd for Color
§impl ToGodot for Color
impl ToGodot for Color
§impl Var for Color
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
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.