Expand description
§farbraum
Rust crate to convert between color spaces. “Farbraum” /ˈfarbraʊ̯m/ is German for “color space”.
Most conversion functions are ported from the culori javascript library. Some parts were modified to make the results more accurate.
§Usage
Colors are created with Color::new(), the last argument is the color space.
The .into() method is used to convert between color spaces:
use farbraum::{Color, spaces::{Srgb, LinearSrgb, Hsv}};
let hsv = Color::new(120.0, 1.0, 1.0, Hsv);
let lrgb = hsv.into(Srgb).into(LinearSrgb);
let (r, g, b) = lrgb.tuple();§Color spaces
Farbraum supports 24 color spaces:
- sRGB, Linear sRGB
- Adobe RGB (1998)
- CMY, CMYK1
- sRGB-derived color spaces (HSL, HSV, HSI, HWB)
- CIE XYZ (supports D50 and D65 illumination)
- CIELAB, CIELCh (supports D50 and D65 illumination)
- CIELUV, CIELChuv (D50 illumination)
- Oklab, Oklch
- DIN99 Lab, DIN99 LCh
- Jzazbz, JzCzhz
- Cubehelix
All color spaces use the D65 standard illuminate unless stated otherwise. CIE XYZ, CIELAB and CIELCh are available with both D50 and D65 illuminant.
1 Since CMYK has 4 components instead of 3, it must be
represented as a tuple instead of a Color. Use the {to,from}_cmyk()
methods to convert between Color<CMY> and (C, M, Y, K).
§Dynamic color spaces
The color spaces are zero-sized types, so they don’t exist at runtime. If you want to choose a color space at runtime, you’ll need to create an enum such as:
#[derive(Debug, Clone, Copy)]
enum AnyColorSpace {
CieXyzD50,
Srgb,
Hsl,
CieLabD50,
OkLab,
}This can be used instead of the farbraum’s builtin color spaces. However,
you’ll need to implement conversions for this enum to make it useful:
use farbraum::{
Color, illuminate::D50,
spaces::{Srgb, CieXyz, Hsl, CieLab, OkLab},
};
// Convert any color space to sRGB
fn any_to_srgb(any: Color<AnyColorSpace>) -> Color<Srgb> {
let (a, b, c) = any.tuple();
match any.space() {
AnyColorSpace::Srgb => Color::new(a, b, c, Srgb),
AnyColorSpace::CieXyzD50 => Color::new(a, b, c, CieXyz(D50)).into(Srgb),
AnyColorSpace::Hsl => Color::new(a, b, c, Hsl).into(Srgb),
AnyColorSpace::CieLabD50 => Color::new(a, b, c, CieLab(D50)).into(Srgb),
AnyColorSpace::OkLab => Color::new(a, b, c, OkLab).into(Srgb),
}
}§Cargo features
double-precision: Components are floating-point values, by defaultf64. If you disable thedouble-precisionfeature,f32is used instead.serde: Enable this feature to serialize and deserializeColorvalues.
§License
Dual-licensed under the Apache 2.0 and MIT license.
Modules§
- illuminate
- Contains the D50 and D65 standard illuminants.
- spaces
- Contains the color spaces.
Structs§
Traits§
- Into
- Trait for color space conversions.
Type Aliases§
- Float
- The color component type,
f64by default.