1use crate::render::backend::{FillStyle, LineStyle, PointOutline, PointStyle};
4use crate::style::LinePattern;
5use crate::{style::PointShape, Error, Result};
6
7use super::{color_from_rgb, Color, RelativeSpacing};
8
9fn validate_width(width: f32) -> Result<()> {
10 if width.is_finite() && width > 0.0 {
11 Ok(())
12 } else {
13 Err(Error::Data(
14 "present stroke width must be finite and positive CSS pixels".into(),
15 ))
16 }
17}
18
19macro_rules! solid_paint {
21 ($name:ident, $documentation:literal) => {
22 #[doc = $documentation]
23 #[derive(Debug, Clone, PartialEq)]
24 pub struct $name {
25 pub(crate) paint: Color,
26 pub(crate) width: f32,
27 }
28
29 impl $name {
30 pub fn new(color: Color, width: f32) -> Result<Self> {
32 let value = Self {
33 paint: color,
34 width,
35 };
36 value.validate()?;
37 Ok(value)
38 }
39
40 pub(crate) fn validate(&self) -> Result<()> {
41 validate_width(self.get_width())
42 }
43
44 pub fn get_color(&self) -> Color {
46 self.paint
47 }
48 pub fn get_width(&self) -> f32 {
50 self.width
51 }
52
53 pub fn color(self, color: Color) -> Result<Self> {
55 Self::new(color, self.width)
56 }
57
58 pub fn width(self, width: f32) -> Result<Self> {
60 Self::new(self.paint, width)
61 }
62 }
63 };
64}
65
66solid_paint!(
67 SolidStrokeTheme,
68 "Complete solid bracket stroke; its topology has no pattern edit."
69);
70solid_paint!(
71 OutlineTheme,
72 "Complete independent outline paint and positive CSS width."
73);
74
75#[derive(Debug, Clone, PartialEq)]
77pub struct StrokeTheme {
78 pub(crate) paint: Color,
79 pub(crate) width: f32,
80 pub(crate) pattern: LinePattern,
81}
82
83impl StrokeTheme {
84 pub(crate) fn validate(&self) -> Result<()> {
85 validate_width(self.get_width())
86 }
87 pub fn new(color: Color, width: f32, pattern: LinePattern) -> Result<Self> {
89 validate_width(width)?;
90 Ok(Self {
91 paint: color,
92 width,
93 pattern,
94 })
95 }
96
97 pub fn get_color(&self) -> Color {
99 self.paint
100 }
101 pub fn get_width(&self) -> f32 {
103 self.width
104 }
105 pub fn get_pattern(&self) -> LinePattern {
107 self.pattern
108 }
109
110 pub fn color(self, color: Color) -> Result<Self> {
112 Self::new(color, self.width, self.pattern)
113 }
114 pub fn width(self, width: f32) -> Result<Self> {
116 Self::new(self.paint, width, self.pattern)
117 }
118 pub fn pattern(self, pattern: LinePattern) -> Result<Self> {
120 Self::new(self.paint, self.width, pattern)
121 }
122
123 pub(crate) fn backend_style(&self) -> LineStyle {
124 LineStyle {
125 color: self.get_color(),
126 width: self.get_width(),
127 pattern: self.get_pattern(),
128 }
129 }
130}
131
132#[derive(Debug, Clone, PartialEq, Eq)]
134pub struct PointOutlineTheme {
135 pub(crate) color: Color,
136 pub(crate) width_ratio: RelativeSpacing,
137}
138
139impl PointOutlineTheme {
140 pub fn new(color: Color, width_ratio: RelativeSpacing) -> Result<Self> {
142 if width_ratio.is_zero() {
143 return Err(Error::Data(
144 "point outline width ratio must be positive".into(),
145 ));
146 }
147 Ok(Self { color, width_ratio })
148 }
149
150 fn validate(&self) -> Result<()> {
151 if self.width_ratio.is_zero() {
152 Err(Error::Data(
153 "point outline width ratio must be positive".into(),
154 ))
155 } else {
156 Ok(())
157 }
158 }
159
160 pub fn get_color(&self) -> Color {
162 self.color
163 }
164 pub fn get_width_ratio(&self) -> &RelativeSpacing {
166 &self.width_ratio
167 }
168 pub fn color(self, color: Color) -> Result<Self> {
170 Self::new(color, self.width_ratio)
171 }
172 pub fn width_ratio(self, width_ratio: RelativeSpacing) -> Result<Self> {
174 Self::new(self.color, width_ratio)
175 }
176
177 pub(crate) fn publication() -> Result<Self> {
178 Self::new(Color::rgb(255, 255, 255), RelativeSpacing::new(0.1)?)
179 }
180
181 pub(crate) fn backend_style(&self) -> PointOutline {
183 PointOutline {
184 color: self.get_color(),
185 width_ratio: self.get_width_ratio().clone(),
186 }
187 }
188}
189
190#[derive(Debug, Clone, PartialEq)]
192pub struct DataOutlineTheme {
193 choice: DataOutline,
194}
195
196#[derive(Debug, Clone, PartialEq)]
197enum DataOutline {
198 Fixed(OutlineTheme),
199 Mapped { width: f32, alpha: f64 },
200}
201
202impl DataOutlineTheme {
203 pub fn fixed(stroke: OutlineTheme) -> Self {
205 Self {
206 choice: DataOutline::Fixed(stroke),
207 }
208 }
209
210 pub fn mapped(width: f32, alpha: f64) -> Result<Self> {
212 validate_width(width)?;
213 Ok(Self {
214 choice: DataOutline::Mapped {
215 width,
216 alpha: crate::alpha::validated_alpha(alpha)?,
217 },
218 })
219 }
220
221 pub fn fixed_stroke(&self) -> Option<&OutlineTheme> {
223 match &self.choice {
224 DataOutline::Fixed(stroke) => Some(stroke),
225 DataOutline::Mapped { .. } => None,
226 }
227 }
228 pub fn mapped_width(&self) -> Option<f32> {
230 match self.choice {
231 DataOutline::Fixed(_) => None,
232 DataOutline::Mapped { width, .. } => Some(width),
233 }
234 }
235 pub fn mapped_alpha(&self) -> Option<f64> {
237 match self.choice {
238 DataOutline::Fixed(_) => None,
239 DataOutline::Mapped { alpha, .. } => Some(alpha),
240 }
241 }
242
243 fn validate(&self) -> Result<()> {
244 match &self.choice {
245 DataOutline::Fixed(stroke) => stroke.validate(),
246 DataOutline::Mapped { width, .. } => validate_width(*width),
247 }
248 }
249
250 fn operands(&self, mapped: Color) -> (Color, f32) {
251 if let Some(stroke) = self.fixed_stroke() {
252 (stroke.get_color(), stroke.get_width())
253 } else {
254 (
255 mapped.with_alpha(self.mapped_alpha().expect("validated mapped outline")),
256 self.mapped_width().expect("validated mapped outline"),
257 )
258 }
259 }
260}
261
262#[derive(Debug, Clone, PartialEq)]
264pub struct DataFillTheme {
265 pub(crate) fill_alpha: f64,
266 pub(crate) outline: Option<DataOutlineTheme>,
267}
268
269impl DataFillTheme {
270 pub(crate) fn validate(&self) -> Result<()> {
271 if let Some(outline) = self.get_outline() {
272 outline.validate()?;
273 }
274 Ok(())
275 }
276 pub fn new(fill_alpha: f64, outline: Option<DataOutlineTheme>) -> Result<Self> {
278 let value = Self {
279 fill_alpha: crate::alpha::validated_alpha(fill_alpha)?,
280 outline,
281 };
282 value.validate()?;
283 Ok(value)
284 }
285 pub fn get_fill_alpha(&self) -> f64 {
287 self.fill_alpha
288 }
289 pub fn get_outline(&self) -> Option<&DataOutlineTheme> {
291 self.outline.as_ref()
292 }
293 pub fn fill_alpha(self, fill_alpha: f64) -> Result<Self> {
295 Self::new(fill_alpha, self.outline)
296 }
297 pub fn outline(self, outline: Option<DataOutlineTheme>) -> Result<Self> {
299 Self::new(self.fill_alpha, outline)
300 }
301 pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> FillStyle {
302 let color = color_from_rgb(rgb);
303 let (stroke, width) = self
304 .get_outline()
305 .map_or((Color::TRANSPARENT, 0.0), |outline| outline.operands(color));
306 FillStyle::new(color.with_alpha(self.get_fill_alpha()), stroke, width)
307 }
308}
309
310#[derive(Debug, Clone, PartialEq)]
312pub struct FixedFillTheme {
313 pub(crate) paint: Color,
314 pub(crate) outline: Option<OutlineTheme>,
315}
316
317impl FixedFillTheme {
318 pub(crate) fn validate(&self) -> Result<()> {
319 if let Some(outline) = self.get_outline() {
320 outline.validate()?;
321 }
322 Ok(())
323 }
324 pub fn new(color: Color, outline: Option<OutlineTheme>) -> Result<Self> {
326 let value = Self {
327 paint: color,
328 outline,
329 };
330 value.validate()?;
331 Ok(value)
332 }
333 pub fn get_color(&self) -> Color {
335 self.paint
336 }
337 pub fn get_outline(&self) -> Option<&OutlineTheme> {
339 self.outline.as_ref()
340 }
341 pub fn color(self, color: Color) -> Result<Self> {
343 Self::new(color, self.outline)
344 }
345 pub fn outline(self, outline: Option<OutlineTheme>) -> Result<Self> {
347 Self::new(self.paint, outline)
348 }
349 pub(crate) fn backend_style(&self) -> FillStyle {
350 self.get_outline().map_or_else(
351 || FillStyle::new(self.get_color(), Color::TRANSPARENT, 0.0),
352 |outline| outline.backend_style(self.get_color()),
353 )
354 }
355}
356
357impl OutlineTheme {
358 pub(crate) fn backend_style(&self, fill: Color) -> FillStyle {
359 FillStyle::new(fill, self.get_color(), self.get_width())
360 }
361}
362
363#[derive(Debug, Clone, PartialEq)]
365pub struct PointTheme {
366 pub(crate) fill_alpha: f64,
367 pub(crate) shape: PointShape,
368 pub(crate) outline: Option<PointOutlineTheme>,
369}
370
371impl PointTheme {
372 pub(crate) fn validate(&self) -> Result<()> {
373 if let Some(outline) = self.get_outline() {
374 outline.validate()?;
375 }
376 Ok(())
377 }
378 pub fn new(
380 fill_alpha: f64,
381 shape: PointShape,
382 outline: Option<PointOutlineTheme>,
383 ) -> Result<Self> {
384 let value = Self {
385 fill_alpha: crate::alpha::validated_alpha(fill_alpha)?,
386 shape,
387 outline,
388 };
389 value.validate()?;
390 Ok(value)
391 }
392 pub fn get_fill_alpha(&self) -> f64 {
394 self.fill_alpha
395 }
396 pub fn get_shape(&self) -> PointShape {
398 self.shape
399 }
400 pub fn get_outline(&self) -> Option<&PointOutlineTheme> {
402 self.outline.as_ref()
403 }
404 pub fn fill_alpha(self, fill_alpha: f64) -> Result<Self> {
406 Self::new(fill_alpha, self.shape, self.outline)
407 }
408 pub fn shape(self, shape: PointShape) -> Result<Self> {
410 Self::new(self.fill_alpha, shape, self.outline)
411 }
412 pub fn outline(self, outline: Option<PointOutlineTheme>) -> Result<Self> {
414 Self::new(self.fill_alpha, self.shape, outline)
415 }
416 pub(crate) fn backend_style(
417 &self,
418 rgb: (u8, u8, u8),
419 radius: u32,
420 outline_visible: bool,
421 ) -> PointStyle {
422 PointStyle {
423 color: color_from_rgb(rgb).with_alpha(self.get_fill_alpha()),
424 radius,
425 shape: self.get_shape(),
426 outline: self
427 .get_outline()
428 .filter(|_| outline_visible)
429 .map(PointOutlineTheme::backend_style),
430 }
431 }
432}
433
434#[derive(Debug, Clone, PartialEq)]
436pub struct DataLineTheme {
437 pub(crate) width: f32,
438 pub(crate) alpha: f64,
439 pub(crate) pattern: LinePattern,
440}
441
442impl DataLineTheme {
443 pub(crate) fn validate(&self) -> Result<()> {
444 validate_width(self.get_width())
445 }
446 pub fn new(width: f32, alpha: f64, pattern: LinePattern) -> Result<Self> {
448 let value = Self {
449 width,
450 alpha: crate::alpha::validated_alpha(alpha)?,
451 pattern,
452 };
453 value.validate()?;
454 Ok(value)
455 }
456 pub fn get_width(&self) -> f32 {
458 self.width
459 }
460 pub fn get_alpha(&self) -> f64 {
462 self.alpha
463 }
464 pub fn get_pattern(&self) -> LinePattern {
466 self.pattern
467 }
468 pub fn width(self, width: f32) -> Result<Self> {
470 Self::new(width, self.alpha, self.pattern)
471 }
472 pub fn alpha(self, alpha: f64) -> Result<Self> {
474 Self::new(self.width, alpha, self.pattern)
475 }
476 pub fn pattern(self, pattern: LinePattern) -> Result<Self> {
478 Self::new(self.width, self.alpha, pattern)
479 }
480 pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> LineStyle {
481 LineStyle {
482 color: color_from_rgb(rgb).with_alpha(self.get_alpha()),
483 width: self.get_width(),
484 pattern: self.get_pattern(),
485 }
486 }
487}