From 87b2f96a5a4dea9b105e845d17d76a9bf95c7993 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 17 Jan 2024 10:27:09 +0100 Subject: [PATCH] Add various conversions to `Color` --- crates/fj-interop/src/color.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/fj-interop/src/color.rs b/crates/fj-interop/src/color.rs index 141c55267..dd3d8b8b0 100644 --- a/crates/fj-interop/src/color.rs +++ b/crates/fj-interop/src/color.rs @@ -8,3 +8,33 @@ impl Default for Color { 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.]) + } +}