mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
migrate Color -> ItemColor and remove unused
This commit is contained in:
parent
33600e890f
commit
eb1e3179a4
6 changed files with 59 additions and 76 deletions
|
|
@ -2,33 +2,37 @@ use crate::*;
|
|||
use rand::{thread_rng, distributions::uniform::UniformSampler};
|
||||
pub use palette::{*, convert::*, okhsl::*};
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
/// A color in OKHSL and RGB representations.
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
pub struct ItemColor {
|
||||
pub okhsl: Okhsl<f32>,
|
||||
pub rgb: Color,
|
||||
}
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
/// A color in OKHSL and RGB with lighter and darker variants.
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
pub struct ItemColorTriplet {
|
||||
pub base: ItemColor,
|
||||
pub light: ItemColor,
|
||||
pub dark: ItemColor,
|
||||
}
|
||||
/// Adds TUI RGB representation to an OKHSL value.
|
||||
impl From<Okhsl<f32>> for ItemColor {
|
||||
fn from (okhsl: Okhsl<f32>) -> Self {
|
||||
Self { okhsl, rgb: okhsl_to_rgb(okhsl) }
|
||||
}
|
||||
fn from (okhsl: Okhsl<f32>) -> Self { Self { okhsl, rgb: okhsl_to_rgb(okhsl) } }
|
||||
}
|
||||
/// Adds OKHSL representation to a TUI RGB value.
|
||||
impl From<Color> for ItemColor {
|
||||
fn from (rgb: Color) -> Self { Self { rgb, okhsl: rgb_to_okhsl(rgb) } }
|
||||
}
|
||||
impl ItemColor {
|
||||
pub fn random () -> Self {
|
||||
random_okhsl().into()
|
||||
}
|
||||
pub fn random () -> Self { random_okhsl().into() }
|
||||
pub fn random_dark () -> Self {
|
||||
random_okhsl_dark().into()
|
||||
let mut rng = thread_rng();
|
||||
let lo = Okhsl::new(-180.0, 0.01, 0.05);
|
||||
let hi = Okhsl::new( 180.0, 0.5, 0.2);
|
||||
UniformOkhsl::new(lo, hi).sample(&mut rng).into()
|
||||
}
|
||||
pub fn random_near (other: Self, distance: f32) -> Self {
|
||||
if distance > 1.0 {
|
||||
panic!("random_color_near requires distance between 0.0 and 1.0");
|
||||
}
|
||||
if distance > 1.0 { panic!("ItemColor::random_near takes distance between 0.0 and 1.0"); }
|
||||
other.okhsl.mix(random_okhsl(), distance).into()
|
||||
}
|
||||
}
|
||||
|
|
@ -46,18 +50,9 @@ impl From<ItemColor> for ItemColorTriplet {
|
|||
|
||||
pub fn random_okhsl () -> Okhsl<f32> {
|
||||
let mut rng = thread_rng();
|
||||
UniformOkhsl::new(
|
||||
Okhsl::new(-180.0, 0.01, 0.2),
|
||||
Okhsl::new( 180.0, 0.9, 0.5),
|
||||
).sample(&mut rng)
|
||||
}
|
||||
|
||||
pub fn random_okhsl_dark () -> Okhsl<f32> {
|
||||
let mut rng = thread_rng();
|
||||
UniformOkhsl::new(
|
||||
Okhsl::new(-180.0, 0.01, 0.05),
|
||||
Okhsl::new( 180.0, 0.5, 0.2),
|
||||
).sample(&mut rng)
|
||||
let lo = Okhsl::new(-180.0, 0.01, 0.2);
|
||||
let hi = Okhsl::new( 180.0, 0.9, 0.5);
|
||||
UniformOkhsl::new(lo, hi).sample(&mut rng)
|
||||
}
|
||||
|
||||
pub fn okhsl_to_rgb (color: Okhsl<f32>) -> Color {
|
||||
|
|
@ -65,26 +60,14 @@ pub fn okhsl_to_rgb (color: Okhsl<f32>) -> Color {
|
|||
Color::Rgb((red * 255.0) as u8, (green * 255.0) as u8, (blue * 255.0) as u8,)
|
||||
}
|
||||
|
||||
pub fn random_color () -> Color {
|
||||
okhsl_to_rgb(random_okhsl())
|
||||
}
|
||||
|
||||
pub fn random_color_dark () -> Color {
|
||||
okhsl_to_rgb(random_okhsl_dark())
|
||||
}
|
||||
|
||||
pub fn random_color_near (color: Color, distance: f32) -> Color {
|
||||
let (r, g, b) = if let Color::Rgb(r, g, b) = color {
|
||||
(r, g, b)
|
||||
pub fn rgb_to_okhsl (color: Color) -> Okhsl<f32> {
|
||||
if let Color::Rgb(r, g, b) = color {
|
||||
Okhsl::from_color(Srgb::new(
|
||||
r as f32 / 255.0,
|
||||
g as f32 / 255.0,
|
||||
b as f32 / 255.0,
|
||||
))
|
||||
} else {
|
||||
panic!("random_color_near works only with Color::Rgb")
|
||||
};
|
||||
if distance > 1.0 {
|
||||
panic!("random_color_near requires distance between 0.0 and 1.0");
|
||||
unreachable!("only Color::Rgb is supported")
|
||||
}
|
||||
okhsl_to_rgb(Okhsl::from_color(Srgb::new(
|
||||
r as f32 / 255.0,
|
||||
g as f32 / 255.0,
|
||||
b as f32 / 255.0,
|
||||
)).mix(random_okhsl(), distance))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue