Skip to main content

RGBA

Struct RGBA 

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

RGBA color with four 0..1 float channels.

This is the primary color type in Optic. Most engine APIs accept or return RGBA directly. All other color types convert through it.

FieldRangeDescription
.00..1Red
.10..1Green
.20..1Blue
.30..1Alpha (0 = transparent, 1 = opaque)

§Hex parsing

use optic_color::*;

let c = RGBA::from_hex("#ff8800").unwrap();
let c = RGBA::from_hex("#f80").unwrap();     // shorthand
let c = RGBA::from_hex("#ff880044").unwrap(); // with alpha
let c = RGBA::from_hex_u32(0xff880044);

§HSV modifiers

use optic_color::*;

let red = RED;
let pink = red.lighten(0.3);
let dull = red.desaturate(0.5);
let inv = red.invert();

§sRGB conversions

to_linear applies the sRGB EOTF (decodes display encoding to linear light). to_srgb applies the OETF (encodes linear light for display).

Tuple Fields§

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

Implementations§

Source§

impl RGBA

Source

pub const fn new(r: f32, g: f32, b: f32, a: f32) -> RGBA

Construct an RGBA from individual 0..1 float channels.

This is a const fn, usable in constant contexts.

Source

pub fn grey(lum: f32) -> RGBA

Construct a greyscale RGBA with alpha 1.0.

use optic_color::*;
let grey = RGBA::grey(0.5);
Source

pub fn from_rgb(rgb: RGB, alpha: f32) -> RGBA

Construct from an RGB and an alpha value.

Source

pub fn to_rgb(&self) -> RGB

Drop alpha, returning an RGB.

Source

pub fn with_alpha(self, a: f32) -> RGBA

Replace the alpha channel, returning a new RGBA.

The RGB channels are unchanged.

Source

pub fn from_hex(hex: &str) -> Result<RGBA, &'static str>

Parse a hex color string.

Supports the following formats (with or without # prefix):

LengthFormatExample
3#RGB#f80
4#RGBA#f80c
6#RRGGBB#ff8800
8#RRGGBBAA#ff880044

Returns an error if the string contains invalid hex digits.

Source

pub fn from_hex_u32(hex: u32) -> RGBA

Construct from a packed 0xRRGGBBAA u32.

use optic_color::*;
let c = RGBA::from_hex_u32(0xff8800ff);
Source

pub fn to_hex_u32(self) -> u32

Encode as a 0xRRGGBBAA u32.

Source

pub fn from_bytes(r: u8, g: u8, b: u8, a: u8) -> RGBA

Construct from 8-bit channels (0..255).

Values are divided by 255.0 to produce the 0..1 float representation.

use optic_color::*;
let c = RGBA::from_bytes(255, 136, 0, 255);
Source

pub fn lighten(self, amount: f32) -> RGBA

Lighten by a fixed amount in HSV value space.

Positive amount increases value; negative decreases it. The result is clamped to 0..1. Alpha is preserved.

use optic_color::*;
let lighter = RED.lighten(0.2);
Source

pub fn darken(self, amount: f32) -> RGBA

Darken by a fixed amount in HSV value space.

Equivalent to lighten(-amount).

Source

pub fn saturate(self, amount: f32) -> RGBA

Increase saturation by a fixed amount in HSV space.

Positive amount increases saturation; negative decreases it. The result is clamped to 0..1. Alpha is preserved.

Source

pub fn desaturate(self, amount: f32) -> RGBA

Decrease saturation by a fixed amount in HSV space.

Equivalent to saturate(-amount).

Source

pub fn invert(self) -> RGBA

Invert the RGB channels (alpha unchanged).

Each channel becomes 1.0 - channel.

use optic_color::*;
let inv = WHITE.invert();
assert_eq!(inv.0, 0.0); // BLACK
Source

pub fn to_linear(self) -> RGBA

Convert from sRGB display encoding to linear light (EOTF).

Applies the sRGB gamma expansion curve. Use this before doing physically based lighting calculations.

Source

pub fn to_srgb(self) -> RGBA

Convert from linear light to sRGB display encoding (OETF).

Applies the sRGB gamma compression curve. Use this before writing to a framebuffer that expects sRGB.

Trait Implementations§

Source§

impl Add for RGBA

Source§

type Output = RGBA

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl ChannelArray<4> for RGBA

Source§

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

Convert to a float array of length N.
Source§

fn from_array(a: [f32; 4]) -> RGBA

Construct from a float array of length N.
Source§

impl Clone for RGBA

Source§

fn clone(&self) -> RGBA

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 RGBA

Source§

impl Debug for RGBA

Source§

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

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

impl Div<f32> for RGBA

Source§

type Output = RGBA

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<HSL> for RGBA

Source§

fn from(hsl: HSL) -> RGBA

Converts to this type from the input type.
Source§

impl From<HSV> for RGBA

Source§

fn from(hsv: HSV) -> RGBA

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<RGBA> for HSV

Source§

fn from(rgba: RGBA) -> HSV

Converts to this type from the input type.
Source§

impl From<RGBA> for HSL

Source§

fn from(rgba: RGBA) -> HSL

Converts to this type from the input type.
Source§

impl From<[f32; 4]> for RGBA

Source§

fn from(arr: [f32; 4]) -> RGBA

Converts to this type from the input type.
Source§

impl FromRgba for RGBA

Source§

impl Mul for RGBA

Source§

type Output = RGBA

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<f32> for RGBA

Source§

type Output = RGBA

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Sub for RGBA

Source§

type Output = RGBA

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl ToRgba for RGBA

Source§

fn to_rgba(self) -> RGBA

Convert to RGBA.

Auto Trait Implementations§

§

impl Freeze for RGBA

§

impl RefUnwindSafe for RGBA

§

impl Send for RGBA

§

impl Sync for RGBA

§

impl Unpin for RGBA

§

impl UnsafeUnpin for RGBA

§

impl UnwindSafe for RGBA

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<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> 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> 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.