crisol-plot 1.15.0

Crisol plot: interactive 2D charts and a 3D sibling with SVG, PNG, and PDF export.
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
//! Hover animations for legend rows and the direction arrow of streaming series.
//!
//! Every effect is a pure function of the time since the pointer entered a legend row, so
//! the screen and an export (which never animates) share the same geometry. Reduced
//! motion turns every effect off. Effects are selected per plot in [`PlotConfig`], and
//! each chart family interprets them where it makes sense (line motion, dash motion, pie
//! slices, radar vertices, gauge needles).
//!
//! [`PlotConfig`]: super::PlotConfig

use egui::Color32;

use super::PlotConfig;
use super::{PlotState, scene::Dash};

/// The animation played while the pointer rests on a legend row.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum LegendAnimation {
    /// Smoothly blends the series color into the highlight color and back.
    #[default]
    Blink,
    /// Pulses the series opacity.
    Fade,
    /// Pulses the stroke width.
    Throb,
    /// Scrolls the dashes along the line (marching ants).
    DashMarch,
    /// Grows the dashes in and out of a solid line.
    DashToggle,
    /// Draws a soft halo around the line.
    Glow,
    /// A bright segment travels along the line.
    Sweep,
    /// The line wiggles perpendicular to its direction.
    Wiggle,
    /// The line draws itself, repeating.
    Reveal,
    /// Markers bounce up and down.
    MarkerBounce,
    /// Markers grow and shrink.
    MarkerPulse,
    /// Small sparkles appear along the line.
    Twinkle,
    /// The line's color washes from left to right.
    Wave,
}

impl LegendAnimation {
    /// Every animation, in menu order.
    pub const ALL: [Self; 13] = [
        Self::Blink,
        Self::Fade,
        Self::Throb,
        Self::DashMarch,
        Self::DashToggle,
        Self::Glow,
        Self::Sweep,
        Self::Wiggle,
        Self::Reveal,
        Self::MarkerBounce,
        Self::MarkerPulse,
        Self::Twinkle,
        Self::Wave,
    ];

    /// A short name for settings menus.
    pub const fn label(self) -> &'static str {
        match self {
            Self::Blink => "Blink",
            Self::Fade => "Fade",
            Self::Throb => "Throb",
            Self::DashMarch => "Marching dashes",
            Self::DashToggle => "Dash toggle",
            Self::Glow => "Glow",
            Self::Sweep => "Sweep",
            Self::Wiggle => "Wiggle",
            Self::Reveal => "Reveal",
            Self::MarkerBounce => "Marker bounce",
            Self::MarkerPulse => "Marker pulse",
            Self::Twinkle => "Twinkle",
            Self::Wave => "Color wave",
        }
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
/// How the animation's clock is shaped within one cycle.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum AnimationEasing {
    /// Constant speed.
    #[default]
    Linear,
    /// Starts slow.
    EaseIn,
    /// Ends slow.
    EaseOut,
    /// Slow at both ends.
    EaseInOut,
    /// Holds, then jumps at the midpoint.
    Step,
    /// A smooth sine oscillator: back and forth with no abrupt turn.
    Sine,
}

impl AnimationEasing {
    /// Every easing, in menu order.
    pub const ALL: [Self; 6] = [
        Self::Linear,
        Self::EaseIn,
        Self::EaseOut,
        Self::EaseInOut,
        Self::Step,
        Self::Sine,
    ];

    /// A short name for settings menus.
    pub const fn label(self) -> &'static str {
        match self {
            Self::Linear => "Linear",
            Self::EaseIn => "Ease in",
            Self::EaseOut => "Ease out",
            Self::EaseInOut => "Ease in-out",
            Self::Step => "Step",
            Self::Sine => "Sine",
        }
    }

    /// Shape a cycle position in `0..=1`.
    pub fn apply(self, t: f32) -> f32 {
        let t = t.clamp(0.0, 1.0);
        match self {
            Self::Linear => t,
            Self::EaseIn => t * t,
            Self::EaseOut => 1.0 - (1.0 - t) * (1.0 - t),
            Self::EaseInOut => t * t * (3.0 - 2.0 * t),
            Self::Step => {
                if t < 0.5 {
                    0.0
                } else {
                    1.0
                }
            }
            Self::Sine => 0.5 - 0.5 * (std::f32::consts::TAU * t).cos(),
        }
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Settings for the legend hover animation.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LegendHoverConfig {
    /// Play an effect while the pointer rests on a legend row.
    pub enabled: bool,
    /// Which effect.
    pub animation: LegendAnimation,
    /// How the cycle is shaped.
    pub easing: AnimationEasing,
    /// Seconds for one full cycle.
    pub period: f32,
    /// The second color; [`crisol_ui_kit::Theme::warning`](crisol_ui_kit::theme::Theme) when `None`.
    pub highlight: Option<Color32>,
}

impl Default for LegendHoverConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            animation: LegendAnimation::Blink,
            easing: AnimationEasing::Linear,
            period: 1.2,
            highlight: None,
        }
    }
}

