plotkit-core 0.5.0

Core types and logic for the plotkit plotting library
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
//! Colorbar component for displaying color-to-value mappings.
//!
//! A colorbar is a narrow strip showing the gradient of a colormap alongside
//! tick marks and labels indicating the corresponding data values. It is the
//! essential companion to heatmaps and colormap-based scatter plots.
//!
//! # Examples
//!
//! ```
//! use plotkit_core::colorbar::{Colorbar, ColorbarOrientation};
//! use plotkit_core::colormap::Colormap;
//!
//! let cb = Colorbar::new(Colormap::Viridis, 0.0, 100.0)
//!     .label("Temperature (C)")
//!     .orientation(ColorbarOrientation::Vertical)
//!     .num_steps(128);
//! ```

use crate::colormap::Colormap;
use crate::primitives::*;
use crate::renderer::Renderer;
use crate::ticks;
use crate::theme::Theme;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Default number of discrete color steps in the gradient strip.
const DEFAULT_NUM_STEPS: usize = 256;

/// Number of ticks to aim for on the colorbar axis.
const COLORBAR_TICK_COUNT: usize = 5;

/// Fraction of the colorbar rect reserved for the gradient strip (the
/// remainder is used for tick labels and the optional label).
const GRADIENT_FRACTION: f64 = 0.35;

/// Padding between the gradient strip and the tick labels (in pixels).
const TICK_LABEL_GAP: f64 = 4.0;

/// Padding between the tick labels and the colorbar label (in pixels).
const LABEL_GAP: f64 = 6.0;

/// Length of each tick mark (in pixels).
const TICK_LENGTH: f64 = 4.0;

/// Fraction of the axes width reserved for the colorbar when one is present.
pub(crate) const COLORBAR_WIDTH_FRACTION: f64 = 0.12;

/// Gap between the plot area and the colorbar (in pixels).
pub(crate) const COLORBAR_GAP: f64 = 10.0;

// ---------------------------------------------------------------------------
// ColorbarOrientation
// ---------------------------------------------------------------------------

/// Orientation of the colorbar gradient strip.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum ColorbarOrientation {
    /// A vertical strip with values increasing from bottom to top.
    #[default]
    Vertical,
    /// A horizontal strip with values increasing from left to right.
    Horizontal,
}

// ---------------------------------------------------------------------------
// Colorbar
// ---------------------------------------------------------------------------

/// Configuration for a colorbar that maps colors to data values.
///
/// A colorbar renders as a narrow gradient strip with tick marks and labels
/// showing the value-to-color mapping used by a heatmap or scatter plot.
///
/// # Builder API
///
/// Use [`Colorbar::new`] to create a colorbar, then chain builder methods
/// (`.label()`, `.orientation()`, `.num_steps()`) to customise it.
#[derive(Debug, Clone)]
pub struct Colorbar {
    /// The colormap to display.
    pub cmap: Colormap,
    /// Minimum value of the data range.
    pub vmin: f64,
    /// Maximum value of the data range.
    pub vmax: f64,
    /// Optional label text displayed alongside the gradient.
    pub label: Option<String>,
    /// Orientation of the gradient strip.
    pub orientation: ColorbarOrientation,
    /// Number of discrete color steps used to render the gradient.
    pub num_steps: usize,
}

impl Colorbar {
    /// Creates a new colorbar with the given colormap and value range.
    ///
    /// Defaults to vertical orientation with 256 color steps and no label.
    pub fn new(cmap: Colormap, vmin: f64, vmax: f64) -> Self {
        Self {
            cmap,
            vmin,
            vmax,
            label: None,
            orientation: ColorbarOrientation::default(),
            num_steps: DEFAULT_NUM_STEPS,
        }
    }

    /// Sets the label text displayed alongside the colorbar gradient.
    pub fn label(mut self, label: &str) -> Self {
        self.label = Some(label.to_string());
        self
    }

