use crate::render::backend::{FillStyle, LineStyle, PointOutline, PointStyle, StrokePattern};
use crate::{Error, PointShape, Result};
use super::{color_from_rgb, Color, RelativeSpacing};
fn validate_width(width: f32) -> Result<()> {
if width.is_finite() && width > 0.0 {
Ok(())
} else {
Err(Error::Data(
"present stroke width must be finite and positive CSS pixels".into(),
))
}
}
macro_rules! solid_paint {
($name:ident, $documentation:literal) => {
#[doc = $documentation]
#[derive(Debug, Clone, PartialEq)]
pub struct $name {
pub(crate) paint: Color,
pub(crate) width: f32,
}
impl $name {
pub fn new(color: Color, width: f32) -> Result<Self> {
let value = Self {
paint: color,
width,
};
value.validate()?;
Ok(value)
}
pub(crate) fn validate(&self) -> Result<()> {
validate_width(self.get_width())
}
pub fn get_color(&self) -> Color {
self.paint
}
pub fn get_width(&self) -> f32 {
self.width
}
pub fn color(self, color: Color) -> Result<Self> {
Self::new(color, self.width)
}
pub fn width(self, width: f32) -> Result<Self> {
Self::new(self.paint, width)
}
}
};
}
solid_paint!(
SolidStrokeTheme,
"Complete solid bracket stroke; its topology has no pattern edit."
);
solid_paint!(
OutlineTheme,
"Complete independent outline paint and positive CSS width."
);
#[derive(Debug, Clone, PartialEq)]
pub struct StrokeTheme {
pub(crate) paint: Color,
pub(crate) width: f32,
pub(crate) pattern: StrokePattern,
}
impl StrokeTheme {
pub(crate) fn validate(&self) -> Result<()> {
validate_width(self.get_width())
}
pub fn new(color: Color, width: f32, pattern: StrokePattern) -> Result<Self> {
validate_width(width)?;
Ok(Self {
paint: color,
width,
pattern,
})
}
pub fn get_color(&self) -> Color {
self.paint
}
pub fn get_width(&self) -> f32 {
self.width
}
pub fn get_pattern(&self) -> StrokePattern {
self.pattern
}
pub fn color(self, color: Color) -> Result<Self> {
Self::new(color, self.width, self.pattern)
}
pub fn width(self, width: f32) -> Result<Self> {
Self::new(self.paint, width, self.pattern)
}
pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
Self::new(self.paint, self.width, pattern)
}
pub(crate) fn backend_style(&self) -> LineStyle {
LineStyle {
color: self.get_color(),
width: self.get_width(),
pattern: self.get_pattern(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PointOutlineTheme {
pub(crate) color: Color,
pub(crate) width_ratio: RelativeSpacing,
}
impl PointOutlineTheme {
pub fn new(color: Color, width_ratio: RelativeSpacing) -> Result<Self> {
if width_ratio.is_zero() {
return Err(Error::Data(
"point outline width ratio must be positive".into(),
));
}
Ok(Self { color, width_ratio })
}
fn validate(&self) -> Result<()> {
if self.width_ratio.is_zero() {
Err(Error::Data(
"point outline width ratio must be positive".into(),
))
} else {
Ok(())
}
}
pub fn get_color(&self) -> Color {
self.color
}
pub fn get_width_ratio(&self) -> &RelativeSpacing {
&self.width_ratio
}
pub fn color(self, color: Color) -> Result<Self> {
Self::new(color, self.width_ratio)
}
pub fn width_ratio(self, width_ratio: RelativeSpacing) -> Result<Self> {
Self::new(self.color, width_ratio)
}
pub(crate) fn publication() -> Result<Self> {
Self::new(Color::rgb(255, 255, 255), RelativeSpacing::new(0.1)?)
}
pub(crate) fn backend_style(&self) -> PointOutline {
PointOutline {
color: self.get_color(),
width_ratio: self.get_width_ratio().clone(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DataOutlineTheme {
choice: DataOutline,
}
#[derive(Debug, Clone, PartialEq)]
enum DataOutline {
Fixed(OutlineTheme),
Mapped { width: f32, alpha: u8 },
}
impl DataOutlineTheme {
pub fn fixed(stroke: OutlineTheme) -> Self {
Self {
choice: DataOutline::Fixed(stroke),
}
}
pub fn mapped(width: f32, alpha: u8) -> Result<Self> {
validate_width(width)?;
Ok(Self {
choice: DataOutline::Mapped { width, alpha },
})
}
pub fn fixed_stroke(&self) -> Option<&OutlineTheme> {
match &self.choice {
DataOutline::Fixed(stroke) => Some(stroke),
DataOutline::Mapped { .. } => None,
}
}
pub fn mapped_width(&self) -> Option<f32> {
match self.choice {
DataOutline::Fixed(_) => None,
DataOutline::Mapped { width, .. } => Some(width),
}
}
pub fn mapped_alpha(&self) -> Option<u8> {
match self.choice {
DataOutline::Fixed(_) => None,
DataOutline::Mapped { alpha, .. } => Some(alpha),
}
}
fn validate(&self) -> Result<()> {
match &self.choice {
DataOutline::Fixed(stroke) => stroke.validate(),
DataOutline::Mapped { width, .. } => validate_width(*width),
}
}
fn operands(&self, mapped: Color) -> (Color, f32) {
if let Some(stroke) = self.fixed_stroke() {
(stroke.get_color(), stroke.get_width())
} else {
(
mapped.alpha(self.mapped_alpha().expect("validated mapped outline")),
self.mapped_width().expect("validated mapped outline"),
)
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DataFillTheme {
pub(crate) fill_alpha: u8,
pub(crate) outline: Option<DataOutlineTheme>,
}
impl DataFillTheme {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(outline) = self.get_outline() {
outline.validate()?;
}
Ok(())
}
pub fn new(fill_alpha: u8, outline: Option<DataOutlineTheme>) -> Result<Self> {
let value = Self {
fill_alpha,
outline,
};
value.validate()?;
Ok(value)
}
pub fn get_fill_alpha(&self) -> u8 {
self.fill_alpha
}
pub fn get_outline(&self) -> Option<&DataOutlineTheme> {
self.outline.as_ref()
}
pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
Self::new(fill_alpha, self.outline)
}
pub fn outline(self, outline: Option<DataOutlineTheme>) -> Result<Self> {
Self::new(self.fill_alpha, outline)
}
pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> FillStyle {
let color = color_from_rgb(rgb);
let (stroke, width) = self
.get_outline()
.map_or((Color::rgba(0, 0, 0, 0), 0.0), |outline| {
outline.operands(color)
});
FillStyle::new(color.alpha(self.get_fill_alpha()), stroke, width)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FixedFillTheme {
pub(crate) paint: Color,
pub(crate) outline: Option<OutlineTheme>,
}
impl FixedFillTheme {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(outline) = self.get_outline() {
outline.validate()?;
}
Ok(())
}
pub fn new(color: Color, outline: Option<OutlineTheme>) -> Result<Self> {
let value = Self {
paint: color,
outline,
};
value.validate()?;
Ok(value)
}
pub fn get_color(&self) -> Color {
self.paint
}
pub fn get_outline(&self) -> Option<&OutlineTheme> {
self.outline.as_ref()
}
pub fn color(self, color: Color) -> Result<Self> {
Self::new(color, self.outline)
}
pub fn outline(self, outline: Option<OutlineTheme>) -> Result<Self> {
Self::new(self.paint, outline)
}
pub(crate) fn backend_style(&self) -> FillStyle {
self.get_outline().map_or_else(
|| FillStyle::new(self.get_color(), Color::rgba(0, 0, 0, 0), 0.0),
|outline| outline.backend_style(self.get_color()),
)
}
}
impl OutlineTheme {
pub(crate) fn backend_style(&self, fill: Color) -> FillStyle {
FillStyle::new(fill, self.get_color(), self.get_width())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PointTheme {
pub(crate) fill_alpha: u8,
pub(crate) shape: PointShape,
pub(crate) outline: Option<PointOutlineTheme>,
}
impl PointTheme {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(outline) = self.get_outline() {
outline.validate()?;
}
Ok(())
}
pub fn new(
fill_alpha: u8,
shape: PointShape,
outline: Option<PointOutlineTheme>,
) -> Result<Self> {
let value = Self {
fill_alpha,
shape,
outline,
};
value.validate()?;
Ok(value)
}
pub fn get_fill_alpha(&self) -> u8 {
self.fill_alpha
}
pub fn get_shape(&self) -> PointShape {
self.shape
}
pub fn get_outline(&self) -> Option<&PointOutlineTheme> {
self.outline.as_ref()
}
pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
Self::new(fill_alpha, self.shape, self.outline)
}
pub fn shape(self, shape: PointShape) -> Result<Self> {
Self::new(self.fill_alpha, shape, self.outline)
}
pub fn outline(self, outline: Option<PointOutlineTheme>) -> Result<Self> {
Self::new(self.fill_alpha, self.shape, outline)
}
pub(crate) fn backend_style(
&self,
rgb: (u8, u8, u8),
radius: u32,
outline_visible: bool,
) -> PointStyle {
PointStyle {
color: color_from_rgb(rgb).alpha(self.get_fill_alpha()),
radius,
shape: self.get_shape(),
outline: self
.get_outline()
.filter(|_| outline_visible)
.map(PointOutlineTheme::backend_style),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DataLineTheme {
pub(crate) width: f32,
pub(crate) alpha: u8,
pub(crate) pattern: StrokePattern,
}
impl DataLineTheme {
pub(crate) fn validate(&self) -> Result<()> {
validate_width(self.get_width())
}
pub fn new(width: f32, alpha: u8, pattern: StrokePattern) -> Result<Self> {
let value = Self {
width,
alpha,
pattern,
};
value.validate()?;
Ok(value)
}
pub fn get_width(&self) -> f32 {
self.width
}
pub fn get_alpha(&self) -> u8 {
self.alpha
}
pub fn get_pattern(&self) -> StrokePattern {
self.pattern
}
pub fn width(self, width: f32) -> Result<Self> {
Self::new(width, self.alpha, self.pattern)
}
pub fn alpha(self, alpha: u8) -> Result<Self> {
Self::new(self.width, alpha, self.pattern)
}
pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
Self::new(self.width, self.alpha, pattern)
}
pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> LineStyle {
LineStyle {
color: color_from_rgb(rgb).alpha(self.get_alpha()),
width: self.get_width(),
pattern: self.get_pattern(),
}
}
}