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
Godot’s predefined colors.
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
Extends aPackedColorArray
with the contents of an iterator
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
Creates a PackedColorArray
from an iterator.
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.