Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add various conversions to Color #2166

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
}