Crates.io | base16cs |
lib.rs | base16cs |
version | 0.3.0 |
source | src |
created_at | 2024-05-12 21:26:51.242582 |
updated_at | 2024-05-23 02:47:41.65511 |
description | A base16 color palette template renderer |
homepage | https://github.com/santoso-wijaya/base16cs |
repository | https://github.com/santoso-wijaya/base16cs |
max_upload_size | |
id | 1237782 |
size | 70,644 |
base16cs
A library for defining a palette of base colors in canonical CIE L*a*b* colorspace values, and then deriving other colorspace values from them.
This library also provides serializers and deserializers for palettes, and a template renderer (Liquid, by default) for injecting palette and color variables into a template for renders.
use base16cs::{Palette, BaseColor, DerivedPalette};
use base16cs::Serializable;
// Define a canonical palette
let palette = Palette::new(
"My Palette",
[
BaseColor::new("bg", 96, 0, 13),
BaseColor::new("fg", 31, -6, -6),
]);
// Derive with computed sRGB values
let derived_palette = DerivedPalette::from(&palette);
// Serialize (to YAML)
let serialized = derived_palette.serialize().unwrap();
assert_eq!(serialized, r#"name: My Palette
colors:
- base:
name: bg
lab:
l: 96.0
a: 0.0
b: 13.0
srgb:
red: 254
green: 243
blue: 218
srgb_hex: fef3da
- base:
name: fg
lab:
l: 31.0
a: -6.0
b: -6.0
srgb:
red: 56
green: 76
blue: 82
srgb_hex: 384c52
"#);
use base16cs::{Base16Palette, Base16DerivedPalette};
use base16cs::LiquidTemplate;
let palette_yaml = std::fs::read_to_string("/path/to/palette.yaml").unwrap();
let palette = Base16Palette::from_yaml(&palette_yaml).unwrap();
let template = LiquidTemplate::parse_file("/path/to/template.liquid", vec![]).unwrap();
// `template.render()` will take care of deriving `palette` for us.
println!(template.render(&palette, false));
When a Liquid template is rendered, it will be injected with a Liquid object
keyed to "palette"
. The Liquid object value is a serialization of a
DerivedPalette
.
Let's say /path/to/template.liquid
contains:
Palette name: {{ palette.name }}
Palette colors:
{%- for color in palette.colors -%}
{{ color.base.name }}: #{{ color.srgb_hex }}
{%- endfor -%}
Rendering this with the palette above would result in:
Palette name: My Palette
Palette colors:
bg: #fef3da