embedded-charts 0.3.0

A rich graph framework for embedded systems using embedded-graphics with std/no_std support
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
//! Gradient fills and advanced styling for no_std environments
//!
//! This module provides gradient rendering capabilities that work efficiently
//! on embedded systems without heap allocation.

use crate::error::ChartError;
use embedded_graphics::prelude::*;
use heapless::Vec;

/// Maximum number of gradient stops supported
pub const MAX_GRADIENT_STOPS: usize = 8;

/// A color stop in a gradient
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GradientStop<C: PixelColor> {
    /// Position along the gradient (0.0 to 1.0)
    pub position: f32,
    /// Color at this position
    pub color: C,
}

impl<C: PixelColor> GradientStop<C> {
    /// Create a new gradient stop
    pub const fn new(position: f32, color: C) -> Self {
        Self { position, color }
    }
}

/// Direction of a linear gradient
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GradientDirection {
    /// Horizontal gradient (left to right)
    Horizontal,
    /// Vertical gradient (top to bottom)
    Vertical,
    /// Diagonal gradient (top-left to bottom-right)
    Diagonal,
    /// Reverse diagonal gradient (top-right to bottom-left)
    ReverseDiagonal,
}

/// Linear gradient definition
#[derive(Debug, Clone)]
pub struct LinearGradient<C: PixelColor, const N: usize = MAX_GRADIENT_STOPS> {
    /// Gradient stops (must have at least 2)
    stops: Vec<GradientStop<C>, N>,
    /// Direction of the gradient
    direction: GradientDirection,
}

impl<C: PixelColor, const N: usize> LinearGradient<C, N> {
    /// Create a new linear gradient
    pub fn new(direction: GradientDirection) -> Self {
        Self {
            stops: Vec::new(),
            direction,
        }
    }

    /// Create a simple two-color gradient
    pub fn simple(start: C, end: C, direction: GradientDirection) -> Result<Self, ChartError> {
        let mut gradient = Self::new(direction);
        gradient.add_stop(0.0, start)?;
        gradient.add_stop(1.0, end)?;
        Ok(gradient)
    }

    /// Add a color stop to the gradient
    pub fn add_stop(&mut self, position: f32, color: C) -> Result<(), ChartError> {
        if !(0.0..=1.0).contains(&position) {
            return Err(ChartError::InvalidConfiguration);
        }

        let stop = GradientStop::new(position, color);

        // Insert in sorted order by position
        let insert_pos = self
            .stops
            .iter()
            .position(|s| s.position > position)
            .unwrap_or(self.stops.len());

        self.stops
            .insert(insert_pos, stop)
            .map_err(|_| ChartError::MemoryFull)?;

        Ok(())
    }

    /// Get the color at a specific position (0.0 to 1.0)
    pub fn color_at(&self, position: f32) -> Option<C> {
        if self.stops.len() < 2 {
            return None;
        }

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

        // Find the two stops to interpolate between
        let mut lower_stop = &self.stops[0];
        let mut upper_stop = &self.stops[self.stops.len() - 1];

        for i in 0..self.stops.len() - 1 {
            if position >= self.stops[i].position && position <= self.stops[i + 1].position {
                lower_stop = &self.stops[i];
                upper_stop = &self.stops[i + 1];
                break;
            }
        }

        if lower_stop.position == upper_stop.position {
            Some(lower_stop.color)
        } else {
            // Simple linear interpolation for now
            // More sophisticated color interpolation requires the color-support feature
            let t = (position - lower_stop.position) / (upper_stop.position - lower_stop.position);
            if t < 0.5 {
                Some(lower_stop.color)
            } else {
                Some(upper_stop.color)
            }
        }
    }

    /// Get the gradient direction
    pub fn direction(&self) -> GradientDirection {
        self.direction
    }

    /// Get the number of stops
    pub fn stop_count(&self) -> usize {
        self.stops.len()
    }

    /// Check if the gradient is valid (has at least 2 stops)
    pub fn is_valid(&self) -> bool {
        self.stops.len() >= 2
    }
}

/// Extension trait for color interpolation with gradients
#[cfg(feature = "color-support")]
pub trait GradientInterpolation<C: PixelColor> {
    /// Get interpolated color at position
    fn interpolated_color_at(&self, position: f32) -> Option<C>;
}

