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 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.
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.
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 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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