/// An effect running on one series this frame.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Effect {
    pub kind: LegendAnimation,
    /// Eased cycle position in `0..1`.
    pub phase: f32,
    pub highlight: Color32,
    /// The extreme `Blink` color: white on dark themes, black on light themes (or the
    /// user's custom highlight).
    pub blink: Color32,
}

impl Effect {
    /// The effect of the hovered legend row at `now`, if motion is on.
    pub(super) fn for_hover(
        config: &PlotConfig,
        state: &PlotState,
        series: usize,
        now: f64,
        theme: &crisol_ui_kit::theme::Theme,
        motion: bool,
    ) -> Option<Self> {
        Self::for_legend(
            &config.legend_hover,
            state.legend_hover(),
            state.legend_hover_start(),
            series,
            now,
            theme,
            motion,
        )
    }

    /// The effect for `series` when the legend row `hovered` was entered at `start`.
    /// Shared by the 2D plot and [`super::Plot3D`], whose states both track one row.
    pub(super) fn for_legend(
        hover: &LegendHoverConfig,
        hovered: Option<usize>,
        start: f64,
        series: usize,
        now: f64,
        theme: &crisol_ui_kit::theme::Theme,
        motion: bool,
    ) -> Option<Self> {
        if !hover.enabled || !motion || hovered != Some(series) {
            return None;
        }
        let period = f64::from(hover.period.max(0.2));
        let elapsed = (now - start).max(0.0);
        let t = (elapsed / period).rem_euclid(1.0) as f32;
        Some(Self {
            kind: hover.animation,
            phase: hover.easing.apply(t),
            highlight: hover.highlight.unwrap_or(theme.warning),
            blink: hover.highlight.unwrap_or(if theme.is_dark() {
                Color32::WHITE
            } else {
                Color32::BLACK
            }),
        })
    }

    /// The smooth 0 → 1 → 0 oscillator of one cycle.
    pub(super) fn wave(self) -> f32 {
        0.5 - 0.5 * (std::f32::consts::TAU * self.phase).cos()
    }

    /// The series color blended toward the highlight.
    pub(super) fn color(self, base: Color32) -> Color32 {
        match self.kind {
            // A strong two-color blink: the series goes from white (on dark themes) or
            // black (on light themes) to its own color and back.
            LegendAnimation::Blink => mix(self.blink, base, self.wave()),
            LegendAnimation::Fade => base.gamma_multiply(1.0 - 0.65 * self.wave()),
            LegendAnimation::Throb => mix(base, self.highlight, 0.3 * self.wave()),
            LegendAnimation::Wave => mix(base, self.highlight, 0.55 * self.phase),
            LegendAnimation::Glow | LegendAnimation::Sweep | LegendAnimation::Reveal => {
                mix(base, self.highlight, 0.35 * self.wave())
            }
            _ => mix(base, self.highlight, 0.18 * self.wave()),
        }
    }

    /// A halo color for [`LegendAnimation::Glow`], or `None`.
    pub(super) fn halo(self) -> Option<Color32> {
        (self.kind == LegendAnimation::Glow).then(|| self.highlight.gamma_multiply(0.18 + 0.25 * self.wave()))
    }

    /// The stroke width, after width effects.
    pub(super) fn width(self, base: f32) -> f32 {
        match self.kind {
            LegendAnimation::Throb => base * (1.0 + 0.9 * self.wave()),
            LegendAnimation::Glow => base * 1.15,
            _ => base,
        }
    }

    /// The dash pattern, after motion effects.
    pub(super) fn dash(self, base: Dash) -> Dash {
        match self.kind {
            LegendAnimation::DashMarch => {
                let (dash, gap) = dash_gap(base);
                Dash::Marching {
                    dash,
                    gap,
                    offset: self.phase * (dash + gap) * 2.0,
                }
            }
            LegendAnimation::DashToggle => {
                let (dash, gap) = dash_gap(base);
                let grow = self.wave();
                if grow < 0.08 {
                    Dash::Solid
                } else {
                    Dash::Dashed(dash * grow, gap * grow)
                }
            }
            _ => base,
        }
    }

    /// A screen-space offset for one point of a line or polygon.
    pub(super) fn point_offset(self, index: usize, tangent: egui::Vec2) -> egui::Vec2 {
        if self.kind != LegendAnimation::Wiggle {
            return egui::Vec2::ZERO;
        }
        let length = tangent.length();
        if length < 1e-3 {
            return egui::Vec2::ZERO;
        }
        let normal = egui::vec2(-tangent.y, tangent.x) / length;
        normal * ((index as f32 * 0.55 + self.phase * std::f32::consts::TAU).sin() * 3.0)
    }

    /// A screen-space offset for one marker.
    pub(super) fn marker_offset(self, index: usize) -> egui::Vec2 {
        match self.kind {
            LegendAnimation::MarkerBounce => {
                let t = index as f32 * 0.8 + self.phase * std::f32::consts::TAU;
                egui::vec2(0.0, -((t.sin() + 1.0) * 2.5))
            }
            _ => egui::Vec2::ZERO,
        }
    }

