use std::ops::{Mul, MulAssign};
use std::str::FromStr;
use knuffel::errors::DecodeError;
use miette::{miette, IntoDiagnostic as _};
use smithay::backend::renderer::Color32F;
use crate::utils::{Flag, MergeWith};
use crate::FloatOrInt;
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::from_array_unpremul([0.25, 0.25, 0.25, 1.]);
pub const DEFAULT_BACKDROP_COLOR: Color = Color::from_array_unpremul([0.15, 0.15, 0.15, 1.]);
/// RGB color in [0, 1] with unpremultiplied alpha.
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const fn new_unpremul(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub fn from_rgba8_unpremul(r: u8, g: u8, b: u8, a: u8) -> Self {
Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
}
pub fn from_array_premul([r, g, b, a]: [f32; 4]) -> Self {
let a = a.clamp(0., 1.);
if a == 0. {
Self::new_unpremul(0., 0., 0., 0.)
} else {
Self {
r: (r / a).clamp(0., 1.),
g: (g / a).clamp(0., 1.),
b: (b / a).clamp(0., 1.),
a,
}
}
}
pub const fn from_array_unpremul([r, g, b, a]: [f32; 4]) -> Self {
Self { r, g, b, a }
}
pub fn from_color32f(color: Color32F) -> Self {
Self::from_array_premul(color.components())
}
pub fn to_array_unpremul(self) -> [f32; 4] {
[self.r, self.g, self.b,