Skip to content

iceice666/apcach-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages