astrelis-geometry 0.2.4

Customizable 2D geometry rendering for Astrelis Game Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Chart styling types.
//!
//! Provides comprehensive styling options for chart elements:
//! - Line configuration with dash patterns and caps/joins
//! - Marker configuration with various shapes
//! - Fill configuration with gradients and targets
//! - Axis styling

use super::grid::DashPattern;
use super::types::SeriesId;
use astrelis_render::Color;

/// Line cap style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineCap {
    /// Flat cap (line ends at endpoint).
    #[default]
    Butt,
    /// Rounded cap (semicircle at endpoint).
    Round,
    /// Square cap (extends beyond endpoint by half line width).
    Square,
}

/// Line join style for connecting line segments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineJoin {
    /// Sharp corner (miter).
    #[default]
    Miter,
    /// Rounded corner.
    Round,
    /// Beveled corner.
    Bevel,
}

/// Line style for series (legacy enum, kept for backward compatibility).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineStyle {
    /// Solid line
    #[default]
    Solid,
    /// Dashed line
    Dashed,
    /// Dotted line
    Dotted,
}

impl LineStyle {
    /// Convert to a DashPattern.
    pub fn to_dash_pattern(&self) -> DashPattern {
        match self {
            Self::Solid => DashPattern::SOLID,
            Self::Dashed => DashPattern::medium_dash(),
            Self::Dotted => DashPattern::dotted(2.0),
        }
    }
}

/// Enhanced line configuration.
#[derive(Debug, Clone, PartialEq)]
pub struct LineConfig {
    /// Line color.
    pub color: Color,
    /// Line thickness in pixels.
    pub thickness: f32,
    /// Dash pattern.
    pub dash: DashPattern,
    /// Line cap style.
    pub cap: LineCap,
    /// Line join style.
    pub join: LineJoin,
}

impl Default for LineConfig {
    fn default() -> Self {
        Self {
            color: Color::BLUE,
            thickness: 1.5,
            dash: DashPattern::SOLID,
            cap: LineCap::Butt,
            join: LineJoin::Miter,
        }
    }
}

impl LineConfig {
    /// Create a line config with the specified color.
    pub fn with_color(color: Color) -> Self {
        Self {
            color,
            ..Default::default()
        }
    }

    /// Set the line thickness.
    pub fn thickness(mut self, thickness: f32) -> Self {
        self.thickness = thickness;
        self
    }

    /// Set the dash pattern.
    pub fn dash(mut self, dash: DashPattern) -> Self {
        self.dash = dash;
        self
    }

    /// Make this a dashed line.
    pub fn dashed(mut self, dash_len: f32, gap_len: f32) -> Self {
        self.dash = DashPattern::dashed(dash_len, gap_len);
        self
    }

    /// Make this a dotted line.
    pub fn dotted(mut self, dot_size: f32) -> Self {
        self.dash = DashPattern::dotted(dot_size);
        self
    }

    /// Set the line cap.
    pub fn cap(mut self, cap: LineCap) -> Self {
        self.cap = cap;
        self
    }

    /// Set the line join.
    pub fn join(mut self, join: LineJoin) -> Self {
        self.join = join;
        self
    }
}

/// Marker shape for data points.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MarkerShape {
    /// Circle
    #[default]
    Circle,
    /// Square
    Square,
    /// Triangle pointing up
    Triangle,
    /// Triangle pointing down
    TriangleDown,
    /// Diamond (rotated square)
    Diamond,
    /// Cross (+)
    Cross,
    /// X shape
    X,
    /// Star
    Star,
    /// No marker (invisible)
    None,
}

/// Point shape (alias for backward compatibility).
pub type PointShape = MarkerShape;

