lichtung-plot 0.1.0

Strongly typed scientific plotting with deterministic SVG and PNG output
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
//! One immutable opaque mapping over prepared raw semantic identities.
//! Prepared family and guide consumers share this opaque mapping.

use super::{
    AccentPalette, CategoricalColorScale, ColorDomain, ColorScales, ColorStop,
    ContinuousColorScale, OpaqueColor, PaletteCapacity, QualitativePalette, SequentialGradient,
};
use crate::font::canonical::OperationContext;
use crate::numeric::mapping::normalized_fraction;
use crate::render::backend::GradientStop;
use crate::Error;

type Rgb = (u8, u8, u8);

impl ContinuousColorScale {
    pub(crate) fn resolved_in(&self, owner: &ColorScales) -> Result<Self, Error> {
        if let Some(center) = self.get_center() {
            Self::diverging(
                center,
                Some(
                    self.get_diverging_gradient()
                        .unwrap_or(owner.get_diverging())
                        .clone(),
                ),
                self.get_domain().cloned(),
            )
        } else {
            Self::sequential(
                Some(
                    self.get_sequential_gradient()
                        .unwrap_or(owner.get_sequential())
                        .clone(),
                ),
                self.get_domain().cloned(),
            )
        }
    }

    pub(crate) fn prepared_domain(
        &self,
        values: &[f64],
        family: &str,
    ) -> Result<ColorDomain, Error> {
        if let Some(domain) = self.get_domain() {
            return Ok(domain.clone());
        }
        let (minimum, maximum) = crate::data::min_max(values.iter().copied(), family)?;
        let Some(center) = self.get_center() else {
            return ColorDomain::exact(minimum, maximum);
        };
        let mut radius = 0.0_f64;
        for value in values {
            let distance = if *value >= center {
                *value - center
            } else {
                center - *value
            };
            if !distance.is_finite() {
                return Err(Error::Data(format!("{family}: cannot form a finite symmetric diverging domain around center {center}; set explicit color limits containing the center")));
            }
            radius = radius.max(distance);
        }
        if radius == 0.0 {
            return ColorDomain::exact(center, center);
        }
        let minimum = center - radius;
        let maximum = center + radius;
        if !minimum.is_finite() || !maximum.is_finite() || minimum >= center || maximum <= center {
            return Err(Error::Data(format!("{family}: symmetric diverging domain around center {center} exceeds finite f64 bounds; set explicit color limits containing the center")));
        }
        ColorDomain::exact(minimum, maximum)
    }
}

#[derive(Debug, Default)]
pub(crate) struct PaletteCache {
    entries: Vec<CachedPalette>,
}

#[derive(Debug)]
struct CachedPalette {
    seeds: Vec<OpaqueColor>,
    colors: Vec<OpaqueColor>,
}

impl QualitativePalette {
    pub(crate) fn validate_capacity(&self, required: usize) -> Result<(), Error> {
        if self.capacity() == PaletteCapacity::Fixed && self.colors().len() < required {
            return Err(Error::Data(format!(
                "fixed categorical palette has {} colors for {required} used identities",
                self.colors().len(),
            )));
        }
        Ok(())
    }

    fn extend_categorical_palette(
        &self,
        requested: usize,
        resources: &OperationContext,
    ) -> Result<Vec<OpaqueColor>, Error> {
        if requested <= self.colors().len() {
            return super::color::extend_palette(self.colors(), requested);
        }
        let mut cache = resources
            .palette_cache
            .lock()
            .map_err(|_| Error::Render("resolved palette cache synchronization failed".into()))?;
        for entry in &mut cache.entries {
            if entry.seeds.len() != self.colors().len() {
                continue;
            }
            let mut matching = true;
            for (cached, current) in entry.seeds.iter().zip(self.colors()) {
                if cached != current {
                    matching = false;
                    break;
                }
            }
            if !matching {
                continue;
            }
            if requested <= entry.colors.len() {
                return super::color::extend_palette(&entry.colors, requested);
            }
            let result = super::color::extend_palette(self.colors(), requested)?;
            let mut retained = Vec::new();
            reserve(&mut retained, result.len())?;
            retained.extend_from_slice(&result);
            entry.colors = retained;
            return Ok(result);
        }
        let result = super::color::extend_palette(self.colors(), requested)?;
        let mut seeds = Vec::new();
        reserve(&mut seeds, self.colors().len())?;
        seeds.extend_from_slice(self.colors());
        let mut colors = Vec::new();
        reserve(&mut colors, result.len())?;
        colors.extend_from_slice(&result);

        reserve(&mut cache.entries, 1)?;
        cache.entries.push(CachedPalette { seeds, colors });
        Ok(result)
    }
}

#[derive(Debug, Clone)]
pub(crate) struct CategoryColors {
    identities: Vec<crate::data::category::CategoryKey>,
    colors: Vec<OpaqueColor>,
}