    /// Sets the orientation of the colorbar.
    pub fn orientation(mut self, orientation: ColorbarOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Sets the number of discrete color steps in the gradient.
    ///
    /// Higher values produce smoother gradients. The default is 256.
    pub fn num_steps(mut self, n: usize) -> Self {
        self.num_steps = n.max(2);
        self
    }

    /// Sets the label text (mutable-borrow variant for in-place building).
    pub fn set_label(&mut self, label: &str) -> &mut Self {
        self.label = Some(label.to_string());
        self
    }

    /// Sets the orientation (mutable-borrow variant for in-place building).
    pub fn set_orientation(&mut self, orientation: ColorbarOrientation) -> &mut Self {
        self.orientation = orientation;
        self
    }

    /// Sets the number of color steps (mutable-borrow variant for in-place building).
    pub fn set_num_steps(&mut self, n: usize) -> &mut Self {
        self.num_steps = n.max(2);
        self
    }
}

// ---------------------------------------------------------------------------
// Drawing
// ---------------------------------------------------------------------------

/// Draws a colorbar into the given rectangle.
///
/// The function divides `rect` into a gradient area and a label/tick area,
/// draws the colormap gradient as a series of thin filled rectangles, then
/// overlays tick marks and labels showing the corresponding data values.
///
/// For a vertical colorbar, values increase from bottom to top. For a
/// horizontal colorbar, values increase from left to right.
pub fn draw_colorbar(
    renderer: &mut impl Renderer,
    colorbar: &Colorbar,
    rect: &Rect,
    theme: &Theme,
) {
    match colorbar.orientation {
        ColorbarOrientation::Vertical => draw_vertical(renderer, colorbar, rect, theme),
        ColorbarOrientation::Horizontal => draw_horizontal(renderer, colorbar, rect, theme),
    }
}

/// Draws a vertical colorbar (gradient runs bottom-to-top).
fn draw_vertical(
    renderer: &mut impl Renderer,
    colorbar: &Colorbar,
    rect: &Rect,
    theme: &Theme,
) {
    // Divide the rect: gradient on the left, ticks/label on the right.
    let gradient_width = (rect.width * GRADIENT_FRACTION).max(8.0);
    let gradient_rect = Rect::new(rect.x, rect.y, gradient_width, rect.height);

    // --- Draw gradient strip ---
    let n = colorbar.num_steps;
    let step_height = rect.height / n as f64;

    for i in 0..n {
        // t goes from 1.0 (top) to 0.0 (bottom) so that values increase upward.
        let t = 1.0 - (i as f64 + 0.5) / n as f64;
        let color = colorbar.cmap.map(t);

        let cell_y = rect.y + i as f64 * step_height;
        let cell_rect = Rect::new(rect.x, cell_y, gradient_width, step_height + 0.5);
        let cell_path = Path::rect(cell_rect);
        renderer.fill_path(&cell_path, &Paint::new(color), Affine::IDENTITY);
    }

    // --- Draw border around gradient ---
    let border_path = Path::rect(gradient_rect);
    let border_paint = Paint::new(theme.spine_color);
    let border_stroke = Stroke::new(theme.spine_width);
    renderer.stroke_path(&border_path, &border_paint, &border_stroke, Affine::IDENTITY);

    // --- Generate ticks ---
    let tick_data = ticks::generate_ticks(
        colorbar.vmin,
        colorbar.vmax,
        COLORBAR_TICK_COUNT,
        &crate::scale::Scale::Linear,
    );

    let tick_x_start = rect.x + gradient_width;
    let tick_x_end = tick_x_start + TICK_LENGTH;
    let label_x = tick_x_end + TICK_LABEL_GAP;

    let tick_paint = Paint::new(theme.tick_color);
    let tick_stroke = Stroke::new(theme.spine_width);

    let label_style = TextStyle {
        size: theme.tick_label_size,
        color: theme.text_color,
        weight: FontWeight::Normal,
        family: theme.font_family.clone(),
        halign: HAlign::Left,
        valign: VAlign::Middle,
    };

    let range = colorbar.vmax - colorbar.vmin;

    for tick in &tick_data {
        // Map tick value to y pixel (vmax at top, vmin at bottom).
        let t = if range.abs() < f64::EPSILON {
            0.5
        } else {
            (tick.value - colorbar.vmin) / range
        };
        let py = rect.y + rect.height * (1.0 - t);

        // Tick mark.
        let mut tick_path = Path::new();
        tick_path.move_to(tick_x_start, py);
        tick_path.line_to(tick_x_end, py);
        renderer.stroke_path(&tick_path, &tick_paint, &tick_stroke, Affine::IDENTITY);

        // Tick label.
        renderer.draw_text(
            &tick.label,
            Point::new(label_x, py),
            &label_style,
            Affine::IDENTITY,
        );
    }

    // --- Draw optional label ---
    if let Some(ref label_text) = colorbar.label {
        let cb_label_style = TextStyle {
            size: theme.axis_label_size,
            color: theme.text_color,
            weight: FontWeight::Normal,
            family: theme.font_family.clone(),
            halign: HAlign::Center,
            valign: VAlign::Top,
        };

        // Place label below the gradient, centered horizontally.
        let label_x_pos = rect.x + gradient_width / 2.0;
        let label_y_pos = rect.y + rect.height + LABEL_GAP;
        renderer.draw_text(
            label_text,
            Point::new(label_x_pos, label_y_pos),
            &cb_label_style,
            Affine::IDENTITY,
        );
    }
}

/// Draws a horizontal colorbar (gradient runs left-to-right).
fn draw_horizontal(
    renderer: &mut impl Renderer,
    colorbar: &Colorbar,
    rect: &Rect,
    theme: &Theme,
) {
    // Divide the rect: gradient on top, ticks/label below.
    let gradient_height = (rect.height * GRADIENT_FRACTION).max(8.0);
    let gradient_rect = Rect::new(rect.x, rect.y, rect.width, gradient_height);

    // --- Draw gradient strip ---
    let n = colorbar.num_steps;
    let step_width = rect.width / n as f64;

    for i in 0..n {
        let t = (i as f64 + 0.5) / n as f64;
        let color = colorbar.cmap.map(t);

        let cell_x = rect.x + i as f64 * step_width;
        let cell_rect = Rect::new(cell_x, rect.y, step_width + 0.5, gradient_height);
        let cell_path = Path::rect(cell_rect);
        renderer.fill_path(&cell_path, &Paint::new(color), Affine::IDENTITY);
    }

    // --- Draw border around gradient ---
    let border_path = Path::rect(gradient_rect);
    let border_paint = Paint::new(theme.spine_color);
    let border_stroke = Stroke::new(theme.spine_width);
    renderer.stroke_path(&border_path, &border_paint, &border_stroke, Affine::IDENTITY);

    // --- Generate ticks ---
    let tick_data = ticks::generate_ticks(
        colorbar.vmin,
        colorbar.vmax,
        COLORBAR_TICK_COUNT,
        &crate::scale::Scale::Linear,
    );

    let tick_y_start = rect.y + gradient_height;
    let tick_y_end = tick_y_start + TICK_LENGTH;
    let label_y = tick_y_end + TICK_LABEL_GAP;

    let tick_paint = Paint::new(theme.tick_color);
    let tick_stroke = Stroke::new(theme.spine_width);

    let label_style = TextStyle {
        size: theme.tick_label_size,
        color: theme.text_color,
        weight: FontWeight::Normal,
        family: theme.font_family.clone(),
        halign: HAlign::Center,
        valign: VAlign::Top,
    };

    let range = colorbar.vmax - colorbar.vmin;

    for tick in &tick_data {
        let t = if range.abs() < f64::EPSILON {
            0.5
        } else {
            (tick.value - colorbar.vmin) / range
        };
        let px = rect.x + rect.width * t;

        // Tick mark.
        let mut tick_path = Path::new();
        tick_path.move_to(px, tick_y_start);
        tick_path.line_to(px, tick_y_end);
        renderer.stroke_path(&tick_path, &tick_paint, &tick_stroke, Affine::IDENTITY);

        // Tick label.
        renderer.draw_text(
            &tick.label,
            Point::new(px, label_y),
            &label_style,
            Affine::IDENTITY,
        );
    }

    // --- Draw optional label ---
    if let Some(ref label_text) = colorbar.label {
        let cb_label_style = TextStyle {
            size: theme.axis_label_size,
            color: theme.text_color,
            weight: FontWeight::Normal,
            family: theme.font_family.clone(),
            halign: HAlign::Center,
            valign: VAlign::Top,
        };

        let label_x_pos = rect.x + rect.width / 2.0;
        let label_y_pos = label_y + theme.tick_label_size + LABEL_GAP;
        renderer.draw_text(
            label_text,
            Point::new(label_x_pos, label_y_pos),
            &cb_label_style,
            Affine::IDENTITY,
        );
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn colorbar_default_settings() {
        let cb = Colorbar::new(Colormap::Viridis, 0.0, 1.0);
        assert_eq!(cb.cmap, Colormap::Viridis);
        assert!((cb.vmin - 0.0).abs() < f64::EPSILON);
        assert!((cb.vmax - 1.0).abs() < f64::EPSILON);
        assert!(cb.label.is_none());
        assert_eq!(cb.orientation, ColorbarOrientation::Vertical);
        assert_eq!(cb.num_steps, DEFAULT_NUM_STEPS);
    }

    #[test]
    fn colorbar_with_label() {
        let cb = Colorbar::new(Colormap::Plasma, 0.0, 100.0)
            .label("Temperature");
        assert_eq!(cb.label.as_deref(), Some("Temperature"));
    }

    #[test]
    fn colorbar_set_label_mut() {
        let mut cb = Colorbar::new(Colormap::Plasma, 0.0, 100.0);
        cb.set_label("Pressure");
        assert_eq!(cb.label.as_deref(), Some("Pressure"));
    }

    #[test]
    fn colorbar_vertical_orientation() {
        let cb = Colorbar::new(Colormap::Viridis, 0.0, 1.0)
            .orientation(ColorbarOrientation::Vertical);
        assert_eq!(cb.orientation, ColorbarOrientation::Vertical);
    }

    #[test]
    fn colorbar_horizontal_orientation() {
        let cb = Colorbar::new(Colormap::Viridis, 0.0, 1.0)
            .orientation(ColorbarOrientation::Horizontal);
        assert_eq!(cb.orientation, ColorbarOrientation::Horizontal);
    }

    #[test]
    fn colorbar_custom_num_steps() {
        let cb = Colorbar::new(Colormap::Inferno, -1.0, 1.0)
            .num_steps(128);
        assert_eq!(cb.num_steps, 128);
    }

    #[test]
    fn colorbar_num_steps_clamped_to_min_2() {
        let cb = Colorbar::new(Colormap::Inferno, 0.0, 1.0)
            .num_steps(0);
        assert_eq!(cb.num_steps, 2);
    }

    #[test]
    fn colorbar_vmin_vmax_respected() {
        let cb = Colorbar::new(Colormap::Coolwarm, -50.0, 50.0);
        assert!((cb.vmin - (-50.0)).abs() < f64::EPSILON);
        assert!((cb.vmax - 50.0).abs() < f64::EPSILON);
    }

    #[test]
    fn colorbar_builder_chaining() {
        let cb = Colorbar::new(Colormap::Turbo, 0.0, 10.0)
            .label("Speed")
            .orientation(ColorbarOrientation::Horizontal)
            .num_steps(64);
        assert_eq!(cb.cmap, Colormap::Turbo);
        assert_eq!(cb.label.as_deref(), Some("Speed"));
        assert_eq!(cb.orientation, ColorbarOrientation::Horizontal);
        assert_eq!(cb.num_steps, 64);
    }

    #[test]
    fn colorbar_different_colormaps() {
        let colormaps = [
            Colormap::Viridis,
            Colormap::Plasma,
            Colormap::Inferno,
            Colormap::Magma,
            Colormap::Cividis,
            Colormap::Turbo,
            Colormap::Coolwarm,
            Colormap::Blues,
        ];
        for &cmap in &colormaps {
            let cb = Colorbar::new(cmap, 0.0, 1.0);
            assert_eq!(cb.cmap, cmap);
        }
    }

    #[test]
    fn orientation_default_is_vertical() {
        let orientation = ColorbarOrientation::default();
        assert_eq!(orientation, ColorbarOrientation::Vertical);
    }

    #[test]
    fn colorbar_equal_vmin_vmax() {
        // Edge case: zero-width range should not panic.
        let cb = Colorbar::new(Colormap::Viridis, 5.0, 5.0);
        assert!((cb.vmin - cb.vmax).abs() < f64::EPSILON);
    }
}