/// Enhanced marker configuration.
#[derive(Debug, Clone, PartialEq)]
pub struct MarkerConfig {
    /// Marker shape.
    pub shape: MarkerShape,
    /// Marker size in pixels.
    pub size: f32,
    /// Fill color (None = no fill).
    pub fill: Option<Color>,
    /// Stroke color (None = no stroke).
    pub stroke: Option<Color>,
    /// Stroke thickness.
    pub stroke_thickness: f32,
    /// Show marker every Nth point (1 = all points).
    pub interval: usize,
    /// Only show markers on hover.
    pub hover_only: bool,
}

impl Default for MarkerConfig {
    fn default() -> Self {
        Self {
            shape: MarkerShape::Circle,
            size: 6.0,
            fill: Some(Color::WHITE),
            stroke: None,
            stroke_thickness: 1.0,
            interval: 1,
            hover_only: false,
        }
    }
}

impl MarkerConfig {
    /// Create a new marker config with the specified shape.
    pub fn new(shape: MarkerShape) -> Self {
        Self {
            shape,
            ..Default::default()
        }
    }

    /// Create a circle marker.
    pub fn circle() -> Self {
        Self::new(MarkerShape::Circle)
    }

    /// Create a square marker.
    pub fn square() -> Self {
        Self::new(MarkerShape::Square)
    }

    /// Create a diamond marker.
    pub fn diamond() -> Self {
        Self::new(MarkerShape::Diamond)
    }

    /// Set the marker size.
    pub fn size(mut self, size: f32) -> Self {
        self.size = size;
        self
    }

    /// Set the fill color.
    pub fn fill(mut self, color: Color) -> Self {
        self.fill = Some(color);
        self
    }

    /// Set no fill (outline only).
    pub fn no_fill(mut self) -> Self {
        self.fill = None;
        self
    }

    /// Set the stroke color.
    pub fn stroke(mut self, color: Color) -> Self {
        self.stroke = Some(color);
        self
    }

    /// Set the stroke thickness.
    pub fn stroke_thickness(mut self, thickness: f32) -> Self {
        self.stroke_thickness = thickness;
        self
    }

    /// Show markers at intervals (every Nth point).
    pub fn every(mut self, n: usize) -> Self {
        self.interval = n.max(1);
        self
    }

    /// Only show markers on hover.
    pub fn on_hover_only(mut self) -> Self {
        self.hover_only = true;
        self
    }
}

/// Fill target for area fills.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum FillTarget {
    /// Fill to a constant Y value.
    ToValue(f64),
    /// Fill to the X axis baseline (Y = 0 or axis minimum).
    #[default]
    ToBaseline,
    /// Fill to another series.
    ToSeries { series_id: SeriesId },
    /// Fill a band between two series.
    Band { lower: SeriesId, upper: SeriesId },
}

/// Gradient definition.
#[derive(Debug, Clone, PartialEq)]
pub struct Gradient {
    /// Gradient stops (position 0.0-1.0, color).
    pub stops: Vec<(f32, Color)>,
    /// Whether the gradient is vertical (true) or horizontal (false).
    pub vertical: bool,
}

impl Default for Gradient {
    fn default() -> Self {
        Self {
            stops: vec![(0.0, Color::WHITE), (1.0, Color::BLACK)],
            vertical: true,
        }
    }
}

impl Gradient {
    /// Create a two-color vertical gradient.
    pub fn vertical(top: Color, bottom: Color) -> Self {
        Self {
            stops: vec![(0.0, top), (1.0, bottom)],
            vertical: true,
        }
    }

    /// Create a two-color horizontal gradient.
    pub fn horizontal(left: Color, right: Color) -> Self {
        Self {
            stops: vec![(0.0, left), (1.0, right)],
            vertical: false,
        }
    }

    /// Add a gradient stop.
    pub fn with_stop(mut self, position: f32, color: Color) -> Self {
        self.stops.push((position.clamp(0.0, 1.0), color));
        self.stops.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
        self
    }

