style: Expose colors via cbindgen.

Also s/Foreground/CurrentColor.

Differential Revision: https://phabricator.services.mozilla.com/D25975
This commit is contained in:
Emilio Cobos Álvarez 2019-04-04 11:34:56 +00:00
parent a6f32bc762
commit 691eb36ffe
6 changed files with 50 additions and 37 deletions

View file

@ -7,44 +7,53 @@
/// Ratios representing the contribution of color and currentcolor to
/// the final color value.
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
#[repr(C)]
pub struct ComplexColorRatios {
/// Numeric color contribution.
pub bg: f32,
/// Foreground color, aka currentcolor, contribution.
/// currentcolor contribution.
pub fg: f32,
}
impl ComplexColorRatios {
/// Ratios representing a `Numeric` color.
pub const NUMERIC: ComplexColorRatios = ComplexColorRatios { bg: 1., fg: 0. };
/// Ratios representing the `Foreground` color.
pub const FOREGROUND: ComplexColorRatios = ComplexColorRatios { bg: 0., fg: 1. };
/// Ratios representing the `CurrentColor` color.
pub const CURRENT_COLOR: ComplexColorRatios = ComplexColorRatios { bg: 0., fg: 1. };
}
/// This enum represents a combined color from a numeric color and
/// the current foreground color (currentcolor keyword).
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToAnimatedValue, ToShmem)]
pub enum Color<RGBA> {
#[repr(C, u8)]
pub enum GenericColor<RGBA> {
/// Numeric RGBA color.
Numeric(RGBA),
/// The current foreground color.
Foreground,
CurrentColor,
/// A linear combination of numeric color and currentcolor.
/// The formula is: `color * ratios.bg + currentcolor * ratios.fg`.
Complex(RGBA, ComplexColorRatios),
Complex {
/// The actual numeric color.
color: RGBA,
/// The ratios of mixing between numeric and currentcolor.
ratios: ComplexColorRatios,
}
}
pub use self::GenericColor as Color;
impl<RGBA> Color<RGBA> {
/// Create a color based upon the specified ratios.
pub fn with_ratios(color: RGBA, ratios: ComplexColorRatios) -> Self {
if ratios == ComplexColorRatios::NUMERIC {
Color::Numeric(color)
} else if ratios == ComplexColorRatios::FOREGROUND {
Color::Foreground
} else if ratios == ComplexColorRatios::CURRENT_COLOR {
Color::CurrentColor
} else {
Color::Complex(color, ratios)
Color::Complex { color, ratios }
}
}
@ -55,7 +64,7 @@ impl<RGBA> Color<RGBA> {
/// Returns a complex color value representing currentcolor.
pub fn currentcolor() -> Self {
Color::Foreground
Color::CurrentColor
}
/// Whether it is a numeric color (no currentcolor component).
@ -65,7 +74,7 @@ impl<RGBA> Color<RGBA> {
/// Whether it is a currentcolor value (no numeric color component).
pub fn is_currentcolor(&self) -> bool {
matches!(*self, Color::Foreground)
matches!(*self, Color::CurrentColor)
}
}
@ -92,9 +101,12 @@ impl<RGBA> From<RGBA> for Color<RGBA> {
ToCss,
ToShmem,
)]
pub enum ColorOrAuto<C> {
/// A `<color>
#[repr(C, u8)]
pub enum GenericColorOrAuto<C> {
/// A `<color>`.
Color(C),
/// `auto`
Auto,
}
pub use self::GenericColorOrAuto as ColorOrAuto;