#[cfg(feature = "color-support")]
impl<const N: usize> GradientInterpolation<embedded_graphics::pixelcolor::Rgb565>
    for LinearGradient<embedded_graphics::pixelcolor::Rgb565, N>
{
    fn interpolated_color_at(
        &self,
        position: f32,
    ) -> Option<embedded_graphics::pixelcolor::Rgb565> {
        use crate::style::ColorInterpolation;
        use embedded_graphics::pixelcolor::Rgb565;

        if self.stops.len() < 2 {
            return None;
        }

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

        // Find the two stops to interpolate between
        let mut lower_stop = &self.stops[0];
        let mut upper_stop = &self.stops[self.stops.len() - 1];

        for i in 0..self.stops.len() - 1 {
            if position >= self.stops[i].position && position <= self.stops[i + 1].position {
                lower_stop = &self.stops[i];
                upper_stop = &self.stops[i + 1];
                break;
            }
        }

        if lower_stop.position == upper_stop.position {
            Some(lower_stop.color)
        } else {
            let t = (position - lower_stop.position) / (upper_stop.position - lower_stop.position);
            Some(Rgb565::interpolate(lower_stop.color, upper_stop.color, t))
        }
    }
}

/// Extension trait for radial gradient interpolation
#[cfg(feature = "color-support")]
pub trait RadialGradientInterpolation<C: PixelColor> {
    /// Get interpolated color at distance
    fn interpolated_color_at_distance(&self, distance: f32) -> Option<C>;
}

#[cfg(feature = "color-support")]
impl<const N: usize> RadialGradientInterpolation<embedded_graphics::pixelcolor::Rgb565>
    for RadialGradient<embedded_graphics::pixelcolor::Rgb565, N>
{
    fn interpolated_color_at_distance(
        &self,
        distance: f32,
    ) -> Option<embedded_graphics::pixelcolor::Rgb565> {
        use crate::style::ColorInterpolation;
        use embedded_graphics::pixelcolor::Rgb565;

        if self.stops.len() < 2 {
            return None;
        }

        let distance = distance.clamp(0.0, 1.0);

        // Find stops to interpolate between
        let mut lower_stop = &self.stops[0];
        let mut upper_stop = &self.stops[self.stops.len() - 1];

        for i in 0..self.stops.len() - 1 {
            if distance >= self.stops[i].position && distance <= self.stops[i + 1].position {
                lower_stop = &self.stops[i];
                upper_stop = &self.stops[i + 1];
                break;
            }
        }

        if lower_stop.position == upper_stop.position {
            Some(lower_stop.color)
        } else {
            let t = (distance - lower_stop.position) / (upper_stop.position - lower_stop.position);
            Some(Rgb565::interpolate(lower_stop.color, upper_stop.color, t))
        }
    }
}

/// Radial gradient definition
#[derive(Debug, Clone)]
pub struct RadialGradient<C: PixelColor, const N: usize = MAX_GRADIENT_STOPS> {
    /// Center point of the gradient (relative to bounds, 0.0 to 1.0)
    center: Point,
    /// Gradient stops
    stops: Vec<GradientStop<C>, N>,
}

impl<C: PixelColor, const N: usize> RadialGradient<C, N> {
    /// Create a new radial gradient
    pub fn new(center: Point) -> Self {
        Self {
            center,
            stops: Vec::new(),
        }
    }

    /// Create a simple two-color radial gradient
    pub fn simple(inner: C, outer: C, center: Point) -> Result<Self, ChartError> {
        let mut gradient = Self::new(center);
        gradient.add_stop(0.0, inner)?;
        gradient.add_stop(1.0, outer)?;
        Ok(gradient)
    }

    /// Add a color stop
    pub fn add_stop(&mut self, position: f32, color: C) -> Result<(), ChartError> {
        if !(0.0..=1.0).contains(&position) {
            return Err(ChartError::InvalidConfiguration);
        }

        let stop = GradientStop::new(position, color);

        // Insert in sorted order
        let insert_pos = self
            .stops
            .iter()
            .position(|s| s.position > position)
            .unwrap_or(self.stops.len());

        self.stops
            .insert(insert_pos, stop)
            .map_err(|_| ChartError::MemoryFull)?;

        Ok(())
    }

    /// Get color at a specific distance from center (0.0 to 1.0)
    pub fn color_at_distance(&self, distance: f32) -> Option<C> {
        if self.stops.len() < 2 {
            return None;
        }

        let distance = distance.clamp(0.0, 1.0);

        // Find stops to interpolate between
        let mut lower_stop = &self.stops[0];
        let mut upper_stop = &self.stops[self.stops.len() - 1];

        for i in 0..self.stops.len() - 1 {
            if distance >= self.stops[i].position && distance <= self.stops[i + 1].position {
                lower_stop = &self.stops[i];
                upper_stop = &self.stops[i + 1];
                break;
            }
        }

        #[cfg(feature = "color-support")]
        {
            if lower_stop.position == upper_stop.position {
                Some(lower_stop.color)
            } else {
                // Simple nearest-neighbor interpolation for generic colors
                let t =
                    (distance - lower_stop.position) / (upper_stop.position - lower_stop.position);
                if t < 0.5 {
                    Some(lower_stop.color)
                } else {
                    Some(upper_stop.color)
                }
            }
        }

        #[cfg(not(feature = "color-support"))]
        {
            let mid = (lower_stop.position + upper_stop.position) / 2.0;
            if distance <= mid {
                Some(lower_stop.color)
            } else {
                Some(upper_stop.color)
            }
        }
    }

