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
use cratePalette;
use Result;
/// Rendering options.
/// A trait for an object that can render itself given an N-color palette.
///
/// A `PaletteRenderer` represents a live template that can render itself when
/// injected with a palette. This is useful for generating a colorscheme file,
/// for instance.
///
/// This crate is compiled with a Liquid template renderer implementation, by
/// default.
///
/// # Examples
///
/// ## Liquid template injected with unrolled color names from a palette
///
/// Let's say we have the following Liquid template file `selenized.md.liquid`:
///
/// ```liquid
/// # Selenized Light
///
/// | name | sRGB (#hex) |
/// |------|-------------|
/// | bg_0 | #{{ bg_0 }} |
/// | bg_1 | #{{ bg_1 }} |
/// | fg_0 | #{{ fg_0 }} |
/// | fg_1 | #{{ fg_1 }} |
/// ```
///
/// Given the following palette (in serialized yaml file):
///
/// ```yaml
/// name: Selenized light
/// colors:
/// - name: bg_0
/// lab:
/// l: 96.0
/// a: 0.0
/// b: 13.0
/// - name: bg_1
/// lab:
/// l: 91.0
/// a: 0.0
/// b: 13.0
/// - name: bg_2
/// lab:
/// l: 82.0
/// a: 0.0
/// b: 13.0
/// - name: dim_0
/// lab:
/// l: 62.0
/// a: -4.0
/// b: 1.0
/// - name: fg_0
/// lab:
/// l: 42.0
/// a: -6.0
/// b: -6.0
/// - name: fg_1
/// lab:
/// l: 31.0
/// a: -6.0
/// b: -6.0
/// ... <snip>
/// ```
///
/// We can deserialize the palette and inject it into a template for rendering:
///
/// ```rust,no_run
/// use base16cs::{Base16Palette, RenderOptions, PaletteRenderer};
/// use base16cs::liquid::LiquidTemplate;
/// # use std::path::Path;
///
/// # let yaml = "";
/// let palette = Base16Palette::from_yaml(&yaml).unwrap();
/// let template = LiquidTemplate::parse_file(Path::new("selenized.md.liquid"), Vec::new()).unwrap();
///
/// let rendered = template.render(&palette, RenderOptions { unroll_colors_hex: true }).unwrap();
///
/// assert_eq!(rendered, r#"# Selenized Light
///
/// | name | sRGB (#hex) |
/// |------|-------------|
/// | bg_0 | #fef3da |
/// | bg_1 | #f0e4cc |
/// | fg_0 | #52666d |
/// | fg_1 | #384c52 |
/// "#);
/// ```
///
/// Note that palette derivation to sRGB colorspace is handled internally by the
/// palette renderer.