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
//! Figure colorbar: a thin vertical gradient strip with tick labels.
//!
//! [`Figure::colorbar`] registers a colorbar mapping a named [`Colormap`] over a
//! data range `[vmin, vmax]`; [`Figure::draw`] renders each registered colorbar
//! as a narrow stack of filled rectangles on the right side of the figure, with
//! a few nicely placed tick labels.
use crate::axis::ticker::{AutoLocator, Formatter, Locator, ScalarFormatter};
use crate::core::{Affine2D, Bbox, Path, color::Colormap, color::Rgba, color::colormap};
use crate::render::{GraphicsContext, Renderer};
use crate::text::FontSource;
use crate::figure::figure::Figure;
/// Number of gradient slices sampled along the colorbar.
const N_SLICES: usize = 256;
/// Left edge of the colorbar, in figure fractions.
const BAR_LEFT: f64 = 0.90;
/// Width of the colorbar, in figure fractions.
const BAR_WIDTH: f64 = 0.03;
/// Bottom edge of the colorbar, in figure fractions.
const BAR_BOTTOM: f64 = 0.10;
/// Height of the colorbar, in figure fractions.
const BAR_HEIGHT: f64 = 0.80;
/// Font size of colorbar tick labels, in pixels.
const TICK_FONT_SIZE: f64 = 9.0;
/// Length of a tick mark, in pixels.
const TICK_LEN: f64 = 4.0;
/// Gap between a tick mark and its label, in pixels.
const TICK_GAP: f64 = 3.0;
/// A registered colorbar: a colormap mapped over a data range.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Colorbar {
/// The name of the colormap to sample (e.g. `"viridis"`).
cmap_name: String,
/// Lower bound of the mapped data range.
vmin: f64,
/// Upper bound of the mapped data range.
vmax: f64,
/// Placement rectangle in figure fractions; `None` uses the legacy
/// right-hand default strip.
rect: Option<Bbox>,
/// Gradient direction: `false` runs bottom→top (ticks right), `true` runs
/// left→right (ticks below).
horizontal: bool,
}
impl Figure {
/// Add a vertical colorbar on the right of the figure.
///
/// `cmap_name` is a builtin colormap name (see [`colormap`]); `vmin`/`vmax`
/// label the bottom and top of the gradient. Unknown colormap names render
/// no gradient (the strip is skipped at draw time).
pub fn colorbar(&mut self, cmap_name: &str, vmin: f64, vmax: f64) -> &mut Self {
self.colorbars.push(Colorbar {
cmap_name: cmap_name.to_string(),
vmin,
vmax,
rect: None,
horizontal: false,
});
self
}
/// Add a **vertical** colorbar in the figure-fraction rectangle
/// `(left, bottom, width, height)` — matplotlib's dedicated-axes
/// `colorbar(cax=…)` form, and the primitive a *shared* colorbar for a
/// subplot grid is built on: compute one `[vmin, vmax]` across the grid's
/// data and place one bar in its own column (a [`GridSpec`] cell resolves
/// to exactly such a rectangle via
/// [`SubplotSpec::get_position`](crate::figure::SubplotSpec::get_position)).
///
/// The gradient runs bottom→top with ticks and labels on the right.
///
/// [`GridSpec`]: crate::figure::GridSpec
///
/// ```
/// use rizzma::Figure;
///
/// let mut fig = Figure::new(5.0, 3.0);
/// fig.add_axes(0.10, 0.10, 0.36, 0.8);
/// fig.add_axes(0.50, 0.10, 0.36, 0.8);
/// // One shared colorbar for both axes, in its own right-hand column.
/// fig.colorbar_at((0.90, 0.10, 0.03, 0.8), "viridis", 0.0, 1.0);
/// assert!(!fig.encode_png().unwrap().is_empty());
/// ```
pub fn colorbar_at(
&mut self,
rect: (f64, f64, f64, f64),
cmap_name: &str,
vmin: f64,
vmax: f64,
) -> &mut Self {
let (left, bottom, width, height) = rect;
self.colorbars.push(Colorbar {
cmap_name: cmap_name.to_string(),
vmin,
vmax,
rect: Some(Bbox::from_bounds(left, bottom, width, height)),
horizontal: false,
});
self
}
/// Add a **horizontal** colorbar in the figure-fraction rectangle
/// `(left, bottom, width, height)`: the gradient runs left→right with
/// ticks and labels below (matplotlib's `orientation="horizontal"`).
pub fn colorbar_at_horizontal(
&mut self,
rect: (f64, f64, f64, f64),
cmap_name: &str,
vmin: f64,
vmax: f64,
) -> &mut Self {
let (left, bottom, width, height) = rect;
self.colorbars.push(Colorbar {
cmap_name: cmap_name.to_string(),
vmin,
vmax,
rect: Some(Bbox::from_bounds(left, bottom, width, height)),
horizontal: true,
});
self
}
/// Draw every registered colorbar onto `renderer` at figure pixel size
/// `(w, h)`.
pub(crate) fn draw_colorbars(&self, renderer: &mut dyn Renderer, w: f64, h: f64) {
for cb in &self.colorbars {
cb.draw(renderer, w, h, self.font_source());
}
}
}
impl Colorbar {
/// Draw this colorbar into `renderer` given the figure size in pixels.
fn draw(&self, renderer: &mut dyn Renderer, fig_w: f64, fig_h: f64, font: &FontSource) {
let Some(cmap) = colormap(&self.cmap_name) else {
return;
};
let frac = self
.rect
.unwrap_or_else(|| Bbox::from_bounds(BAR_LEFT, BAR_BOTTOM, BAR_WIDTH, BAR_HEIGHT));
let bar = Bbox::from_extents(
frac.xmin() * fig_w,
frac.ymin() * fig_h,
frac.xmax() * fig_w,
frac.ymax() * fig_h,
);
self.draw_gradient(renderer, &bar, cmap.as_ref());
self.draw_frame(renderer, &bar);
self.draw_ticks(renderer, &bar, font);
}
/// Fill the bar with `N_SLICES` thin rectangles sampled from `cmap`,
/// running bottom→top (vertical) or left→right (horizontal).
fn draw_gradient(&self, renderer: &mut dyn Renderer, bar: &Bbox, cmap: &dyn Colormap) {
let id = Affine2D::identity();
let along = if self.horizontal {
bar.width()
} else {
bar.height()
};
let slice = along / N_SLICES as f64;
for i in 0..N_SLICES {
let t = i as f64 / (N_SLICES as f64 - 1.0);
let a0 = i as f64 * slice;
// Overlap each slice by a hair to avoid seam gaps from rounding.
let a1 = a0 + slice + 0.5;
let rect = if self.horizontal {
Bbox::from_extents(bar.xmin() + a0, bar.ymin(), bar.xmin() + a1, bar.ymax())
} else {
Bbox::from_extents(bar.xmin(), bar.ymin() + a0, bar.xmax(), bar.ymin() + a1)
};
renderer.draw_path(
&GraphicsContext::new(),
&rect_path(&rect),
&id,
Some(cmap.sample(t)),
);
}
}
/// Stroke a thin black frame around the bar.
fn draw_frame(&self, renderer: &mut dyn Renderer, bar: &Bbox) {
let frame_gc = GraphicsContext::new()
.with_stroke(Rgba::BLACK)
.with_line_width(0.8);
renderer.draw_path(&frame_gc, &rect_path(bar), &Affine2D::identity(), None);
}
/// Draw tick marks and labels mapping the gradient onto `[vmin, vmax]`.
fn draw_ticks(&self, renderer: &mut dyn Renderer, bar: &Bbox, font: &FontSource) {
let s = renderer.decoration_scale();
let (tick_len, tick_gap, font_size) = (TICK_LEN * s, TICK_GAP * s, TICK_FONT_SIZE * s);
let locator = AutoLocator::new();
let ticks = locator.tick_values(self.vmin, self.vmax);
let mut formatter = ScalarFormatter::new();
formatter.set_locs(&ticks);
let id = Affine2D::identity();
let tick_gc = GraphicsContext::new()
.with_stroke(Rgba::BLACK)
.with_line_width(0.8);
let span = self.vmax - self.vmin;
if span.abs() < f64::EPSILON {
return;
}
for (i, &value) in ticks.iter().enumerate() {
if value < self.vmin.min(self.vmax) || value > self.vmin.max(self.vmax) {
continue;
}
let frac = (value - self.vmin) / span;
let label = formatter.format(value, Some(i));
if self.horizontal {
// Tick mark below the bar, label centered under it.
let x = bar.xmin() + frac * bar.width();
let ty0 = bar.ymin();
let ty1 = bar.ymin() - tick_len;
let mark = Path::from_polyline(&[[x, ty0], [x, ty1]]);
renderer.draw_path(&tick_gc, &mark, &id, None);
let width = font.measure(&label, font_size).width;
let lx = x - width / 2.0;
let ly = ty1 - tick_gap - font_size * 0.8;
let text = font.text_to_path(&label, font_size, [lx, ly]);
renderer.draw_path(&GraphicsContext::new(), &text, &id, Some(Rgba::BLACK));
} else {
let y = bar.ymin() + frac * bar.height();
// Tick mark on the right edge.
let tx0 = bar.xmax();
let tx1 = bar.xmax() + tick_len;
let mark = Path::from_polyline(&[[tx0, y], [tx1, y]]);
renderer.draw_path(&tick_gc, &mark, &id, None);
// Label, vertically centered on the tick.
let lx = tx1 + tick_gap;
let ly = y - font_size / 3.0;
let text = font.text_to_path(&label, font_size, [lx, ly]);
renderer.draw_path(&GraphicsContext::new(), &text, &id, Some(Rgba::BLACK));
}
}
}
}
/// A closed-rectangle [`Path`] tracing `bbox`'s four corners.
fn rect_path(bbox: &Bbox) -> Path {
let (x0, y0) = (bbox.xmin(), bbox.ymin());
let (x1, y1) = (bbox.xmax(), bbox.ymax());
Path::from_polyline(&[[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]])
}
#[cfg(test)]
mod tests {
use crate::figure::Figure;
use crate::skia::SkiaRenderer;
/// Straight RGBA bytes of the pixel at `(x, y)` (top-left origin).
fn pixel(r: &SkiaRenderer, x: u32, y: u32) -> [u8; 4] {
let p = r.pixmap().pixel(x, y).expect("pixel in bounds");
[p.red(), p.green(), p.blue(), p.alpha()]
}
#[test]
fn colorbar_strip_has_varied_colors() {
let mut fig = Figure::new(4.0, 4.0).with_dpi(100.0);
fig.add_axes(0.1, 0.1, 0.7, 0.8);
fig.colorbar("viridis", 0.0, 1.0);
let r = fig.render();
let (w, h) = fig.size_px();
let (w, h) = (w as u32, h as u32);
// Sample a column through the middle of the colorbar strip
// (left = 0.90, width = 0.03 -> center near 0.915 of the width).
let x = (0.915 * w as f64) as u32;
let mut colors = std::collections::HashSet::new();
let mut non_bg = 0;
for y in 0..h {
let p = pixel(&r, x, y);
if p != [255, 255, 255, 255] {
non_bg += 1;
colors.insert([p[0], p[1], p[2]]);
}
}
assert!(non_bg > 0, "expected non-background pixels in the strip");
assert!(
colors.len() > 5,
"expected a varied gradient, got {} distinct colors",
colors.len()
);
}
/// Count distinct non-background colors along a pixel column (`vertical`)
/// or row (`!vertical`) at fraction `at` of the canvas.
fn gradient_colors(r: &SkiaRenderer, w: u32, h: u32, at: f64, vertical: bool) -> usize {
let mut colors = std::collections::HashSet::new();
if vertical {
let x = (at * w as f64) as u32;
for y in 0..h {
let p = pixel(r, x, y);
if p != [255, 255, 255, 255] {
colors.insert([p[0], p[1], p[2]]);
}
}
} else {
let y = (at * h as f64) as u32;
for x in 0..w {
let p = pixel(r, x, y);
if p != [255, 255, 255, 255] {
colors.insert([p[0], p[1], p[2]]);
}
}
}
colors.len()
}
#[test]
fn colorbar_at_places_the_gradient_in_the_requested_rect() {
let mut fig = Figure::new(4.0, 4.0).with_dpi(100.0);
// A bar in the *left* half, well away from the legacy right-side slot.
fig.colorbar_at((0.20, 0.10, 0.04, 0.80), "viridis", 0.0, 1.0);
let r = fig.render();
let (w, h) = fig.size_px();
let (w, h) = (w as u32, h as u32);
// Gradient present at the requested column (center of the 0.20..0.24
// strip)…
assert!(
gradient_colors(&r, w, h, 0.22, true) > 5,
"expected a gradient in the requested rect"
);
// …and nothing at the legacy default position.
assert_eq!(
gradient_colors(&r, w, h, 0.915, true),
0,
"legacy strip position must stay empty"
);
}
#[test]
fn horizontal_colorbar_runs_left_to_right() {
let mut fig = Figure::new(4.0, 4.0).with_dpi(100.0);
fig.colorbar_at_horizontal((0.10, 0.85, 0.80, 0.05), "viridis", 0.0, 1.0);
let r = fig.render();
let (w, h) = fig.size_px();
let (w, h) = (w as u32, h as u32);
// Note: figure fractions are y-up; row 0.875 in y-up is 0.125 in the
// top-down pixmap.
assert!(
gradient_colors(&r, w, h, 0.125, false) > 5,
"expected a horizontal gradient across the requested row"
);
}
}