Skip to content

Commit

Permalink
Merge pull request #2166 from hannobraun/color
Browse files Browse the repository at this point in the history
Add various conversions to `Color`
  • Loading branch information
hannobraun authored Jan 17, 2024
2 parents 43f49a5 + 87b2f96 commit a9c15d9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
40 changes: 40 additions & 0 deletions crates/fj-interop/src/color.rs
Original file line number Diff line number Diff line change
@@ -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.])
}
}
4 changes: 3 additions & 1 deletion crates/fj-interop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
13 changes: 2 additions & 11 deletions crates/fj-interop/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V> {
Expand Down Expand Up @@ -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])
}
}

0 comments on commit a9c15d9

Please sign in to comment.