#[cfg(not(feature = "std"))]
use alloc::string::String;
pub use grayscale::GrayScaleMethod;
use grayscale::rgb_grayscale;
pub use ratio::RgbRatio;
use crate::{ColorAlpha, ColorTuple, ColorTupleA, converters, Hsl, ColorUnitsIter};
use crate::common::{tuple_to_string};
use crate::err::ParseError;
use crate::units::{Alpha, GetColorUnits, Unit, Units};
#[cfg(test)]
mod tests;
mod from;
mod from_str;
mod grayscale;
mod ops;
mod ratio;
mod transform;
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rgb {
pub(crate) units: Units,
}
iter_def!(Rgb);
pub(crate) fn new_rgb_units(r: f64, g: f64, b: f64) -> Units {
let ul = [Unit::new_rgb(r), Unit::new_rgb(g), Unit::new_rgb(b), Unit::default()];
Units { len: 3, list: ul, alpha: Alpha::default() }
}
impl Rgb {
fn _apply_tuple(&mut self, t: &ColorTuple) {
self.units.list[0].value = t.0;
self.units.list[1].value = t.1;
self.units.list[2].value = t.2;
}
pub(crate) fn from_units(u: Units) -> Self { Rgb { units: u } }
pub fn new(r: f64, g: f64, b: f64, a: Option<f64>) -> Rgb {
let mut units = new_rgb_units(r, g, b);
units.alpha.set_opt(a);
units.restrict();
Rgb { units }
}
pub fn from_hex_str(s: &str) -> Result<Rgb, ParseError> {
let (tuple, alpha) = converters::hex_to_rgb(s)?;
let mut rgb = Rgb::from(&tuple);
if let Some(a) = alpha {
rgb.set_alpha(a);
}
Ok(rgb)
}
pub fn to_hex_string(&self) -> String {
converters::rgb_to_hex(&self.into())
}
pub fn to_hexa_string(&self) -> String {
converters::rgb_to_hexa(&self.into())
}
pub fn red(&self) -> f64 { self.units[0] }
pub fn green(&self) -> f64 {
self.units[1]
}
pub fn blue(&self) -> f64 {
self.units[2]
}
#[deprecated(since = "0.7.0", note = "Please use `red` instead")]
pub fn get_red(&self) -> f64 { self.red() }
#[deprecated(since = "0.7.0", note = "Please use `green` instead")]
pub fn get_green(&self) -> f64 {
self.green()
}
#[deprecated(since = "0.7.0", note = "Please use `blue` instead")]
pub fn get_blue(&self) -> f64 {
self.blue()
}
pub fn set_red(&mut self, val: f64) { self.units.list[0].set(val); }
pub fn set_green(&mut self, val: f64) { self.units.list[1].set(val); }
pub fn set_blue(&mut self, val: f64) { self.units.list[2].set(val); }
pub fn to_css_string(&self) -> String {
let t: ColorTupleA = self.into();
tuple_to_string(&t, "rgb")
}
pub fn grayscale(&mut self, method: GrayScaleMethod) {
rgb_grayscale(self, method);
}
pub fn iter(&self) -> ColorUnitsIter {
ColorUnitsIter::from_units(&self.units)
}
pub fn as_ratio(&self) -> RgbRatio {
RgbRatio::from_units(self.units.as_ratio())
}
}
impl Default for Rgb {
fn default() -> Rgb {
Rgb::from_units(new_rgb_units(0.0, 0.0, 0.0))
}
}
impl AsRef<Rgb> for Rgb {
fn as_ref(&self) -> &Rgb {
self
}
}
impl GetColorUnits for Rgb {
fn get_units(&self) -> &Units {
&self.units
}
fn get_units_mut(&mut self) -> &mut Units {
&mut self.units
}
}
impl core::str::FromStr for Rgb {
type Err = ParseError;
fn from_str(s: &str) -> Result<Rgb, ParseError> {
let (tuple, alpha) = from_str::rgb(s)?;
let mut rgb = Rgb::from(&tuple);
if let Some(a) = alpha {
rgb.set_alpha(a);
}
Ok(rgb)
}
}