Skip to content
Open
72 changes: 70 additions & 2 deletions crates/bevy_color/crates/gen_tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,67 @@ const TEST_COLORS: &[(f32, f32, f32, &str)] = &[
(0., 0.5, 0.5, "aqua"),
];

/// Pre-computed okhsl results of [`TEST_COLORS`].
/// See <https://github.com/bevyengine/bevy/pull/24137> for how this was computed.
#[expect(
clippy::excessive_precision,
reason = "The results are copied from output of script"
)]
const TEST_COLORS_OKHSL: &[[f32; 3]] = &[
[0., 0., 0.],
[0., 0., 0.9999999923961898],
[29.23388519234263, 1.0000000052082665, 0.5680846525040862],
[142.49533888780996, 0.999999970072876, 0.8445289645307816],
[264.052020638055, 0.9999999966883574, 0.3665653394260194],
[109.76923207652122, 1.0000000336324515, 0.9627043968088945],
[328.36341792345144, 0.9999999982842802, 0.6532987448472438],
[194.76894793196382, 0.9999999858415309, 0.889848301619521],
[0., 0., 0.5337598228073358],
[109.76923207652133, 1.0000004293546714, 0.5117162225399476],
[328.36341792345144, 0.9999999247891016, 0.33011042396630463],
[194.7689479319638, 0.9999998087565706, 0.4687233442820504],
[29.23388519234263, 1.0000000079893017, 0.28080442778247217],
[142.49533888780996, 0.9999999780939269, 0.4420348389571636],
[264.052020638055, 0.9999999965721178, 0.1673431798736403],
[109.76923207652133, 1.0000004293546714, 0.5117162225399476],
[328.36341792345144, 0.9999999247891016, 0.33011042396630463],
[194.7689479319638, 0.9999998087565706, 0.4687233442820504],
];

/// Pre-computed okhsv results of [`TEST_COLORS`].
/// See <https://github.com/bevyengine/bevy/pull/24137> for how this was computed.
#[expect(
clippy::excessive_precision,
reason = "The results are copied from output of script"
)]
const TEST_COLORS_OKHSV: &[[f32; 3]] = &[
[0., 0., 0.],
[0., 0., 0.9999999923961898],
[29.23388519234263, 1.0000000118548495, 1.0000000001685643],
[142.49533888780996, 0.9999998547944557, 0.9999999884428643],
[264.052020638055, 0.9999999869716024, 0.9999999646150842],
[109.76923207652122, 1.0000004272507257, 1.0000000319591922],
[328.36341792345144, 0.9999999851480317, 0.9999999978651299],
[194.76894793196382, 0.9999997954575895, 0.9999999950078031],
[0., 0., 0.5337598228073358],
[109.76923207652133, 1.0000004272507266, 0.5318634934374442],
[328.36341792345144, 0.9999999851480317, 0.5106204722384122],
[194.7689479319638, 0.9999997954575874, 0.527824149473251],
[29.23388519234263, 1.0000000118548495, 0.5022602924281349],
[142.49533888780996, 0.999999854794456, 0.525059257999103],
[264.052020638055, 0.9999999869716024, 0.4749665533554043],
[109.76923207652133, 1.0000004272507266, 0.5318634934374442],
[328.36341792345144, 0.9999999851480317, 0.5106204722384122],
[194.7689479319638, 0.9999997954575874, 0.527824149473251],
];