    /// A marker radius, after pulse effects.
    pub(super) fn marker_radius(self, base: f32, index: usize) -> f32 {
        match self.kind {
            LegendAnimation::MarkerPulse => base * (1.0 + 0.7 * self.wave()),
            LegendAnimation::Twinkle => base * (0.7 + 0.8 * (index as f32 * 0.9 + self.phase * 6.0).sin().abs()),
            _ => base,
        }
    }

    /// A portion of `points` to draw in the highlight color, for sweep and reveal.
    pub(super) fn highlighted(self, points: &[egui::Pos2]) -> Option<Vec<egui::Pos2>> {
        let count = points.len();
        if count < 2 {
            return None;
        }
        match self.kind {
            LegendAnimation::Sweep => {
                let span = (count / 4).max(2);
                let start = ((count - span) as f32 * self.phase) as usize;
                Some(points[start..(start + span).min(count)].to_vec())
            }
            LegendAnimation::Reveal => {
                let end = (count as f32 * self.phase).ceil() as usize;
                (end >= 2).then(|| points[..end.min(count)].to_vec())
            }
            _ => None,
        }
    }

    /// Apply a pie slice offset: the hovered slice lifts out of the ring.
    pub(super) fn explode(self) -> f32 {
        match self.kind {
            LegendAnimation::Wiggle | LegendAnimation::MarkerBounce => 5.0 + 2.0 * self.wave(),
            _ => 3.0 + 2.0 * self.wave(),
        }
    }

    /// Apply a radar value scale, for vertex motion.
    pub(super) fn radial_scale(self) -> f32 {
        match self.kind {
            LegendAnimation::Wiggle => 1.0 + 0.06 * self.wave(),
            LegendAnimation::Throb => 1.0 + 0.03 * self.wave(),
            _ => 1.0,
        }
    }
}

fn dash_gap(dash: Dash) -> (f32, f32) {
    match dash {
        Dash::Solid => (7.0, 4.0),
        Dash::Dashed(dash, gap) => (dash, gap),
        Dash::Dotted(spacing) => (spacing * 0.5, spacing),
        Dash::Marching { dash, gap, .. } => (dash, gap),
    }
}

/// Linear interpolation in sRGB with unmultiplied alpha.
pub fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
    let t = t.clamp(0.0, 1.0);
    let [ar, ag, ab, aa] = a.to_srgba_unmultiplied();
    let [br, bg, bb, ba] = b.to_srgba_unmultiplied();
    let channel = |a: u8, b: u8| (f32::from(a) + (f32::from(b) - f32::from(a)) * t).round() as u8;
    Color32::from_rgba_unmultiplied(channel(ar, br), channel(ag, bg), channel(ab, bb), channel(aa, ba))
}

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

    #[test]
    fn easing_stays_in_range_and_shapes_the_cycle() {
        for easing in AnimationEasing::ALL {
            for step in 0..=20 {
                let t = easing.apply(step as f32 / 20.0);
                assert!((0.0..=1.0).contains(&t), "{easing:?} at {step}: {t}");
            }
        }
        assert_eq!(AnimationEasing::Linear.apply(0.25), 0.25);
        assert!(AnimationEasing::EaseIn.apply(0.25) < 0.25);
        assert!(AnimationEasing::EaseOut.apply(0.25) > 0.25);
    }

    #[test]
    fn every_animation_has_a_label() {
        for animation in LegendAnimation::ALL {
            assert!(!animation.label().is_empty());
        }
    }

    #[test]
    fn mix_blends_both_ends() {
        let a = Color32::from_rgb(0, 0, 0);
        let b = Color32::from_rgb(200, 100, 50);
        assert_eq!(mix(a, b, 0.0), a);
        assert_eq!(mix(a, b, 1.0), b);
        assert_eq!(mix(a, b, 0.5), Color32::from_rgb(100, 50, 25));
    }

    #[test]
    fn the_hover_effect_follows_the_pointer_and_reduced_motion() {
        let config = PlotConfig::default();
        let mut state = PlotState::default();
        let theme = crisol_ui_kit::theme::Theme::dark();
        assert!(Effect::for_hover(&config, &state, 0, 0.0, &theme, true).is_none());
        state.set_legend_hover(Some(0), 1.0);
        let effect = Effect::for_hover(&config, &state, 0, 1.6, &theme, true).expect("hovered");
        assert_eq!(effect.kind, LegendAnimation::Blink);
        assert!(effect.wave() > 0.0);
        assert!(Effect::for_hover(&config, &state, 1, 1.6, &theme, true).is_none());
        assert!(Effect::for_hover(&config, &state, 0, 1.6, &theme, false).is_none());
    }

    #[test]
    fn blink_goes_from_white_on_dark_and_black_on_light() {
        let base = Color32::from_rgb(30, 120, 220);
        let dark = Effect {
            kind: LegendAnimation::Blink,
            phase: 0.0,
            highlight: Color32::YELLOW,
            blink: Color32::WHITE,
        };
        let light = Effect {
            blink: Color32::BLACK,
            ..dark
        };
        assert_eq!(dark.color(base), Color32::WHITE);
        assert_eq!(light.color(base), Color32::BLACK);
        let mid = Effect { phase: 0.5, ..dark };
        assert_eq!(mid.color(base), base);
    }
}