use crate::BigColor;
pub fn is_light(color: &BigColor) -> bool {
color.to_oklch().l >= 0.5
}
pub fn get_contrast_color(color: &BigColor, intensity: f32) -> BigColor {
let is_light_color = is_light(color);
let (light_r, light_g, light_b) = (0, 0, 0); let (dark_r, dark_g, dark_b) = (255, 255, 255);
let intensity = intensity.max(0.0).min(1.0);
if is_light_color {
let r = ((128.0 * (1.0 - intensity)) + (light_r as f32 * intensity)) as u8;
let g = ((128.0 * (1.0 - intensity)) + (light_g as f32 * intensity)) as u8;
let b = ((128.0 * (1.0 - intensity)) + (light_b as f32 * intensity)) as u8;
BigColor::from_rgb(r, g, b, 1.0)
} else {
let r = ((128.0 * (1.0 - intensity)) + (dark_r as f32 * intensity)) as u8;
let g = ((128.0 * (1.0 - intensity)) + (dark_g as f32 * intensity)) as u8;
let b = ((128.0 * (1.0 - intensity)) + (dark_b as f32 * intensity)) as u8;
BigColor::from_rgb(r, g, b, 1.0)
}
}
pub fn get_contrast_ratio(color1: &BigColor, color2: &BigColor) -> f32 {
let l1 = color1.get_luminance();
let l2 = color2.get_luminance();
let (lighter, darker) = if l1 > l2 { (l1, l2) } else { (l2, l1) };
(lighter + 0.05) / (darker + 0.05)
}
pub fn calculate_luminance(color: &BigColor) -> f32 {
let rgb = color.to_rgb();
let r = to_linear(rgb.r as f32 / 255.0) * 0.2126;
let g = to_linear(rgb.g as f32 / 255.0) * 0.7152;
let b = to_linear(rgb.b as f32 / 255.0) * 0.0722;
r + g + b
}
pub fn to_linear(component: f32) -> f32 {
if component <= 0.03928 {
component / 12.92
} else {
((component + 0.055) / 1.055).powf(2.4)
}
}