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.
Add the crate to Cargo.toml:
[dependencies]
apcach-rs = "0.1"In Rust code, import it as apcach_rs.
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(())
}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,
)?;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::OklchCssFormat::RgbCssFormat::HexCssFormat::P3CssFormat::FigmaP3
Core composition and conversion:
apcachcalc_contrastto_css
Contrast configuration helpers:
cr_to_bgcr_to_bg_whitecr_to_bg_blackcr_to_fgcr_to_fg_whitecr_to_fg_black
Color adjustment helpers:
set_contrastmap_contrastset_chromamap_chromaset_huemap_hue
Chroma helpers:
max_chromamax_chroma_capped
Key public types:
ApcachColorChromaColorColorSpaceContrastConfigContrastInputContrastModelCssFormatErrorSearchDirection
MIT