    /// Get the color at a position (0.0-1.0).
    pub fn color_at(&self, position: f32) -> Color {
        if self.stops.is_empty() {
            return Color::WHITE;
        }
        if self.stops.len() == 1 {
            return self.stops[0].1;
        }

        let pos = position.clamp(0.0, 1.0);

        // Find the two stops to interpolate between
        let mut prev = &self.stops[0];
        for stop in &self.stops {
            if stop.0 >= pos {
                if (stop.0 - prev.0).abs() < f32::EPSILON {
                    return stop.1;
                }
                let t = (pos - prev.0) / (stop.0 - prev.0);
                return Color::rgba(
                    prev.1.r + (stop.1.r - prev.1.r) * t,
                    prev.1.g + (stop.1.g - prev.1.g) * t,
                    prev.1.b + (stop.1.b - prev.1.b) * t,
                    prev.1.a + (stop.1.a - prev.1.a) * t,
                );
            }
            prev = stop;
        }

        self.stops.last().unwrap().1
    }
}

/// Enhanced fill configuration.
#[derive(Debug, Clone, PartialEq)]
pub struct FillConfig {
    /// Fill target.
    pub target: FillTarget,
    /// Solid fill color.
    pub color: Color,
    /// Optional gradient (overrides solid color if present).
    pub gradient: Option<Gradient>,
}

impl Default for FillConfig {
    fn default() -> Self {
        Self {
            target: FillTarget::ToBaseline,
            color: Color::rgba(0.0, 0.5, 1.0, 0.3),
            gradient: None,
        }
    }
}

impl FillConfig {
    /// Create a fill to baseline with the specified color.
    pub fn to_baseline(color: Color) -> Self {
        Self {
            target: FillTarget::ToBaseline,
            color,
            gradient: None,
        }
    }

    /// Create a fill to a constant value.
    pub fn to_value(value: f64, color: Color) -> Self {
        Self {
            target: FillTarget::ToValue(value),
            color,
            gradient: None,
        }
    }

    /// Create a fill to another series.
    pub fn to_series(series_id: SeriesId, color: Color) -> Self {
        Self {
            target: FillTarget::ToSeries { series_id },
            color,
            gradient: None,
        }
    }

    /// Set a gradient fill.
    pub fn with_gradient(mut self, gradient: Gradient) -> Self {
        self.gradient = Some(gradient);
        self
    }

    /// Get the effective fill color at a position (considering gradient).
    pub fn color_at(&self, position: f32) -> Color {
        if let Some(gradient) = &self.gradient {
            gradient.color_at(position)
        } else {
            self.color
        }
    }
}

/// Point style for scatter/line charts (legacy, kept for compatibility).
#[derive(Debug, Clone, Copy)]
pub struct PointStyle {
    /// Point size
    pub size: f32,
    /// Point shape
    pub shape: PointShape,
    /// Fill color
    pub color: Color,
}

impl Default for PointStyle {
    fn default() -> Self {
        Self {
            size: 6.0,
            shape: PointShape::Circle,
            color: Color::WHITE,
        }
    }
}

/// Fill style for area charts (legacy, kept for compatibility).
#[derive(Debug, Clone, Copy)]
pub struct FillStyle {
    /// Fill color
    pub color: Color,
    /// Opacity (0.0 to 1.0)
    pub opacity: f32,
}

impl Default for FillStyle {
    fn default() -> Self {
        Self {
            color: Color::BLUE,
            opacity: 0.3,
        }
    }
}

/// Series visual style.
#[derive(Debug, Clone)]
pub struct SeriesStyle {
    /// Line color
    pub color: Color,
    /// Line width
    pub line_width: f32,
    /// Line style (legacy)
    pub line_style: LineStyle,
    /// Point style (None = no points) - legacy
    pub point_style: Option<PointStyle>,
    /// Fill style (for area charts) - legacy
    pub fill: Option<FillStyle>,
    /// Z-order for rendering (higher = on top)
    pub z_order: i32,
    /// Whether this series is visible
    pub visible: bool,
    /// Whether to show in legend
    pub show_in_legend: bool,
}

impl Default for SeriesStyle {
    fn default() -> Self {
        Self {
            color: Color::BLUE,
            line_width: 1.0, // Thinner lines for better visibility with dense data
            line_style: LineStyle::Solid,
            point_style: None,
            fill: None,
            z_order: 0,
            visible: true,
            show_in_legend: true,
        }
    }
}

impl SeriesStyle {
    /// Create a style with a specific color.
    pub fn with_color(color: Color) -> Self {
        Self {
            color,
            ..Default::default()
        }
    }

    /// Set line width.
    pub fn line_width(mut self, width: f32) -> Self {
        self.line_width = width;
        self
    }

    /// Set line style.
    pub fn line_style(mut self, style: LineStyle) -> Self {
        self.line_style = style;
        self
    }

    /// Add points.
    pub fn with_points(mut self) -> Self {
        self.point_style = Some(PointStyle {
            color: self.color,
            ..Default::default()
        });
        self
    }

    /// Add points with custom style.
    pub fn with_point_style(mut self, style: PointStyle) -> Self {
        self.point_style = Some(style);
        self
    }

    /// Add area fill.
    pub fn with_fill(mut self) -> Self {
        self.fill = Some(FillStyle {
            color: self.color,
            opacity: 0.3,
        });
        self
    }

    /// Add area fill with custom style.
    pub fn with_fill_style(mut self, style: FillStyle) -> Self {
        self.fill = Some(style);
        self
    }

    /// Set z-order (higher = rendered on top).
    pub fn z_order(mut self, z_order: i32) -> Self {
        self.z_order = z_order;
        self
    }

    /// Set visibility.
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Hide from legend.
    pub fn hide_from_legend(mut self) -> Self {
        self.show_in_legend = false;
        self
    }

    /// Make this a dashed line.
    pub fn dashed(mut self) -> Self {
        self.line_style = LineStyle::Dashed;
        self
    }

    /// Make this a dotted line.
    pub fn dotted(mut self) -> Self {
        self.line_style = LineStyle::Dotted;
        self
    }

    /// Get the line configuration.
    pub fn to_line_config(&self) -> LineConfig {
        LineConfig {
            color: self.color,
            thickness: self.line_width,
            dash: self.line_style.to_dash_pattern(),
            cap: LineCap::default(),
            join: LineJoin::default(),
        }
    }

    /// Get the marker configuration.
    pub fn to_marker_config(&self) -> Option<MarkerConfig> {
        self.point_style.as_ref().map(|ps| MarkerConfig {
            shape: ps.shape,
            size: ps.size,
            fill: Some(ps.color),
            stroke: None,
            stroke_thickness: 1.0,
            interval: 1,
            hover_only: false,
        })
    }

    /// Get the fill configuration.
    pub fn to_fill_config(&self) -> Option<FillConfig> {
        self.fill.as_ref().map(|fs| FillConfig {
            target: FillTarget::ToBaseline,
            color: Color::rgba(fs.color.r, fs.color.g, fs.color.b, fs.opacity),
            gradient: None,
        })
    }
}

/// Enhanced series style with full configuration.
#[derive(Debug, Clone)]
pub struct EnhancedSeriesStyle {
    /// Line configuration.
    pub line: LineConfig,
    /// Marker configuration (None = no markers).
    pub markers: Option<MarkerConfig>,
    /// Fill configuration (None = no fill).
    pub fill: Option<FillConfig>,
    /// Z-order for rendering.
    pub z_order: i32,
    /// Whether this series is visible.
    pub visible: bool,
    /// Whether to show in legend.
    pub show_in_legend: bool,
}

impl Default for EnhancedSeriesStyle {
    fn default() -> Self {
        Self {
            line: LineConfig::default(),
            markers: None,
            fill: None,
            z_order: 0,
            visible: true,
            show_in_legend: true,
        }
    }
}

