coloremetry 0.1.0

small color library written in Rust
Documentation
/*
 *    Copyright 2025 Jared Davis
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

use crate::xyz::{Illumination, XYZ};
pub mod rgb;
mod xyz;
pub mod lab;
pub mod palette;
pub mod lch;
pub mod norm;

#[macro_export]
macro_rules! rgb {
    ($r:expr, $g:expr, $b:expr) => {
        RGB{
            r: $r,
            g: $g,
            b: $b,
        }
    };
}

#[macro_export]
macro_rules! lab {
    ($l:expr, $a:expr, $b:expr) => {
        Lab{
            l: $l,
            a: $a,
            b: $b,
        }
    };
}

#[macro_export]
macro_rules! rgb2lab {
    ($rgb:expr, $illuminant:expr) => {
        rgb!($rgb[0], $rgb[1], $rgb[2]).lab($illuminant);
    };
}

#[macro_export]
macro_rules! hex2rgb {
    ($hex:expr) => {
        RGB::from_hex($hex)
    };
}

macro_rules! illumination {
    ($xyz:expr, $rgb:expr, $reference:expr) => {
        Illumination {
            x: $xyz[0],
            y: $xyz[1],
            z: $xyz[2],
            r: $rgb[0],
            g: $rgb[1],
            b: $rgb[2],
            reference: XYZ{
                x: $reference[0],
                y: $reference[1],
                z: $reference[2],
            },
        }
    };
}

// D65 "Daylight" Illumination transform values from Bruce Lindbloom
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
pub const D65: Illumination = illumination!(
    [[0.4124564, 0.3575761, 0.1804375],
     [0.2126729, 0.7151522, 0.0721750],
     [0.0193339, 0.1191920, 0.9503041]],

    [[3.2404542, -1.5371385, -0.4985314],
     [-0.9692660, 1.8760108, 0.0415560],
     [0.0556434, -0.2040259, 1.0572252]],

    [1.05211106084, 1.0, 0.9184170164]
);

#[cfg(test)]
mod tests {
    use crate::lab::Lab;
    use crate::rgb::RGB;
    use crate::D65;

    #[test]
    fn it_parses_hex_colors() {
        assert_eq!(hex2rgb!("#FFFFFF"), Ok(rgb!(255.0, 255.0, 255.0)));
        assert_eq!(hex2rgb!("#000000"), Ok(rgb!(0.0, 0.0, 0.0)));
        assert_eq!(hex2rgb!("#FF0000"), Ok(rgb!(255.0, 0.0, 0.0)));
        assert_eq!(hex2rgb!("#00FF00"), Ok(rgb!(0.0, 255.0, 0.0)));
        assert_eq!(hex2rgb!("#FFFF00"), Ok(rgb!(255.0, 255.0, 0.0)));
        assert_eq!(hex2rgb!("#FF00FF"), Ok(rgb!(255.0, 0.0, 255.0)));
        assert_eq!(hex2rgb!("#0000FF"), Ok(rgb!(0.0, 0.0, 255.0)));
        assert_eq!(hex2rgb!("#808080"), Ok(rgb!(128.0, 128.0, 128.0)));
        assert_eq!(hex2rgb!("#123456"), Ok(rgb!(18.0, 52.0, 86.0)));
        assert_eq!(hex2rgb!("#ABCDEF"), Ok(rgb!(171.0, 205.0, 239.0)));
        assert_eq!(hex2rgb!("#FEA724"), Ok(rgb!(254.0, 167.0, 36.0)));
        assert_eq!(hex2rgb!("#E5E5E5"), Ok(rgb!(229.0, 229.0, 229.0)));
        assert_eq!(hex2rgb!("#7F11B2"), Ok(rgb!(127.0, 17.0, 178.0)));
    }

    macro_rules! assert_lab_eq {
        ($result:expr, $expect:expr) => {
            assert!($result.l - $expect.l <= 1e-2);
            assert!($result.a - $expect.a <= 1e-2);
            assert!($result.b - $expect.b <= 1e-2);
        };
    }

    #[test]
    fn it_converts_rgb_to_lab() {
        assert_lab_eq!(rgb2lab!([0.0, 0.0, 0.0], D65), lab!(0.0, 0.0, 0.0));
        assert_lab_eq!(rgb2lab!([255.0, 0.0, 0.0], D65), lab!(53.24, 80.09, 67.20));
        assert_lab_eq!(rgb2lab!([255.0, 255.0, 0.0], D65), lab!(97.14, -21.55, 94.48));
        assert_lab_eq!(rgb2lab!([0.0, 255.0, 0.0], D65), lab!(87.74, -86.18, 83.18));
        assert_lab_eq!(rgb2lab!([0.0, 255.0, 255.0], D65), lab!(91.11, -48.09, -14.13));
        assert_lab_eq!(rgb2lab!([0.0, 0.0, 255.0], D65), lab!(32.30, 79.19, -107.86));
        assert_lab_eq!(rgb2lab!([255.0, 0.0, 255.0], D65), lab!(60.32, 98.24, -60.83));
        assert_lab_eq!(rgb2lab!([255.0, 255.0, 255.0], D65), lab!(100.0, 0.00, 0.00));
        assert_lab_eq!(rgb2lab!([127.5, 127.5, 127.5], D65), lab!(53.39, 0.00, 0.00));
        assert_lab_eq!(rgb2lab!([191.25, 0.0, 0.0], D65), lab!(39.77, 64.51, 54.13));
        assert_lab_eq!(rgb2lab!([127.5, 0.0, 0.0], D65), lab!(25.42, 47.91, 37.91));
        assert_lab_eq!(rgb2lab!([63.75, 0.0, 0.0], D65), lab!(9.66, 29.68, 15.24));
        assert_lab_eq!(rgb2lab!([255.0, 127.5, 127.5], D65), lab!(68.11, 48.39, 22.83));
    }
}