apcach-rs 0.1.0

Rust color calculator for composing colors with consistent APCA or WCAG contrast
Documentation
  • Coverage
  • 0%
    0 out of 82 items documented0 out of 35 items with examples
  • Size
  • Source code size: 36.4 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.86 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 58s Average build duration of successful builds.
  • all releases: 58s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • iceice666/apcach-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • iceice666

apcach-rs

Rust port of apcach, a color calculator for composing colors with consistent APCA or WCAG contrast.

The crate works in OKLCH and can target both sRGB and Display-P3 output.

Install

Add the crate to Cargo.toml:

[dependencies]
apcach-rs = "0.1"

In Rust code, import it as apcach_rs.

Example

use apcach_rs::{apcach, to_css, Chroma, ColorSpace, CssFormat};

fn main() -> Result<(), apcach_rs::Error> {
    let color = apcach(70.0, Chroma::Fixed(0.15), 150.0, 100.0, ColorSpace::Srgb)?;

    assert_eq!(to_css(color, CssFormat::Hex)?.len(), 7);
    Ok(())
}

Compose Colors

White background shorthand:

use apcach_rs::{apcach, Chroma, ColorSpace};

let color = apcach(60.0, Chroma::Fixed(0.2), 145.0, 100.0, ColorSpace::Srgb)?;

Foreground on a custom background:

use apcach_rs::{apcach, cr_to_bg, Chroma, Color, ColorSpace, ContrastModel, SearchDirection};

let color = apcach(
    cr_to_bg(
        Color::srgb(0.91, 0.91, 0.91),
        60.0,
        ContrastModel::Apca,
        SearchDirection::Auto,
    ),
    Chroma::Fixed(0.2),
    145.0,
    100.0,
    ColorSpace::Srgb,
)?;

Maximum chroma search:

use apcach_rs::{apcach, cr_to_bg_white, max_chroma, ColorSpace, ContrastModel, SearchDirection};

let color = apcach(
    cr_to_bg_white(70.0, ContrastModel::Apca, SearchDirection::Auto),
    max_chroma(),
    200.0,
    100.0,
    ColorSpace::Srgb,
)?;

Convert To CSS

use apcach_rs::{apcach, to_css, Chroma, ColorSpace, CssFormat};

let color = apcach(70.0, Chroma::Fixed(0.15), 150.0, 100.0, ColorSpace::Srgb)?;

let oklch = to_css(color, CssFormat::Oklch)?;
let hex = to_css(color, CssFormat::Hex)?;
let rgb = to_css(color, CssFormat::Rgb)?;
let p3 = to_css(color, CssFormat::P3)?;

Supported CSS output formats:

  • CssFormat::Oklch
  • CssFormat::Rgb
  • CssFormat::Hex
  • CssFormat::P3
  • CssFormat::FigmaP3

API Overview

Core composition and conversion:

  • apcach
  • calc_contrast
  • to_css

Contrast configuration helpers:

  • cr_to_bg
  • cr_to_bg_white
  • cr_to_bg_black
  • cr_to_fg
  • cr_to_fg_white
  • cr_to_fg_black

Color adjustment helpers:

  • set_contrast
  • map_contrast
  • set_chroma
  • map_chroma
  • set_hue
  • map_hue

Chroma helpers:

  • max_chroma
  • max_chroma_capped

Key public types:

  • ApcachColor
  • Chroma
  • Color
  • ColorSpace
  • ContrastConfig
  • ContrastInput
  • ContrastModel
  • CssFormat
  • Error
  • SearchDirection

License

MIT