diff --git a/crates/fj-interop/src/color.rs b/crates/fj-interop/src/color.rs new file mode 100644 index 000000000..dd3d8b8b0 --- /dev/null +++ b/crates/fj-interop/src/color.rs @@ -0,0 +1,40 @@ +/// RGBA color +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct Color(pub [u8; 4]); + +impl Default for Color { + fn default() -> Self { + // The default color is red. This is an arbitrary choice. + Self([255, 0, 0, 255]) + } +} + +impl From<[u8; 4]> for Color { + fn from(rgba: [u8; 4]) -> Self { + Self(rgba) + } +} + +impl From<[u8; 3]> for Color { + fn from([r, g, b]: [u8; 3]) -> Self { + Self([r, g, b, 255]) + } +} + +impl From<[f64; 4]> for Color { + fn from(rgba: [f64; 4]) -> Self { + let rgba = rgba.map(|value| { + let value = value.clamp(0., 1.); + let value: u8 = (value * 255.0) as u8; + value + }); + + Self(rgba) + } +} + +impl From<[f64; 3]> for Color { + fn from([r, g, b]: [f64; 3]) -> Self { + Self::from([r, g, b, 1.]) + } +} diff --git a/crates/fj-interop/src/lib.rs b/crates/fj-interop/src/lib.rs index ee5930599..ca99ea539 100644 --- a/crates/fj-interop/src/lib.rs +++ b/crates/fj-interop/src/lib.rs @@ -9,12 +9,14 @@ //! //! [Fornjot]: https://www.fornjot.app/ +mod color; mod mesh; mod model; pub mod ext; pub use self::{ - mesh::{Color, Index, Mesh, Triangle}, + color::Color, + mesh::{Index, Mesh, Triangle}, model::Model, }; diff --git a/crates/fj-interop/src/mesh.rs b/crates/fj-interop/src/mesh.rs index 8df3f1a05..032e5ef4f 100644 --- a/crates/fj-interop/src/mesh.rs +++ b/crates/fj-interop/src/mesh.rs @@ -2,6 +2,8 @@ use std::{collections::HashMap, hash::Hash}; use fj_math::Point; +use crate::Color; + /// A triangle mesh #[derive(Clone, Debug)] pub struct Mesh { @@ -116,14 +118,3 @@ pub struct Triangle { /// The color of the triangle pub color: Color, } - -/// RGBA color -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct Color(pub [u8; 4]); - -impl Default for Color { - fn default() -> Self { - // The default color is red. This is an arbitrary choice. - Self([255, 0, 0, 255]) - } -}