fn main() {
println!(
"// Generated by gen_tests. Do not edit.
#[cfg(test)]
use crate::{{Hsla, Hsva, Hwba, Srgba, LinearRgba, Oklaba, Oklcha, Laba, Lcha, Xyza}};
use crate::{{
okhsla::Okhsla, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Okhsva, Oklaba, Oklcha, Srgba, Xyza,
}};

#[cfg(test)]
pub struct TestColor {{
Expand All @@ -40,14 +96,16 @@ pub struct TestColor {{
pub oklab: Oklaba,
pub oklch: Oklcha,
pub xyz: Xyza,
pub okhsl: Okhsla,
pub okhsv: Okhsva,
}}
"
);

println!("// Table of equivalent colors in various color spaces");
println!("#[cfg(test)]");
println!("pub const TEST_COLORS: &[TestColor] = &[");
for (r, g, b, name) in TEST_COLORS {
for (i, (r, g, b, name)) in TEST_COLORS.iter().enumerate() {
let srgb = Srgb::new(*r, *g, *b);
let linear_rgb: LinSrgb = srgb.into_color();
let hsl: Hsl = srgb.into_color();
Expand All @@ -58,6 +116,8 @@ pub struct TestColor {{
let oklab: Oklab = srgb.into_color();
let oklch: Oklch = srgb.into_color();
let xyz: Xyz = srgb.into_color();
let okhsl = TEST_COLORS_OKHSL[i];
let okhsv = TEST_COLORS_OKHSV[i];
println!(" // {name}");
println!(
" TestColor {{
Expand All @@ -72,6 +132,8 @@ pub struct TestColor {{
oklab: Oklaba::new({}, {}, {}, 1.0),
oklch: Oklcha::new({}, {}, {}, 1.0),
xyz: Xyza::new({}, {}, {}, 1.0),
okhsl: Okhsla::new({}, {}, {}, 1.0),
okhsv: Okhsva::new({}, {}, {}, 1.0),
}},",
VariablePrecision(srgb.red),
VariablePrecision(srgb.green),
Expand Down Expand Up @@ -103,6 +165,12 @@ pub struct TestColor {{
VariablePrecision(xyz.x),
VariablePrecision(xyz.y),
VariablePrecision(xyz.z),
VariablePrecision(okhsl[0]),
VariablePrecision(okhsl[1]),
VariablePrecision(okhsl[2]),
VariablePrecision(okhsv[0]),
VariablePrecision(okhsv[1]),
VariablePrecision(okhsv[2]),
);
}
println!("];");
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_color/docs/diagrams/model_graph.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ flowchart LR
lRGB(<a href='https://en.wikipedia.org/wiki/Rgb'>Linear<br/>sRGB</a>)
Oklab(<a href='https://oklch.com/'>Oklab</a>)
Oklch(<a href='https://oklch.com/'>Oklch</a>)
Okhsl(<a href='https://bottosson.github.io/posts/colorpicker'>Okhsl</a>)
Okhsv(<a href='https://bottosson.github.io/posts/colorpicker'>Okhsv</a>)
XYZ(<a href='https://en.wikipedia.org/wiki/XYZ_color'>XYZ</a>)
Lab(<a href='https://en.wikipedia.org/wiki/Lab_color'>Lab</a>)
Lch(<a href='https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model'>Lch</a>)
Expand All @@ -13,10 +15,12 @@ flowchart LR
GPU <--> lRGB
lRGB <--<a href='https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab'>Conversion</a>--> Oklab
Oklab <--<a href='https://bottosson.github.io/posts/oklab/#the-oklab-color-space'>Conversion</a>--> Oklch
Oklab <--<a href='https://bottosson.github.io/posts/colorpicker'>Conversion</a>--> Okhsl
Oklab <--<a href='https://bottosson.github.io/posts/colorpicker'>Conversion</a>--> Okhsv
lRGB <--<a href='http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html'>Conversion</a>--> XYZ
XYZ <--<a href='http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_Lab.html'>Conversion</a>--> Lab
Lab <--<a href='http://www.brucelindbloom.com/index.html?Eqn_Lab_to_LCH.html'>Conversion</a>--> Lch
lRGB <--<a href='https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ'>Conversion</a>--> sRGB
sRGB <--<a href='http://alvyray.com/Papers/CG/HWB_JGTv208.pdf'>Conversion</a>--> HWB
HWB <--<a href='http://alvyray.com/Papers/CG/HWB_JGTv208.pdf'>Conversion</a>--> HSV
HSV <--<a href='https://en.wikipedia.org/wiki/HSL_and_HSV#Interconversion'>Conversion</a>--> HSL
HSV <--<a href='https://en.wikipedia.org/wiki/HSL_and_HSV#Interconversion'>Conversion</a>--> HSL
2 changes: 1 addition & 1 deletion crates/bevy_color/docs/diagrams/model_graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading