pub struct D3Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}Expand description
RGB color with alpha channel and interpolation support
§Example
use d3rs::color::D3Color;
let red = D3Color::rgb(255, 0, 0);
let blue = D3Color::from_hex(0x0000ff);
let purple = red.interpolate(&blue, 0.5);Fields§
§r: f32Red component (0.0 - 1.0)
g: f32Green component (0.0 - 1.0)
b: f32Blue component (0.0 - 1.0)
a: f32Alpha component (0.0 - 1.0)
Implementations§
Source§impl D3Color
impl D3Color
Sourcepub fn rgb(r: u8, g: u8, b: u8) -> D3Color
pub fn rgb(r: u8, g: u8, b: u8) -> D3Color
Create a color from RGB values (0-255)
§Example
use d3rs::color::D3Color;
let red = D3Color::rgb(255, 0, 0);Sourcepub fn rgba(r: u8, g: u8, b: u8, a: u8) -> D3Color
pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> D3Color
Create a color from RGBA values (0-255)
§Example
use d3rs::color::D3Color;
let semi_transparent_red = D3Color::rgba(255, 0, 0, 128);Sourcepub fn from_hex(hex: u32) -> D3Color
pub fn from_hex(hex: u32) -> D3Color
Create a color from a hex value (0xRRGGBB)
§Example
use d3rs::color::D3Color;
let orange = D3Color::from_hex(0xff5500);Sourcepub fn from_rgb_f32(r: f32, g: f32, b: f32) -> D3Color
pub fn from_rgb_f32(r: f32, g: f32, b: f32) -> D3Color
Create a color from RGB floats (0.0 - 1.0)
§Example
use d3rs::color::D3Color;
let red = D3Color::from_rgb_f32(1.0, 0.0, 0.0);Sourcepub fn from_rgba_f32(r: f32, g: f32, b: f32, a: f32) -> D3Color
pub fn from_rgba_f32(r: f32, g: f32, b: f32, a: f32) -> D3Color
Create a color from RGBA floats (0.0 - 1.0)
Sourcepub fn with_alpha(self, alpha: f32) -> D3Color
pub fn with_alpha(self, alpha: f32) -> D3Color
Set the alpha channel
§Example
use d3rs::color::D3Color;
let semi_transparent = D3Color::rgb(255, 0, 0).with_alpha(0.5);Sourcepub fn interpolate(&self, other: &D3Color, t: f32) -> D3Color
pub fn interpolate(&self, other: &D3Color, t: f32) -> D3Color
Linear interpolation between two colors
t should be in the range [0.0, 1.0], where:
- 0.0 returns
self - 1.0 returns
other - 0.5 returns the midpoint
§Example
use d3rs::color::D3Color;
let red = D3Color::rgb(255, 0, 0);
let blue = D3Color::rgb(0, 0, 255);
let purple = red.interpolate(&blue, 0.5);Sourcepub fn to_hex(&self) -> String
pub fn to_hex(&self) -> String
Convert to a hex color string (e.g., “#ff0000”)
§Example
use d3rs::color::D3Color;
let red = D3Color::rgb(255, 0, 0);
assert_eq!(red.to_hex(), "#ff0000");Sourcepub fn to_hex_alpha(&self) -> String
pub fn to_hex_alpha(&self) -> String
Convert to a hex color string with alpha (e.g., “#ff000080”)
§Example
use d3rs::color::D3Color;
let red = D3Color::rgba(255, 0, 0, 128);
assert_eq!(red.to_hex_alpha(), "#ff000080");Sourcepub fn luminance(&self) -> f32
pub fn luminance(&self) -> f32
Get the luminance of the color (0.0 to 1.0)
Uses the relative luminance formula from WCAG 2.0.
§Example
use d3rs::color::D3Color;
let white = D3Color::rgb(255, 255, 255);
let black = D3Color::rgb(0, 0, 0);
assert!(white.luminance() > 0.9);
assert!(black.luminance() < 0.1);Sourcepub fn brighter(&self, k: f32) -> D3Color
pub fn brighter(&self, k: f32) -> D3Color
Make the color brighter by a factor
Factor of 1.0 increases brightness by ~18% (similar to d3.brighter).
Sourcepub fn darker(&self, k: f32) -> D3Color
pub fn darker(&self, k: f32) -> D3Color
Make the color darker by a factor
Factor of 1.0 decreases brightness by ~18% (similar to d3.darker).
Sourcepub fn with_opacity(&self, opacity: f32) -> D3Color
pub fn with_opacity(&self, opacity: f32) -> D3Color
Set the opacity (alpha channel)
Trait Implementations§
impl Copy for D3Color
impl StructuralPartialEq for D3Color
Auto Trait Implementations§
impl Freeze for D3Color
impl RefUnwindSafe for D3Color
impl Send for D3Color
impl Sync for D3Color
impl Unpin for D3Color
impl UnwindSafe for D3Color
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§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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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