    /// Get the center point
    pub fn center(&self) -> Point {
        self.center
    }

    /// Check if the gradient is valid
    pub fn is_valid(&self) -> bool {
        self.stops.len() >= 2
    }
}

/// Pattern fill types for advanced styling
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatternType {
    /// Horizontal lines
    HorizontalLines {
        /// Spacing between lines in pixels
        spacing: u32,
        /// Width of each line in pixels
        width: u32,
    },
    /// Vertical lines
    VerticalLines {
        /// Spacing between lines in pixels
        spacing: u32,
        /// Width of each line in pixels
        width: u32,
    },
    /// Diagonal lines
    DiagonalLines {
        /// Spacing between lines in pixels
        spacing: u32,
        /// Width of each line in pixels
        width: u32,
    },
    /// Dots
    Dots {
        /// Spacing between dot centers in pixels
        spacing: u32,
        /// Radius of each dot in pixels
        radius: u32,
    },
    /// Checkerboard
    Checkerboard {
        /// Size of each square in pixels
        size: u32,
    },
    /// Cross hatch
    CrossHatch {
        /// Spacing between lines in pixels
        spacing: u32,
        /// Width of each line in pixels
        width: u32,
    },
}

/// Pattern fill definition
#[derive(Debug, Clone, Copy)]
pub struct PatternFill<C: PixelColor> {
    /// Foreground color (pattern color)
    pub foreground: C,
    /// Background color
    pub background: C,
    /// Pattern type
    pub pattern: PatternType,
}

impl<C: PixelColor> PatternFill<C> {
    /// Create a new pattern fill
    pub const fn new(foreground: C, background: C, pattern: PatternType) -> Self {
        Self {
            foreground,
            background,
            pattern,
        }
    }

    /// Check if a pixel at the given position should use foreground color
    pub fn is_foreground(&self, x: i32, y: i32) -> bool {
        match self.pattern {
            PatternType::HorizontalLines { spacing, width } => (y as u32 % spacing) < width,
            PatternType::VerticalLines { spacing, width } => (x as u32 % spacing) < width,
            PatternType::DiagonalLines { spacing, width } => ((x + y) as u32 % spacing) < width,
            PatternType::Dots { spacing, radius } => {
                let px = x as u32 % spacing;
                let py = y as u32 % spacing;
                let center = spacing / 2;
                let dx = px.abs_diff(center);
                let dy = py.abs_diff(center);
                (dx * dx + dy * dy) <= (radius * radius)
            }
            PatternType::Checkerboard { size } => ((x as u32 / size) + (y as u32 / size)) % 2 == 0,
            PatternType::CrossHatch { spacing, width } => {
                let h = (y as u32 % spacing) < width;
                let v = (x as u32 % spacing) < width;
                h || v
            }
        }
    }

    /// Get the color at a specific position
    pub fn color_at(&self, x: i32, y: i32) -> C {
        if self.is_foreground(x, y) {
            self.foreground
        } else {
            self.background
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use embedded_graphics::pixelcolor::Rgb565;

    #[test]
    fn test_linear_gradient_simple() {
        let gradient: LinearGradient<Rgb565, 8> =
            LinearGradient::simple(Rgb565::RED, Rgb565::BLUE, GradientDirection::Horizontal)
                .unwrap();

        assert!(gradient.is_valid());
        assert_eq!(gradient.stop_count(), 2);
    }

    #[test]
    fn test_gradient_color_at() {
        let mut gradient: LinearGradient<Rgb565, 4> =
            LinearGradient::new(GradientDirection::Horizontal);
        gradient.add_stop(0.0, Rgb565::RED).unwrap();
        gradient.add_stop(1.0, Rgb565::BLUE).unwrap();

        // Test edge colors
        assert_eq!(gradient.color_at(0.0), Some(Rgb565::RED));
        assert_eq!(gradient.color_at(1.0), Some(Rgb565::BLUE));
    }

    #[test]
    fn test_pattern_fill() {
        let pattern = PatternFill::new(
            Rgb565::BLACK,
            Rgb565::WHITE,
            PatternType::Checkerboard { size: 10 },
        );

        assert_eq!(pattern.color_at(0, 0), Rgb565::BLACK);
        assert_eq!(pattern.color_at(10, 0), Rgb565::WHITE);
        assert_eq!(pattern.color_at(0, 10), Rgb565::WHITE);
        assert_eq!(pattern.color_at(10, 10), Rgb565::BLACK);
    }
}