libcosmic/cosmic-theme/src/steps.rs

138 lines
5 KiB
Rust
Raw Normal View History

2023-08-03 15:03:23 -04:00
use almost::equal;
2023-08-03 19:30:08 -04:00
use palette::{convert::FromColorUnclamped, ClampAssign, Oklcha, Srgb, Srgba};
2023-08-03 15:03:23 -04:00
/// Get an array of 100 colors with a specific hue and chroma
/// over the full range of lightness.
2023-08-03 19:30:08 -04:00
/// Colors which are not valid Srgba will fallback to a color with the nearest valid chroma.
pub fn steps(mut c: Oklcha) -> [Srgba; 100] {
let mut steps = [Srgba::new(0.0, 0.0, 0.0, 1.0); 100];
2023-08-03 15:03:23 -04:00
for i in 0..steps.len() {
let lightness = i as f32 / 100.0;
c.l = lightness;
steps[i] = oklch_to_srgba_nearest_chroma(c)
}
steps
}
2023-08-03 19:30:08 -04:00
/// find the nearest chroma which makes our color a valid color in Srgba
pub fn oklch_to_srgba_nearest_chroma(mut c: Oklcha) -> Srgba {
2023-08-03 15:03:23 -04:00
let mut r_chroma = c.chroma;
let mut l_chroma = 0.0;
// exit early if we found it right away
2023-08-03 19:30:08 -04:00
let mut new_c = Srgba::from_color_unclamped(c);
2023-08-03 15:03:23 -04:00
if is_valid_srgb(new_c) {
new_c.clamp_assign();
return new_c;
}
// is this an excessive depth to search?
for _ in 0..64 {
2023-08-03 19:30:08 -04:00
let new_c = Srgba::from_color_unclamped(c);
2023-08-03 15:03:23 -04:00
if is_valid_srgb(new_c) {
l_chroma = c.chroma;
c.chroma = (c.chroma + r_chroma) / 2.0;
} else {
r_chroma = c.chroma;
c.chroma = (c.chroma + l_chroma) / 2.0;
}
}
2023-08-03 19:30:08 -04:00
Srgba::from_color_unclamped(c)
2023-08-03 15:03:23 -04:00
}
/// checks that the color is valid srgb
2023-08-03 19:30:08 -04:00
pub fn is_valid_srgb(c: Srgba) -> bool {
2023-08-03 15:03:23 -04:00
(equal(c.red, Srgb::max_red()) || (c.red >= Srgb::min_red() && c.red <= Srgb::max_red()))
&& (equal(c.blue, Srgb::max_blue())
|| (c.blue >= Srgb::min_blue() && c.blue <= Srgb::max_blue()))
&& (equal(c.green, Srgb::max_green())
|| (c.green >= Srgb::min_green() && c.green <= Srgb::max_green()))
}
#[cfg(test)]
mod tests {
use almost::equal;
2023-08-03 19:30:08 -04:00
use palette::{OklabHue, Srgba};
2023-08-03 15:03:23 -04:00
use super::{is_valid_srgb, oklch_to_srgba_nearest_chroma};
#[test]
fn test_valid_check() {
2023-08-03 19:30:08 -04:00
assert!(is_valid_srgb(Srgba::new(1.0, 1.0, 1.0, 1.0)));
assert!(is_valid_srgb(Srgba::new(0.0, 0.0, 0.0, 1.0)));
assert!(is_valid_srgb(Srgba::new(0.5, 0.5, 0.5, 1.0)));
assert!(!is_valid_srgb(Srgba::new(-0.1, 0.0, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(0.0, -0.1, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(-0.0, 0.0, -0.1, 1.0)));
assert!(!is_valid_srgb(Srgba::new(-100.1, 0.0, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(0.0, -100.1, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(-0.0, 0.0, -100.1, 1.0)));
assert!(!is_valid_srgb(Srgba::new(1.1, 0.0, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(0.0, 1.1, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(-0.0, 0.0, 1.1, 1.0)));
assert!(!is_valid_srgb(Srgba::new(100.1, 0.0, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(0.0, 100.1, 0.0, 1.0)));
assert!(!is_valid_srgb(Srgba::new(-0.0, 0.0, 100.1, 1.0)));
2023-08-03 15:03:23 -04:00
}
#[test]
fn test_conversion_boundaries() {
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.0, 0.288, OklabHue::from_degrees(0.0), 1.0);
2023-08-03 15:03:23 -04:00
let srgb = oklch_to_srgba_nearest_chroma(c1);
equal(srgb.red, 0.0);
equal(srgb.blue, 0.0);
equal(srgb.green, 0.0);
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(1.0, 0.288, OklabHue::from_degrees(0.0), 1.0);
2023-08-03 15:03:23 -04:00
let srgb = oklch_to_srgba_nearest_chroma(c1);
equal(srgb.red, 1.0);
equal(srgb.blue, 1.0);
equal(srgb.green, 1.0);
}
#[test]
fn test_conversion_colors() {
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.4608, 0.11111, OklabHue::new(57.31), 1.0);
let srgb = oklch_to_srgba_nearest_chroma(c1).into_format::<u8, u8>();
2023-08-03 15:03:23 -04:00
assert!(srgb.red == 133);
assert!(srgb.green == 69);
assert!(srgb.blue == 0);
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.30, 0.08, OklabHue::new(35.0), 1.0);
let srgb = oklch_to_srgba_nearest_chroma(c1).into_format::<u8, u8>();
2023-08-03 15:03:23 -04:00
assert!(srgb.red == 78);
assert!(srgb.green == 27);
assert!(srgb.blue == 15);
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.757, 0.146, OklabHue::new(301.2), 1.0);
let srgb = oklch_to_srgba_nearest_chroma(c1).into_format::<u8, u8>();
2023-08-03 15:03:23 -04:00
assert!(srgb.red == 192);
assert!(srgb.green == 153);
assert!(srgb.blue == 253);
}
#[test]
fn test_conversion_fallback_colors() {
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.70, 0.284, OklabHue::new(35.0), 1.0);
let srgb = oklch_to_srgba_nearest_chroma(c1).into_format::<u8, u8>();
2023-08-03 15:03:23 -04:00
assert!(srgb.red == 255);
assert!(srgb.green == 103);
assert!(srgb.blue == 65);
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.757, 0.239, OklabHue::new(301.2), 1.0);
let srgb = oklch_to_srgba_nearest_chroma(c1).into_format::<u8, u8>();
2023-08-03 15:03:23 -04:00
assert!(srgb.red == 193);
assert!(srgb.green == 152);
assert!(srgb.blue == 255);
2023-08-03 19:30:08 -04:00
let c1 = palette::Oklcha::new(0.163, 0.333, OklabHue::new(141.0), 1.0);
let srgb = oklch_to_srgba_nearest_chroma(c1).into_format::<u8, u8>();
2023-08-03 15:03:23 -04:00
assert!(srgb.red == 1);
assert!(srgb.green == 19);
assert!(srgb.blue == 0);
}
}