/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use peek_poke::PeekPoke; use std::cmp; use std::hash::{Hash, Hasher};
/// Represents pre-multiplied RGBA colors with floating point numbers. /// /// All components must be between 0.0 and 1.0. /// An alpha value of 1.0 is opaque while 0.0 is fully transparent. /// /// In premultiplied colors transitions to transparent always look "nice" /// therefore they are used in CSS gradients. #[repr(C)] #[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)] pubstruct PremultipliedColorF { pub r: f32, pub g: f32, pub b: f32, pub a: f32,
}
/// Represents RGBA screen colors with floating point numbers. /// /// All components must be between 0.0 and 1.0. /// An alpha value of 1.0 is opaque while 0.0 is fully transparent. #[repr(C)] #[derive(Clone, Copy, Debug, Default, Deserialize, MallocSizeOf, PartialEq, Serialize, PeekPoke)] pubstruct ColorF { pub r: f32, pub g: f32, pub b: f32, pub a: f32,
}
/// Multiply the RGB components with the alpha channel. pubfn premultiplied(&self) -> PremultipliedColorF { let c = self.scale_rgb(self.a);
PremultipliedColorF { r: c.r, g: c.g, b: c.b, a: c.a }
}
}
// Floats don't impl Hash/Eq/Ord... impl Eq for PremultipliedColorF {} impl Ord for PremultipliedColorF { fn cmp(&self, other: &Self) -> cmp::Ordering { self.partial_cmp(other).unwrap_or(cmp::Ordering::Equal)
}
}
#[allow(clippy::derived_hash_with_manual_eq)] impl Hash for PremultipliedColorF { fn hash<H: Hasher>(&self, state: &mut H) { // Note: this is inconsistent with the Eq impl for -0.0 (don't care). self.r.to_bits().hash(state); self.g.to_bits().hash(state); self.b.to_bits().hash(state); self.a.to_bits().hash(state);
}
}
/// Represents RGBA screen colors with one byte per channel. /// /// If the alpha value `a` is 255 the color is opaque. #[repr(C)] #[derive(Clone, Copy, Hash, Eq, Debug, Deserialize, MallocSizeOf, PartialEq)] #[derive(PartialOrd, Ord, Serialize, PeekPoke, Default)] pubstruct ColorU { pub r: u8, pub g: u8, pub b: u8, pub a: u8,
}
impl ColorU { /// Constructs a new additive `ColorU` from its components. pubfn new(r: u8, g: u8, b: u8, a: u8) -> Self {
ColorU { r, g, b, a }
}
}
fn round_to_int(x: f32) -> u8 {
debug_assert!((0.0 <= x) && (x <= 1.0), "{} should be between 0 and 1", x); let f = (255.0 * x) + 0.5; let val = f.floor();
debug_assert!(val <= 255.0);
val as u8
}
// TODO: We shouldn't really convert back to `ColorU` ever, // since it's lossy. One of the blockers is that all of our debug colors // are specified in `ColorF`. Changing it to `ColorU` would be nice. impl From<ColorF> for ColorU { fn from(color: ColorF) -> Self {
ColorU {
r: round_to_int(color.r),
g: round_to_int(color.g),
b: round_to_int(color.b),
a: round_to_int(color.a),
}
}
}
impl From<ColorU> for ColorF { fn from(color: ColorU) -> Self {
ColorF {
r: color.r as f32 / 255.0,
g: color.g as f32 / 255.0,
b: color.b as f32 / 255.0,
a: color.a as f32 / 255.0,
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.