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.
| Field | Range | Description |
|---|---|---|
.0 | 0..1 | Red |
.1 | 0..1 | Green |
.2 | 0..1 | Blue |
.3 | 0..1 | Alpha (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: f32Implementations§
Source§impl RGBA
impl RGBA
Sourcepub const fn new(r: f32, g: f32, b: f32, a: f32) -> RGBA
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.
Sourcepub fn grey(lum: f32) -> RGBA
pub fn grey(lum: f32) -> RGBA
Construct a greyscale RGBA with alpha 1.0.
use optic_color::*;
let grey = RGBA::grey(0.5);Sourcepub fn with_alpha(self, a: f32) -> RGBA
pub fn with_alpha(self, a: f32) -> RGBA
Replace the alpha channel, returning a new RGBA.
The RGB channels are unchanged.
Sourcepub fn from_hex(hex: &str) -> Result<RGBA, &'static str>
pub fn from_hex(hex: &str) -> Result<RGBA, &'static str>
Parse a hex color string.
Supports the following formats (with or without # prefix):
| Length | Format | Example |
|---|---|---|
| 3 | #RGB | #f80 |
| 4 | #RGBA | #f80c |
| 6 | #RRGGBB | #ff8800 |
| 8 | #RRGGBBAA | #ff880044 |
Returns an error if the string contains invalid hex digits.
Sourcepub fn from_hex_u32(hex: u32) -> RGBA
pub fn from_hex_u32(hex: u32) -> RGBA
Construct from a packed 0xRRGGBBAA u32.
use optic_color::*;
let c = RGBA::from_hex_u32(0xff8800ff);Sourcepub fn to_hex_u32(self) -> u32
pub fn to_hex_u32(self) -> u32
Encode as a 0xRRGGBBAA u32.
Sourcepub fn from_bytes(r: u8, g: u8, b: u8, a: u8) -> RGBA
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);Sourcepub fn lighten(self, amount: f32) -> RGBA
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);Sourcepub fn darken(self, amount: f32) -> RGBA
pub fn darken(self, amount: f32) -> RGBA
Darken by a fixed amount in HSV value space.
Equivalent to lighten(-amount).
Sourcepub fn saturate(self, amount: f32) -> RGBA
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.
Sourcepub fn desaturate(self, amount: f32) -> RGBA
pub fn desaturate(self, amount: f32) -> RGBA
Decrease saturation by a fixed amount in HSV space.
Equivalent to saturate(-amount).
Sourcepub fn invert(self) -> RGBA
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