use crate::object::ObjectId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorSpaceFamily {
DeviceGray,
DeviceRgb,
DeviceCmyk,
CalGray,
CalRgb,
Lab,
IccBased,
Indexed,
Separation,
DeviceN,
Pattern,
}
impl ColorSpaceFamily {
pub fn component_count(self) -> Option<u8> {
match self {
ColorSpaceFamily::DeviceGray | ColorSpaceFamily::CalGray => Some(1),
ColorSpaceFamily::DeviceRgb | ColorSpaceFamily::CalRgb | ColorSpaceFamily::Lab => {
Some(3)
}
ColorSpaceFamily::DeviceCmyk => Some(4),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct ColorSpaceDesc {
pub family: ColorSpaceFamily,
pub components: u8,
pub object: Option<ObjectId>,
}
pub(crate) mod cie;
pub mod cmyk;
pub mod icc;
pub fn gray_to_rgb(g: f32) -> [f32; 3] {
[g, g, g]
}
pub fn lab_to_rgb(l: f32, a: f32, b: f32, white_point: [f32; 3], range: [f32; 4]) -> [f32; 3] {
let _ = white_point;
let (a_lo, a_hi) = ordered(cie::sanitize(range[0]), cie::sanitize(range[1]));
let (b_lo, b_hi) = ordered(cie::sanitize(range[2]), cie::sanitize(range[3]));
let l = cie::sanitize(l).clamp(0.0, 100.0);
let a = cie::sanitize(a).clamp(a_lo, a_hi);
let b = cie::sanitize(b).clamp(b_lo, b_hi);
let m = (l + 16.0) / 116.0;
let ll = m + a / 500.0;
let n = m - b / 200.0;
let x = if ll < 0.2069 {
0.957 * 0.12842 * (ll - 0.1379)
} else {
0.957 * ll * ll * ll
};
let y = if m < 0.2069 {
0.12842 * (m - 0.1379)
} else {
m * m * m
};
let z = if n < 0.2069 {
1.0889 * 0.12842 * (n - 0.1379)
} else {
1.0889 * n * n * n
};
cie::xyz_to_srgb(x, y, z)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CalRgbTransform {
gamma: [f32; 3],
matrix: [f32; 9],
adaptation: [f32; 9],
}
impl CalRgbTransform {
pub fn new(white_point: [f32; 3], gamma: [f32; 3], matrix: [f32; 9]) -> Self {
Self {
gamma: gamma.map(|x| if x.is_finite() { x } else { 1.0 }),
matrix,
adaptation: cie::white_point_adaptation_matrix(
white_point[0],
white_point[1],
white_point[2],
),
}
}
pub fn apply(&self, abc: [f32; 3]) -> [f32; 3] {
let a = cie::sanitize(abc[0]).powf(self.gamma[0]);
let b = cie::sanitize(abc[1]).powf(self.gamma[1]);
let c = cie::sanitize(abc[2]).powf(self.gamma[2]);
let m = self.matrix;
let x = m[0] * a + m[3] * b + m[6] * c;
let y = m[1] * a + m[4] * b + m[7] * c;
let z = m[2] * a + m[5] * b + m[8] * c;
cie::xyz_to_srgb_with_adaptation(x, y, z, self.adaptation)
}
}
pub fn calrgb_to_rgb(
abc: [f32; 3],
white_point: [f32; 3],
gamma: [f32; 3],
matrix: [f32; 9],
) -> [f32; 3] {
CalRgbTransform::new(white_point, gamma, matrix).apply(abc)
}
pub fn calgray_to_gray(a: f32, gamma: f32) -> f32 {
let _ = gamma;
cie::sanitize(a).clamp(0.0, 1.0)
}
#[inline]
fn ordered(lo: f32, hi: f32) -> (f32, f32) {
if lo <= hi { (lo, hi) } else { (hi, lo) }
}
pub fn cmyk_to_rgb(c: f32, m: f32, y: f32, k: f32) -> [f32; 3] {
cmyk::adobe_cmyk_to_srgb(c, m, y, k)
}
#[derive(Debug, Clone, thiserror::Error)]
pub enum ColorError {
#[error("unknown color space family: {0}")]
UnknownFamily(String),
#[error("malformed color space definition: {0}")]
Malformed(&'static str),
#[error("component count {found} invalid for {family:?}")]
BadComponentCount { family: ColorSpaceFamily, found: u8 },
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn cmyk_policy_is_the_measured_table() {
let px = |v: [f32; 3]| v.map(|x| (x * 255.0).round() as i32);
assert_eq!(
px(cmyk_to_rgb(0.0, 0.0, 0.0, 0.0)),
[255, 255, 255],
"no ink → paper"
);
assert_eq!(
px(cmyk_to_rgb(0.0, 0.0, 0.0, 1.0)),
[35, 31, 32],
"full K is measured ink"
);
let cyan = px(cmyk_to_rgb(1.0, 0.0, 0.0, 0.0));
assert!(
cyan[0] < 60 && cyan[2] > 180,
"cyan is an ink, not [0,255,255]: {cyan:?}"
);
let magenta = px(cmyk_to_rgb(0.0, 1.0, 0.0, 0.0));
assert!(magenta[0] > 180 && magenta[1] < 80, "magenta: {magenta:?}");
let yellow = px(cmyk_to_rgb(0.0, 0.0, 1.0, 0.0));
assert!(yellow[0] > 180 && yellow[2] < 90, "yellow: {yellow:?}");
assert_eq!(
cmyk_to_rgb(0.5, 0.1, 0.0, 0.2),
cmyk_to_rgb(0.5, 0.1, 0.0, 0.2)
);
}
#[test]
fn gray_policy_is_identity_triple() {
assert_eq!(gray_to_rgb(0.0), [0.0, 0.0, 0.0]);
assert_eq!(gray_to_rgb(1.0), [1.0, 1.0, 1.0]);
assert_eq!(gray_to_rgb(0.5), [0.5, 0.5, 0.5]);
}
}