Crate base16cs

Source
Expand description

§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.

§Examples

§Defining a base palette and deriving computed color values

use base16cs::{Palette, BaseColor, DerivedPalette};

// 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);
assert_eq!(derived_palette.name, "My Palette");

let derived_colors = &derived_palette.colors;
assert_eq!(derived_colors[0].base, &palette.colors[0]);
assert_eq!(derived_colors[1].base, &palette.colors[1]);

assert_eq!(derived_colors[0].srgb_hex, "fef3da");
assert_eq!(derived_colors[1].srgb_hex, "384c52");

§Serializing and deserializing a palette (YAML)

This crate is compiled with YAML serde by default.

use base16cs::Serializable;

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
"#);

Deserialization is always done from a base palette, since its derived form can be re-computed at runtime.

let yaml_str = r#"
name: My Palette
colors:
- name: bg
  lab:
    l: 96.0
    a: 0.0
    b: 13.0
- name: fg
  lab:
    l: 31.0
    a: -6.0
    b: -6.0
"#;

let de_palette = Palette::<2>::from_yaml(yaml_str).unwrap();
assert_eq!(de_palette, palette);

§Injecting a palette into a template for render

See: template module.

Modules§

liquid
yaml

Structs§

BaseColor
A base color in its canonical form.
DerivedColor
A color with derived forms (sRGB values derived from its canonical CIE Lab).
DerivedPalette
Like Palette, a DerivedPalette contains an array of DerivedColors.
Palette
A palette is a collection of base colors (in their canonical forms only).
RenderOptions
Rendering options.

Traits§

PaletteRenderer
A trait for an object that can render itself given an N-color palette.
Serializable
A trait for an object that can serialize and deserialize to and from a utf-8 string.

Type Aliases§

Base16Colors
Base16DerivedColors
Base16DerivedPalette
Base16Palette
A palette with 16 base colors.