use crate::Error;
use crate::style::LinePattern;
use crate::{CategoryValues, Color, NumericValues};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GuideStyle {
color: Color,
width: f32,
pattern: LinePattern,
}
impl GuideStyle {
pub fn new(color: Color, width: f32, pattern: LinePattern) -> Result<Self, Error> {
validate_guide_width(width)?;
Ok(Self {
color,
width,
pattern,
})
}
pub const fn get_color(&self) -> Color {
self.color
}
pub const fn get_width(&self) -> f32 {
self.width
}
pub const fn get_pattern(&self) -> LinePattern {
self.pattern
}
pub fn color(self, color: Color) -> Result<Self, Error> {
Self::new(color, self.width, self.pattern)
}
pub fn width(self, width: f32) -> Result<Self, Error> {
Self::new(self.color, width, self.pattern)
}
pub fn pattern(self, pattern: LinePattern) -> Result<Self, Error> {
Self::new(self.color, self.width, pattern)
}
}
pub(crate) fn validate_guide_width(width: f32) -> Result<(), Error> {
if !width.is_finite() || width <= 0.0 {
return Err(Error::Data(
format!("guide width must be finite and strictly positive; got {width}").into(),
));
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum GuideGeometry {
Horizontal {
y: f64,
},
Vertical {
x: f64,
},
Segment {
start: (f64, f64),
end: (f64, f64),
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct GuideSpec {
geometry: GuideGeometry,
color: Option<Color>,
width: Option<f32>,
pattern: Option<LinePattern>,
}
impl GuideSpec {
const fn new(geometry: GuideGeometry) -> Self {
Self {
geometry,
color: None,
width: None,
pattern: None,
}
}
#[must_use]
pub const fn horizontal(y: f64) -> Self {
Self::new(GuideGeometry::Horizontal { y })
}
#[must_use]
pub const fn vertical(x: f64) -> Self {
Self::new(GuideGeometry::Vertical { x })
}
#[must_use]
pub const fn segment(x_start: f64, y_start: f64, x_end: f64, y_end: f64) -> Self {
Self::new(GuideGeometry::Segment {
start: (x_start, y_start),
end: (x_end, y_end),
})
}
pub const fn geometry(&self) -> &GuideGeometry {
&self.geometry
}
#[must_use]
pub fn style(self, style: GuideStyle) -> Self {
Self {
geometry: self.geometry,
color: Some(style.color),
width: Some(style.width),
pattern: Some(style.pattern),
}
}
#[must_use]
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn width(mut self, width: f32) -> Result<Self, Error> {
validate_guide_width(width)?;
self.width = Some(width);
Ok(self)
}
#[must_use]
pub fn pattern(mut self, pattern: LinePattern) -> Self {
self.pattern = Some(pattern);
self
}
pub const fn get_color(&self) -> Option<Color> {
self.color
}
pub const fn get_width(&self) -> Option<f32> {
self.width
}
pub const fn get_pattern(&self) -> Option<LinePattern> {
self.pattern
}
pub(crate) fn replace_visual(
&mut self,
color: Option<Color>,
width: Option<f32>,
pattern: Option<LinePattern>,
) {
self.color = color;
self.width = width;
self.pattern = pattern;
}
pub(crate) fn line_style(&self, theme: &crate::Theme) -> crate::render::backend::LineStyle {
let defaults = theme
.marks
.get_strokes()
.get_references()
.get_estimate_reference();
crate::render::backend::LineStyle {
color: self.color.unwrap_or(defaults.get_color()),
width: self.width.unwrap_or(defaults.get_width()),
pattern: self.pattern.unwrap_or(defaults.get_pattern()),
}
}
}
#[derive(Debug, Clone)]
pub struct PointLayer {
pub(crate) identity: crate::data::InstanceId,
pub(crate) label_layout: Option<crate::text::TextLayout>,
x: NumericValues,
y: NumericValues,
labels: Option<CategoryValues>,
legend_label: crate::Text,
color: Option<Color>,
radius: Option<u32>,
alpha: Option<f64>,
}
#[derive(Debug, Clone)]
pub struct BarPointLayer {
pub(crate) identity: crate::data::InstanceId,
category: CategoryValues,
series: Option<CategoryValues>,
value: NumericValues,
color: Option<Color>,
radius: Option<u32>,
}
impl BarPointLayer {
#[must_use]
pub fn new(category: CategoryValues, value: NumericValues) -> Self {
Self {
identity: crate::data::InstanceId::new(),
category,
series: None,
value,
color: None,
radius: None,
}
}
#[must_use]
pub fn series(mut self, series: CategoryValues) -> Self {
self.series = Some(series);
self
}
#[must_use]
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn radius(mut self, radius: u32) -> Result<Self, Error> {
validate_point_radius("bar-point-layer", radius)?;
self.radius = Some(radius);
Ok(self)
}
#[must_use]
pub const fn category_values(&self) -> &CategoryValues {
&self.category
}
#[must_use]
pub const fn series_values(&self) -> Option<&CategoryValues> {
self.series.as_ref()
}
#[must_use]
pub const fn values(&self) -> &NumericValues {
&self.value
}
#[must_use]
pub const fn get_color(&self) -> Option<Color> {
self.color
}
#[must_use]
pub const fn get_radius(&self) -> Option<u32> {
self.radius
}
pub(crate) fn replace_visual(&mut self, color: Option<Color>, radius: Option<u32>) {
self.color = color;
self.radius = radius;
}
pub(crate) fn point_style(&self, theme: &crate::Theme) -> crate::render::backend::PointStyle {
let defaults = theme.marks.get_points().get_supplemental();
let color = self.color.unwrap_or_else(|| {
crate::theme::color_from_rgb(theme.marks.get_neutral_paints().get_path().as_rgb())
.with_alpha(defaults.get_fill_alpha())
});
crate::render::backend::PointStyle {
color,
radius: self.radius.unwrap_or(defaults.get_radius()),
outline: None,
shape: crate::style::PointShape::Circle,
}
}
}
impl PointLayer {
#[must_use]
pub fn label_text_layout(mut self, layout: crate::text::TextLayout) -> Self {
self.label_layout = Some(layout);
self
}
#[must_use]
pub fn new(x: NumericValues, y: NumericValues) -> Self {
Self {
identity: crate::data::InstanceId::new(),
x,
label_layout: None,
y,
labels: None,
legend_label: crate::Text::UNSET,
color: None,
radius: None,
alpha: None,
}
}
#[must_use]
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn radius(mut self, radius: u32) -> Result<Self, Error> {
validate_point_radius("point-layer", radius)?;
self.radius = Some(radius);
Ok(self)
}
pub fn alpha(mut self, alpha: f64) -> Result<Self, Error> {
self.alpha = Some(crate::alpha::validated_alpha(alpha)?);
Ok(self)
}
pub const fn get_color(&self) -> Option<Color> {
self.color
}
pub const fn get_alpha(&self) -> Option<f64> {
self.alpha
}
pub const fn get_radius(&self) -> Option<u32> {
self.radius
}
#[must_use]
pub fn labels(mut self, labels: CategoryValues) -> Self {
self.labels = Some(labels);
self
}
#[must_use]
pub fn legend_label(mut self, label: impl Into<crate::Text>) -> Self {
self.legend_label
.replace_from(label.into())
.expect("public Text always has a source");
self
}
#[must_use]
pub fn clear_legend_label_style(mut self) -> Self {
self.legend_label.edit_clear_style();
self
}
#[must_use]
pub fn clear_legend_label_layout(mut self) -> Self {
self.legend_label.edit_clear_layout();
self
}
pub(crate) const fn x_values(&self) -> &NumericValues {
&self.x
}
pub(crate) const fn y_values(&self) -> &NumericValues {
&self.y
}
pub(crate) const fn label_values(&self) -> Option<&CategoryValues> {
self.labels.as_ref()
}
pub(crate) const fn legend_content(&self) -> Option<&crate::Text> {
self.legend_label.provided()
}
pub(crate) fn replace_visual(
&mut self,
color: Option<Color>,
radius: Option<u32>,
alpha: Option<f64>,
legend: crate::Text,
layout: Option<crate::text::TextLayout>,
) {
self.color = color;
self.radius = radius;
self.alpha = alpha;
self.legend_label.apply_appearance(legend);
self.label_layout = layout;
}
pub(crate) fn point_style(
&self,
automatic_color: (u8, u8, u8),
defaults: &crate::theme::mark::SupplementalPointTheme,
) -> crate::render::backend::PointStyle {
let color = self.color.unwrap_or_else(|| {
crate::theme::color_from_rgb(automatic_color).with_alpha(defaults.get_fill_alpha())
});
crate::render::backend::PointStyle {
color: self.alpha.map_or(color, |alpha| color.with_alpha(alpha)),
radius: self.radius.unwrap_or(defaults.get_radius()),
outline: None,
shape: crate::style::PointShape::Circle,
}
}
pub(crate) fn label_appearance(
&self,
role: &crate::theme::TextTheme,
) -> Result<crate::theme::TextTheme, Error> {
let paint = self.color.unwrap_or(role.get_color());
role.try_copy()?
.color(self.alpha.map_or(paint, |alpha| paint.with_alpha(alpha)))
}
}
pub(crate) fn validate_point_radius(role: &str, radius: u32) -> Result<(), Error> {
if radius == 0 {
return Err(Error::Data(
format!("{role} radius must be greater than zero").into(),
));
}
Ok(())
}