impl EnhancedSeriesStyle {
    /// Create a style with the specified line color.
    pub fn with_color(color: Color) -> Self {
        Self {
            line: LineConfig::with_color(color),
            ..Default::default()
        }
    }

    /// Set the line configuration.
    pub fn line(mut self, line: LineConfig) -> Self {
        self.line = line;
        self
    }

    /// Set the marker configuration.
    pub fn markers(mut self, markers: MarkerConfig) -> Self {
        self.markers = Some(markers);
        self
    }

    /// Set the fill configuration.
    pub fn fill(mut self, fill: FillConfig) -> Self {
        self.fill = Some(fill);
        self
    }

    /// Set z-order.
    pub fn z_order(mut self, z_order: i32) -> Self {
        self.z_order = z_order;
        self
    }

    /// Set visibility.
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Hide from legend.
    pub fn hide_from_legend(mut self) -> Self {
        self.show_in_legend = false;
        self
    }

    /// Convert to legacy SeriesStyle.
    pub fn to_legacy(&self) -> SeriesStyle {
        SeriesStyle {
            color: self.line.color,
            line_width: self.line.thickness,
            line_style: if self.line.dash.is_solid() {
                LineStyle::Solid
            } else if self.line.dash.segments.len() == 2
                && self.line.dash.segments[0] == self.line.dash.segments[1]
            {
                LineStyle::Dotted
            } else {
                LineStyle::Dashed
            },
            point_style: self.markers.as_ref().map(|m| PointStyle {
                size: m.size,
                shape: m.shape,
                color: m.fill.unwrap_or(self.line.color),
            }),
            fill: self.fill.as_ref().map(|f| FillStyle {
                color: f.color,
                opacity: f.color.a,
            }),
            z_order: self.z_order,
            visible: self.visible,
            show_in_legend: self.show_in_legend,
        }
    }
}

/// Axis visual style.
#[derive(Debug, Clone)]
pub struct AxisStyle {
    /// Axis line color
    pub line_color: Color,
    /// Axis line width
    pub line_width: f32,
    /// Tick color
    pub tick_color: Color,
    /// Tick length
    pub tick_length: f32,
    /// Grid line color
    pub grid_color: Color,
    /// Grid line width
    pub grid_width: f32,
    /// Label color
    pub label_color: Color,
    /// Label font size
    pub label_size: f32,
}

impl Default for AxisStyle {
    fn default() -> Self {
        Self {
            line_color: Color::rgba(0.4, 0.4, 0.45, 1.0),
            line_width: 1.0,
            tick_color: Color::rgba(0.4, 0.4, 0.45, 1.0),
            tick_length: 4.0,
            grid_color: Color::rgba(0.25, 0.25, 0.28, 1.0),
            grid_width: 0.5,
            label_color: Color::rgba(0.6, 0.6, 0.65, 1.0),
            label_size: 11.0,
        }
    }
}

/// Modern, minimal color palette for series.
/// Inspired by contemporary data visualization tools.
pub const SERIES_COLORS: [Color; 8] = [
    Color::rgba(0.36, 0.67, 0.93, 1.0), // Soft blue
    Color::rgba(0.95, 0.55, 0.38, 1.0), // Coral
    Color::rgba(0.45, 0.80, 0.69, 1.0), // Mint/teal
    Color::rgba(0.91, 0.70, 0.41, 1.0), // Warm gold
    Color::rgba(0.70, 0.55, 0.85, 1.0), // Soft purple
    Color::rgba(0.95, 0.60, 0.60, 1.0), // Soft red/pink
    Color::rgba(0.55, 0.75, 0.50, 1.0), // Sage green
    Color::rgba(0.60, 0.60, 0.65, 1.0), // Neutral gray
];

/// Get a color from the palette by index.
pub fn palette_color(index: usize) -> Color {
    SERIES_COLORS[index % SERIES_COLORS.len()]
}