45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
use palette::convert::{IntoColor, IntoColorUnclamped, FromColorUnclamped};
|
|
use palette::encoding::srgb::Srgb;
|
|
use palette::Hsv;
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
pub struct RGB8 {
|
|
pub red: u8,
|
|
pub green: u8,
|
|
pub blue: u8
|
|
}
|
|
|
|
impl RGB8 {
|
|
fn new(red : u8, green : u8, blue : u8) -> Self {
|
|
Self {
|
|
red: red,
|
|
green: green,
|
|
blue: blue
|
|
}
|
|
}
|
|
}
|
|
|
|
impl FromColorUnclamped<Hsv<Srgb, u8>> for RGB8 {
|
|
fn from_color_unclamped(hsv: Hsv<Srgb, u8>) -> RGB8 {
|
|
if hsv.saturation == 0 {
|
|
return RGB8::new(hsv.value, hsv.value, hsv.value);
|
|
}
|
|
|
|
let region = hsv.hue.into_inner() / 43;
|
|
let remainder = (hsv.hue.into_inner() - (region * 43)) * 6;
|
|
|
|
let p = (hsv.value.wrapping_mul(255 - hsv.saturation).wrapping_shr(8));
|
|
let q = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8);
|
|
let t = (hsv.value.wrapping_mul(255 - ((hsv.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8);
|
|
|
|
match region {
|
|
0 => RGB8::new(hsv.value, t, p),
|
|
1 => RGB8::new(q, hsv.value, p),
|
|
2 => RGB8::new(p, hsv.value, t),
|
|
3 => RGB8::new(p, q, hsv.value),
|
|
4 => RGB8::new(t, p, hsv.value),
|
|
_ => RGB8::new(hsv.value, p, q)
|
|
}
|
|
}
|
|
}
|