Skip to main content

RGB

Struct RGB 

Source
pub struct RGB(pub f32, pub f32, pub f32);
Expand description

RGB color with three 0..1 float channels.

This is a tuple struct with public fields:

FieldRangeDescription
.00..1Red channel
.10..1Green channel
.20..1Blue channel

§Arithmetic

RGB implements [Add], [Sub], [Mul] (componentwise), [Mul<f32>], and [Div<f32>] via the [ChannelArray<3>] impl. These operate on all three channels independently.

§Conversions

use optic_color::*;

let rgb = RGB(0.5, 0.2, 0.8);
let rgba: RGBA = rgb.into();        // alpha = 1.0
let rgba = rgb.to_rgba(0.5);        // custom alpha
let arr: [f32; 3] = rgb.into();     // flatten

See also RGBA, [HSV], [HSL].

Tuple Fields§

§0: f32§1: f32§2: f32

Implementations§

Source§

impl RGB

Source

pub fn grey(lum: f32) -> RGB

Construct a greyscale RGB.

All three channels are set to lum.

use optic_color::*;

let grey = RGB::grey(0.5);
assert_eq!(grey.0, 0.5);
assert_eq!(grey.1, 0.5);
assert_eq!(grey.2, 0.5);
Source

pub fn from_rgba(rgba: RGBA) -> RGB

Construct an RGB from an RGBA, dropping alpha.

Source

pub fn to_rgba(&self, alpha: f32) -> RGBA

Convert to RGBA with a given alpha.

use optic_color::*;

let rgb = RGB(1.0, 0.0, 0.0);
let rgba = rgb.to_rgba(0.5);
assert_eq!(rgba.3, 0.5);

Trait Implementations§

Source§

impl Add for RGB

Source§

type Output = RGB

The resulting type after applying the + operator.
Source§

fn add(self, rhs: RGB) -> RGB

Performs the + operation. Read more
Source§

impl ChannelArray<3> for RGB

Source§

fn to_array(self) -> [f32; 3]

Convert to a float array of length N.
Source§

fn from_array(a: [f32; 3]) -> RGB

Construct from a float array of length N.
Source§

impl Clone for RGB

Source§

fn clone(&self) -> RGB

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for RGB

Source§

impl Debug for RGB

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Div<f32> for RGB

Source§

type Output = RGB

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> RGB

Performs the / operation. Read more
Source§

impl From<(f32, f32, f32)> for RGB

Source§

fn from(t: (f32, f32, f32)) -> RGB

Converts to this type from the input type.
Source§

impl From<RGB> for RGBA

Source§

fn from(rgb: RGB) -> RGBA

Converts to this type from the input type.
Source§

impl From<RGBA> for RGB

Source§

fn from(rgba: RGBA) -> RGB

Converts to this type from the input type.
Source§

impl From<[f32; 3]> for RGB

Source§

fn from(arr: [f32; 3]) -> RGB

Converts to this type from the input type.
Source§

impl FromRgba for RGB

Source§

impl Mul for RGB

Source§

type Output = RGB

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: RGB) -> RGB

Performs the * operation. Read more
Source§

impl Mul<f32> for RGB

Source§

type Output = RGB

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> RGB

Performs the * operation. Read more
Source§

impl Sub for RGB

Source§

type Output = RGB

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: RGB) -> RGB

Performs the - operation. Read more
Source§

impl ToRgba for RGB

Source§

fn to_rgba(self) -> RGBA

Convert to RGBA.

Auto Trait Implementations§

§

impl Freeze for RGB

§

impl RefUnwindSafe for RGB

§

impl Send for RGB

§

impl Sync for RGB

§

impl Unpin for RGB

§

impl UnsafeUnpin for RGB

§

impl UnwindSafe for RGB

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ColorInfo for T
where T: ToRgba,

Source§

fn luminance(self) -> f32

Relative luminance per ITU-R BT.709. Read more
Source§

fn is_light(self) -> bool

Returns true if the luminance is greater than 0.5.
Source§

fn contrast_ratio(self, other: impl ToRgba) -> f32

Compute the WCAG contrast ratio against another color. Read more
Source§

fn to_hex(self) -> String

Encode as a hex string: #RRGGBBAA.
Source§

fn to_bytes(self) -> (u8, u8, u8, u8)

Convert to 8-bit byte channels: (r, g, b, a) in 0..255.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>