use aksr::Builder;
use crate::{Color, ColorSource, Palette, Skeleton, TextLoc, TextStyle, TextStyleMode};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum KeypointStyleMode {
Circle,
Star {
points: usize,
inner_ratio: f32,
},
Square,
Cross {
thickness: usize,
},
Diamond,
Triangle,
X {
thickness: usize,
},
RoundedSquare {
corner_ratio: f32,
},
Glow {
glow_multiplier: f32,
},
}
impl Default for KeypointStyleMode {
fn default() -> Self {
Self::Circle
}
}
impl KeypointStyleMode {
pub fn star() -> Self {
Self::Star {
points: 5,
inner_ratio: 0.5,
}
}
pub fn cross() -> Self {
Self::Cross { thickness: 2 }
}
pub fn x() -> Self {
Self::X { thickness: 2 }
}
pub fn rounded_square() -> Self {
Self::RoundedSquare { corner_ratio: 0.3 }
}
pub fn glow() -> Self {
Self::Glow {
glow_multiplier: 2.0,
}
}
pub fn glow_with(glow_multiplier: f32) -> Self {
Self::Glow { glow_multiplier }
}
}
#[derive(Debug, Clone, Builder, PartialEq)]
pub struct KeypointStyle {
visible: bool,
text_visible: bool,
draw_fill: bool,
draw_outline: bool,
fill_color: ColorSource,
outline_color: ColorSource,
mode: KeypointStyleMode,
radius: usize,
thickness: usize,
skeleton: Option<Skeleton>,
skeleton_thickness: usize,
text_style: TextStyle,
palette: Vec<Color>,
}
impl Default for KeypointStyle {
fn default() -> Self {
Self {
visible: true,
text_visible: true,
draw_fill: true,
draw_outline: true,
fill_color: ColorSource::Auto,
outline_color: ColorSource::Auto,
mode: KeypointStyleMode::default(),
radius: 4,
thickness: 2,
skeleton: None,
skeleton_thickness: 2,
text_style: TextStyle::default()
.with_mode(TextStyleMode::rounded(2.0, 3.0))
.with_loc(TextLoc::OuterTopRight)
.with_thickness(2)
.with_draw_fill(true)
.with_draw_outline(true)
.with_bg_fill_color(ColorSource::InheritFillAlpha(220))
.with_bg_outline_color(ColorSource::Custom(Color::black()))
.with_id(true)
.with_name(false)
.with_confidence(false),
palette: Color::palette_base_20(),
}
}
}
impl Palette for KeypointStyle {
fn palette(&self) -> &[Color] {
&self.palette
}
}
impl KeypointStyle {
pub fn star() -> Self {
Self {
mode: KeypointStyleMode::star(),
..Default::default()
}
}
pub fn glow() -> Self {
Self {
mode: KeypointStyleMode::glow(),
..Default::default()
}
}
pub fn show_confidence(mut self, show: bool) -> Self {
self.text_style = self.text_style.with_confidence(show);
self
}
pub fn show_id(mut self, show: bool) -> Self {
self.text_style = self.text_style.with_id(show);
self
}
pub fn show_name(mut self, show: bool) -> Self {
self.text_style = self.text_style.with_name(show);
self
}
}