impl CategoryColors {
    pub(crate) fn resolve<'a>(
        field: Option<&crate::data::category::RawField>,
        identities: impl Iterator<Item = &'a str> + Clone,
        palette: &QualitativePalette,
        exact: Option<&CategoricalColorScale>,
        resources: &OperationContext,
    ) -> Result<Self, Error> {
        // Selection retains the current immutable map/palette, never another key table.

        let count = identities.clone().count();
        let selected = exact
            .cloned()
            .unwrap_or_else(|| CategoricalColorScale::from_palette(palette.clone()));
        let colors = if let Some(exact) = selected.map() {
            let mut colors = Vec::new();
            reserve(&mut colors, count)?;
            for identity in identities.clone() {
                colors.push(exact.color_in(identity).ok_or_else(|| {
                    Error::Data(format!(
                        "categorical color map has no color for used raw key {identity:?}"
                    ))
                })?);
            }
            colors
        } else {
            let palette = selected.palette().expect("validated palette selection");
            match palette.capacity() {
                PaletteCapacity::Fixed => {
                    let seeds = palette.colors();
                    palette.validate_capacity(count)?;
                    let mut colors = Vec::new();
                    reserve(&mut colors, count)?;
                    colors.extend_from_slice(&seeds[..count]);
                    colors
                }
                PaletteCapacity::Extendable => {
                    palette.extend_categorical_palette(count, resources)?
                }
            }
        };
        let mut retained = Vec::new();
        reserve(&mut retained, count)?;
        for identity in identities {
            if retained
                .iter()
                .any(|seen: &crate::data::category::CategoryKey| seen.as_ref().value == identity)
            {
                return Err(Error::Data(format!(
                    "resolved categorical domain repeats raw key {identity:?}"
                )));
            }

            retained.push(crate::data::category::CategoryKey::new(
                field.expect("prepared categorical field").clone(),
                identity.to_owned(),
            ));
        }
        Ok(Self {
            identities: retained,
            colors,
        })
    }

    pub(crate) fn color(&self, raw_identity: &str) -> Result<Rgb, Error> {
        for (index, identity) in self.identities.iter().enumerate() {
            if identity.as_ref().value == raw_identity {
                return Ok(self.colors[index].as_rgb());
            }
        }
        Err(Error::Data(format!(
            "used categorical color identity {raw_identity:?} is absent from the prepared domain"
        )))
    }

    #[cfg(test)]
    pub(crate) fn colors(&self) -> &[OpaqueColor] {
        &self.colors
    }
}

#[derive(Debug, Clone)]
pub(crate) struct AccentColors {
    colors: Vec<OpaqueColor>,
}

impl AccentColors {
    pub(crate) fn resolve(palette: &AccentPalette, slots: usize) -> Result<Self, Error> {
        Ok(Self {
            colors: super::color::extend_palette(palette.seeds(), slots)?,
        })
    }

    pub(crate) fn color(&self, slot: usize) -> Result<Rgb, Error> {
        self.colors
            .get(slot)
            .copied()
            .map(OpaqueColor::as_rgb)
            .ok_or_else(|| {
                Error::Render("undeclared accent slot is not part of this resolved plot".into())
            })
    }
}

/// One prepared numeric domain and ordered opaque stops, shared by marks and
/// their guide. Normalization happens before first-party OKLab interpolation.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ContinuousColors {
    domain: ColorDomain,
    gradient: SequentialGradient,
    neutral: Option<(f64, OpaqueColor)>,
}

impl ContinuousColors {
    pub(crate) fn same(&self, other: &Self) -> bool {
        if self.domain != other.domain || self.neutral != other.neutral {
            return false;
        }
        self.gradient.same_in(&other.gradient)
    }

    pub(crate) fn resolve(domain: (f64, f64), stops: &[ColorStop]) -> Result<Self, Error> {
        Ok(Self {
            domain: ColorDomain::exact(domain.0, domain.1)?,
            gradient: SequentialGradient::new(stops.iter().copied())?,
            neutral: None,
        })
    }

