pub struct Rgb(/* private fields */);Expand description
A floating-point RGB color value, unbounded.
- Represents a light intensity in unspecified units.
- Each color component must have a nonnegative, non-NaN
f32value. - Color components are linear (gamma = 1), but use the same RGB primaries as sRGB (Rec. 709).
For colors whose components do not exceed 1, use Rgb01 instead.
§Suitability
The primary purpose of this color type and its relatives, and the reason for its enforced value restrictions, is to be able to be stored in game state data structures, with reliable comparison and serialization. It is not necessarily appropriate for storing the intermediate results of computations (though the operations do take care to avoid unnecessary re-validation).
Future versions of All is Cubes may choose to reduce the precision of this type to f16
when Rust has built-in support for that data type.
Implementations§
Source§impl Rgb
impl Rgb
Sourcepub const ONE: Rgb
pub const ONE: Rgb
Nominal white; the constant equal to Rgb::new(1., 1., 1.).unwrap().
Note that brighter values may exist; the color system “supports HDR”.
Sourcepub const fn new(r: f32, g: f32, b: f32) -> Rgb
pub const fn new(r: f32, g: f32, b: f32) -> Rgb
Constructs a color from components.
Panics if any component is NaN. Clamps any component that is negative.
Sourcepub const fn new_ps(
r: PositiveSign<f32>,
g: PositiveSign<f32>,
b: PositiveSign<f32>,
) -> Rgb
pub const fn new_ps( r: PositiveSign<f32>, g: PositiveSign<f32>, b: PositiveSign<f32>, ) -> Rgb
Constructs a color from components that have already been checked for not being NaN or negative.
Note: This exists primarily to assist the rgb_const! macro and may be renamed
or replaced in future versions.
Sourcepub const fn from_luminance(luminance: f32) -> Rgb
pub const fn from_luminance(luminance: f32) -> Rgb
Constructs a shade of gray (components all equal). Panics if any component is NaN. No other range checks are performed.
Sourcepub const fn with_alpha(self, alpha: ZeroOne<f32>) -> Rgba
pub const fn with_alpha(self, alpha: ZeroOne<f32>) -> Rgba
Adds an alpha component to produce an Rgba color.
Sourcepub const fn with_alpha_one(self) -> Rgba
pub const fn with_alpha_one(self) -> Rgba
Adds an alpha component of 1.0 (fully opaque) to produce an Rgba color.
Sourcepub const fn red(self) -> PositiveSign<f32>
pub const fn red(self) -> PositiveSign<f32>
Returns the red color component. Values are linear (gamma = 1).
Sourcepub const fn green(self) -> PositiveSign<f32>
pub const fn green(self) -> PositiveSign<f32>
Returns the green color component. Values are linear (gamma = 1).
Sourcepub const fn blue(self) -> PositiveSign<f32>
pub const fn blue(self) -> PositiveSign<f32>
Returns the blue color component. Values are linear (gamma = 1).
Sourcepub fn luminance(self) -> f32
pub fn luminance(self) -> f32
Combines the red, green, and blue components to obtain a relative luminance (“grayscale”) value. This will be equal to 1 if all components are 1.
use all_is_cubes::math::Rgb;
assert_eq!(0.0, Rgb::ZERO.luminance());
assert_eq!(0.5, (Rgb::ONE * 0.5).luminance());
assert_eq!(1.0, Rgb::ONE.luminance());
assert_eq!(2.0, (Rgb::ONE * 2.0).luminance());
assert_eq!(0.2126, Rgb::new(1., 0., 0.).luminance());
assert_eq!(0.7152, Rgb::new(0., 1., 0.).luminance());
assert_eq!(0.0722, Rgb::new(0., 0., 1.).luminance());Sourcepub const fn from_srgb8(rgb: [u8; 3]) -> Rgb
pub const fn from_srgb8(rgb: [u8; 3]) -> Rgb
Converts sRGB 8-bits-per-component color to the corresponding linear Rgba value.
Sourcepub fn clamp(self, maximum: PositiveSign<f32>) -> Rgb
pub fn clamp(self, maximum: PositiveSign<f32>) -> Rgb
Clamp each component to lie within the range 0 to maximum, inclusive.
Sourcepub fn clamp_01(self) -> Rgb01
pub fn clamp_01(self) -> Rgb01
Clamp each component to lie within the range 0 to maximum, inclusive.
Sourcepub fn saturating_sub(self, other: Rgb) -> Rgb
pub fn saturating_sub(self, other: Rgb) -> Rgb
Subtract other from self; if any component would be negative, it is zero instead.
Trait Implementations§
Source§impl AddAssign for Rgb
impl AddAssign for Rgb
Source§fn add_assign(&mut self, other: Rgb)
fn add_assign(&mut self, other: Rgb)
+= operation. Read moreSource§impl<'a> Arbitrary<'a> for Rgb
impl<'a> Arbitrary<'a> for Rgb
Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Rgb, Error>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Rgb, Error>
Self from the given unstructured data. Read moreSource§fn size_hint(_depth: usize) -> (usize, Option<usize>)
fn size_hint(_depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl From<Rgb> for PackedLight
impl From<Rgb> for PackedLight
Source§impl Mul<f32> for Rgb
Multiplies this color value by a scalar.
impl Mul<f32> for Rgb
Multiplies this color value by a scalar.
Panics if the scalar is NaN. Returns zero if the scalar is negative.
Source§impl Sum for Rgb
There is no corresponding impl Sum for Rgba because the alpha would
not have a universally reasonable interpretation.
impl Sum for Rgb
There is no corresponding impl Sum for Rgba because the alpha would
not have a universally reasonable interpretation.
impl Copy for Rgb
impl Eq for Rgb
impl StructuralPartialEq for Rgb
Auto Trait Implementations§
impl Freeze for Rgb
impl RefUnwindSafe for Rgb
impl Send for Rgb
impl Sync for Rgb
impl Unpin for Rgb
impl UnwindSafe for Rgb
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> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more