Enum bevy_color::prelude::Color
source · pub enum Color {
Srgba(Srgba),
LinearRgba(LinearRgba),
Hsla(Hsla),
Hsva(Hsva),
Hwba(Hwba),
Laba(Laba),
Lcha(Lcha),
Oklaba(Oklaba),
Oklcha(Oklcha),
Xyza(Xyza),
}
Expand description
An enumerated type that can represent any of the color types in this crate.
This is useful when you need to store a color in a data structure that can’t be generic over the color type.
§Conversion
Conversion between the various color spaces is achieved using Rust’s native From trait. Because certain color spaces are defined by their transformation to and from another space, these From implementations reflect that set of definitions.
let color = Srgba::rgb(0.5, 0.5, 0.5);
// Using From explicitly
let linear_color = LinearRgba::from(color);
// Using Into
let linear_color: LinearRgba = color.into();
For example, the sRGB space is defined by its relationship with Linear RGB, and HWB by its with sRGB. As such, it is the responsibility of sRGB to provide From implementations for Linear RGB, and HWB for sRGB. To then provide conversion between Linear RGB and HWB directly, HWB is responsible for implementing these conversions, delegating to sRGB as an intermediatory. This ensures that all conversions take the shortest path between any two spaces, and limit the proliferation of domain specific knowledge for each color space to their respective definitions.
§Operations
Color
supports all the standard color operations, such as mixing,
luminance and hue adjustment, clamping,
and diffing. These operations delegate to the concrete color space contained
by Color
, but will convert to Oklch
for operations which aren’t supported in the
current space. After performing the operation, if a conversion was required, the result will be
converted back into the original color space.
let red_hsv = Color::hsv(0., 1., 1.);
let red_srgb = Color::srgb(1., 0., 0.);
// HSV has a definition of hue, so it will be returned.
red_hsv.hue();
// SRGB doesn't have a native definition for hue.
// Converts to Oklch and returns that result.
red_srgb.hue();
Oklch
has been chosen as the intermediary space in cases where conversion is required
due to its perceptual uniformity and broad support for Bevy’s color operations.
To avoid the cost of repeated conversion, and ensure consistent results where that is desired,
first convert this Color
into your desired color space.
Variants§
Srgba(Srgba)
A color in the sRGB color space with alpha.
LinearRgba(LinearRgba)
A color in the linear sRGB color space with alpha.
Hsla(Hsla)
A color in the HSL color space with alpha.
Hsva(Hsva)
A color in the HSV color space with alpha.
Hwba(Hwba)
A color in the HWB color space with alpha.
Laba(Laba)
A color in the LAB color space with alpha.
Lcha(Lcha)
A color in the LCH color space with alpha.
Oklaba(Oklaba)
A color in the Oklab color space with alpha.
Oklcha(Oklcha)
A color in the Oklch color space with alpha.
Xyza(Xyza)
A color in the XYZ color space with alpha.
Implementations§
source§impl Color
impl Color
sourcepub fn to_linear(&self) -> LinearRgba
pub fn to_linear(&self) -> LinearRgba
Return the color as a linear RGBA color.
sourcepub const fn rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self
👎Deprecated: Use Color::srgba
instead
pub const fn rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self
Color::srgba
insteadsourcepub fn rgb_from_array([r, g, b]: [f32; 3]) -> Self
👎Deprecated: Use Color::srgb_from_array
instead
pub fn rgb_from_array([r, g, b]: [f32; 3]) -> Self
Color::srgb_from_array
insteadsourcepub fn srgb_from_array(array: [f32; 3]) -> Self
pub fn srgb_from_array(array: [f32; 3]) -> Self
sourcepub fn rgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Self
👎Deprecated: Use Color::srgba_u8
instead
pub fn rgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Self
Color::srgba_u8
insteadsourcepub const fn rbga_linear(red: f32, green: f32, blue: f32, alpha: f32) -> Self
👎Deprecated: Use Color::linear_rgba instead.
pub const fn rbga_linear(red: f32, green: f32, blue: f32, alpha: f32) -> Self
Creates a new Color
object storing a LinearRgba
color.
sourcepub const fn linear_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self
pub const fn linear_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self
Creates a new Color
object storing a LinearRgba
color.
sourcepub const fn rgb_linear(red: f32, green: f32, blue: f32) -> Self
👎Deprecated: Use Color::linear_rgb instead.
pub const fn rgb_linear(red: f32, green: f32, blue: f32) -> Self
Creates a new Color
object storing a LinearRgba
color with an alpha of 1.0.
sourcepub const fn linear_rgb(red: f32, green: f32, blue: f32) -> Self
pub const fn linear_rgb(red: f32, green: f32, blue: f32) -> Self
Creates a new Color
object storing a LinearRgba
color with an alpha of 1.0.
sourcepub const WHITE: Self = _
pub const WHITE: Self = _
A fully white Color::LinearRgba
color with an alpha of 1.0.
sourcepub const BLACK: Self = _
pub const BLACK: Self = _
A fully black Color::LinearRgba
color with an alpha of 1.0.
sourcepub const NONE: Self = _
pub const NONE: Self = _
A fully transparent Color::LinearRgba
color with 0 red, green and blue.
Trait Implementations§
source§impl Alpha for Color
impl Alpha for Color
source§fn with_alpha(&self, alpha: f32) -> Self
fn with_alpha(&self, alpha: f32) -> Self
source§fn is_fully_transparent(&self) -> bool
fn is_fully_transparent(&self) -> bool
source§fn is_fully_opaque(&self) -> bool
fn is_fully_opaque(&self) -> bool
source§impl Default for Color
impl Default for Color
source§fn default() -> Self
fn default() -> Self
A fully white Color::LinearRgba
color with an alpha of 1.0.
source§impl<'de> Deserialize<'de> for Color
impl<'de> Deserialize<'de> for Color
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Enum for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
impl Enum for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
source§fn field(&self, __name_param: &str) -> Option<&dyn Reflect>
fn field(&self, __name_param: &str) -> Option<&dyn Reflect>
source§fn field_at(&self, __index_param: usize) -> Option<&dyn Reflect>
fn field_at(&self, __index_param: usize) -> Option<&dyn Reflect>
source§fn field_mut(&mut self, __name_param: &str) -> Option<&mut dyn Reflect>
fn field_mut(&mut self, __name_param: &str) -> Option<&mut dyn Reflect>
source§fn field_at_mut(&mut self, __index_param: usize) -> Option<&mut dyn Reflect>
fn field_at_mut(&mut self, __index_param: usize) -> Option<&mut dyn Reflect>
source§fn index_of(&self, __name_param: &str) -> Option<usize>
fn index_of(&self, __name_param: &str) -> Option<usize>
source§fn name_at(&self, __index_param: usize) -> Option<&str>
fn name_at(&self, __index_param: usize) -> Option<&str>
source§fn iter_fields(&self) -> VariantFieldIter<'_>
fn iter_fields(&self) -> VariantFieldIter<'_>
source§fn variant_name(&self) -> &str
fn variant_name(&self) -> &str
source§fn variant_index(&self) -> usize
fn variant_index(&self) -> usize
source§fn variant_type(&self) -> VariantType
fn variant_type(&self) -> VariantType
fn clone_dynamic(&self) -> DynamicEnum
source§fn is_variant(&self, variant_type: VariantType) -> bool
fn is_variant(&self, variant_type: VariantType) -> bool
source§fn variant_path(&self) -> String
fn variant_path(&self) -> String
source§impl EuclideanDistance for Color
impl EuclideanDistance for Color
source§impl From<Color> for LinearRgba
impl From<Color> for LinearRgba
source§impl From<LinearRgba> for Color
impl From<LinearRgba> for Color
source§fn from(value: LinearRgba) -> Self
fn from(value: LinearRgba) -> Self
source§impl FromReflect for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
impl FromReflect for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
source§fn from_reflect(__param0: &dyn Reflect) -> Option<Self>
fn from_reflect(__param0: &dyn Reflect) -> Option<Self>
Self
from a reflected value.source§fn take_from_reflect(
reflect: Box<dyn Reflect>
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect> ) -> Result<Self, Box<dyn Reflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read moresource§impl GetTypeRegistration for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
impl GetTypeRegistration for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration
for this type.source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
source§impl Hue for Color
impl Hue for Color
source§impl Luminance for Color
impl Luminance for Color
source§fn with_luminance(&self, value: f32) -> Self
fn with_luminance(&self, value: f32) -> Self
source§fn darker(&self, amount: f32) -> Self
fn darker(&self, amount: f32) -> Self
amount
should be between 0.0 and 1.0.
The amount represents an absolute decrease in luminance, and is distributive:
color.darker(a).darker(b) == color.darker(a + b)
. Colors are clamped to black
if the amount would cause them to go below black. Read moresource§fn lighter(&self, amount: f32) -> Self
fn lighter(&self, amount: f32) -> Self
amount
should be between 0.0 and 1.0.
The amount represents an absolute increase in luminance, and is distributive:
color.lighter(a).lighter(b) == color.lighter(a + b)
. Colors are clamped to white
if the amount would cause them to go above white. Read moresource§impl Mix for Color
impl Mix for Color
source§fn mix(&self, other: &Self, factor: f32) -> Self
fn mix(&self, other: &Self, factor: f32) -> Self
source§fn mix_assign(&mut self, other: Self, factor: f32)
fn mix_assign(&mut self, other: Self, factor: f32)
source§impl PartialEq for Color
impl PartialEq for Color
source§impl Reflect for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
impl Reflect for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any
.source§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read moresource§fn set(
&mut self,
__value_param: Box<dyn Reflect>
) -> Result<(), Box<dyn Reflect>>
fn set( &mut self, __value_param: Box<dyn Reflect> ) -> Result<(), Box<dyn Reflect>>
source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
source§fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
source§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
source§impl TypePath for Color
impl TypePath for Color
source§fn type_path() -> &'static str
fn type_path() -> &'static str
source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
source§impl Typed for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
impl Typed for Colorwhere
Self: Any + Send + Sync,
Srgba: FromReflect + TypePath + RegisterForReflection,
LinearRgba: FromReflect + TypePath + RegisterForReflection,
Hsla: FromReflect + TypePath + RegisterForReflection,
Hsva: FromReflect + TypePath + RegisterForReflection,
Hwba: FromReflect + TypePath + RegisterForReflection,
Laba: FromReflect + TypePath + RegisterForReflection,
Lcha: FromReflect + TypePath + RegisterForReflection,
Oklaba: FromReflect + TypePath + RegisterForReflection,
Oklcha: FromReflect + TypePath + RegisterForReflection,
Xyza: FromReflect + TypePath + RegisterForReflection,
impl Copy 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
source§impl<T> GetPath for T
impl<T> GetPath for T
source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>
) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p> ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p> ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read moresource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more