    pub(crate) fn selected(
        domain: (f64, f64),
        selection: &ContinuousColorScale,
    ) -> Result<Self, Error> {
        let domain = ColorDomain::exact(domain.0, domain.1)?;
        let pair = domain.as_pair();
        let semantic = selection.stops()?;
        let Some(center) = selection.get_center() else {
            return Self::resolve(pair, semantic);
        };
        if center < pair.0 || center > pair.1 {
            return Err(Error::Data(
                "diverging color domain must contain its finite center".into(),
            ));
        }
        let neutral = selection
            .get_diverging_gradient()
            .expect("validated diverging selection")
            .neutral();
        let mut stops = Vec::new();
        crate::data::source::reserve_source(&mut stops, semantic.len())?;
        if pair.0.to_bits() == pair.1.to_bits() {
            stops.push(ColorStop::new(0.0, neutral)?);
            stops.push(ColorStop::new(1.0, neutral)?);
        } else {
            let at_low = center.to_bits() == pair.0.to_bits();
            let at_high = center.to_bits() == pair.1.to_bits();
            let c = normalized_fraction(pair, center)?;
            if !(at_low || at_high || c > 0.0 && c < 1.0) {
                return Err(Error::Data(
                    "diverging center has no representable gradient stop".into(),
                ));
            }
            for stop in semantic {
                let u = stop.offset();
                if (at_low && u < 0.5) || (at_high && u > 0.5) {
                    continue;
                }
                let offset = if at_low {
                    (u - 0.5) * 2.0
                } else if at_high {
                    u * 2.0
                } else if u == 0.0 {
                    0.0
                } else if u.to_bits() == 0.5_f64.to_bits() {
                    c
                } else if u.to_bits() == 1.0_f64.to_bits() {
                    1.0
                } else if u < 0.5 {
                    (u * 2.0) * c
                } else {
                    c + ((u - 0.5) * 2.0) * (1.0 - c)
                };
                stops.push(ColorStop::new(offset, stop.color())?);
            }
        }
        let mut selected = Self::resolve(pair, &stops)?;
        selected.neutral = Some((center, neutral));
        Ok(selected)
    }

    pub(crate) fn stops(&self) -> &[ColorStop] {
        self.gradient.stops()
    }

    pub(crate) fn domain(&self) -> (f64, f64) {
        self.domain.as_pair()
    }

    pub(crate) fn color(&self, value: f64) -> Result<OpaqueColor, Error> {
        if let Some((center, neutral)) = self.neutral {
            if value.to_bits() == center.to_bits() || (value == 0.0 && center == 0.0) {
                return Ok(neutral);
            }
        }
        let fraction = normalized_fraction(self.domain(), value)?;
        for pair in self.stops().windows(2) {
            if fraction <= pair[1].offset() {
                let local = (fraction - pair[0].offset()) / (pair[1].offset() - pair[0].offset());
                let rgb = super::color::interpolate(
                    pair[0].color().as_rgb(),
                    pair[1].color().as_rgb(),
                    local.clamp(0.0, 1.0),
                )?;
                return Ok(OpaqueColor::rgb(rgb.0, rgb.1, rgb.2));
            }
        }
        Err(Error::Render(
            "resolved continuous stops omit the color value".into(),
        ))
    }

    /// Freeze a backend-interpolated gradient that contains every representable
    /// observed mark color as an exact stop, under the backend's six-decimal
    /// offset spelling. Distinct colors at one spelled position must refuse.
    pub(crate) fn guide_stops(&self, observed: &[f64]) -> Result<Vec<GradientStop>, Error> {
        use crate::render::coordinates::{CanonicalCoordinate, CoordinatePrecision};

        let domain = self.domain();
        let constant = domain.0.total_cmp(&domain.1).is_eq();
        let midpoint = constant.then(|| self.color(domain.0)).transpose()?;
        let capacity = if constant {
            2
        } else {
            self.stops()
                .len()
                .checked_add(observed.len())
                .ok_or_else(|| Error::Render("continuous guide stop count overflowed".into()))?
        };
        let mut candidates = Vec::new();
        crate::data::source::reserve_source(&mut candidates, capacity)?;
        let mut push = |stop: GradientStop| -> Result<(), Error> {
            let canonical = CanonicalCoordinate::from_f64(
                stop.offset,
                CoordinatePrecision::Millionths,
                "continuous guide stop offset",
            )?;
            candidates.push((
                canonical.units(),
                GradientStop::new(canonical.value(), stop.color),
            ));
            Ok(())
        };
        if let Some(color) = midpoint {
            push(GradientStop::new(0.0, color.as_rgb()))?;
            push(GradientStop::new(1.0, color.as_rgb()))?;
        } else {
            for stop in self.stops() {
                push(stop.backend_stop())?;
            }
            for &value in observed {
                let offset = normalized_fraction(domain, value)?;
                push(GradientStop::new(offset, self.color(value)?.as_rgb()))?;
            }
        }
        crate::data::source::sort_source_by(&mut candidates, |left, right| left.0.cmp(&right.0));
        let mut selected = Vec::new();
        crate::data::source::reserve_source(&mut selected, candidates.len())?;
        let mut previous = None;
        for (units, stop) in candidates {
            if let Some((last_units, last_color)) = previous {
                if last_units == units {
                    if last_color != stop.color {
                        return Err(Error::Data(
                            "distinct continuous mark colors collapse at one serialized guide offset".into(),
                        ));
                    }
                    continue;
                }
            }
            previous = Some((units, stop.color));
            selected.push(stop);
        }
        Ok(selected)
    }
}

fn reserve<T>(values: &mut Vec<T>, count: usize) -> Result<(), Error> {
    values
        .try_reserve_exact(count)
        .map_err(|_| Error::Render("cannot reserve resolved color map".into()))
}