From 58f9a508dc37f67806620f75fa20a493574a3142 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 17 Mar 2020 23:28:24 +0100 Subject: [PATCH] library: core: color: add image::Rgbconversion --- src/core/color.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/core/color.rs b/src/core/color.rs index bc0e543..4d9b3a8 100644 --- a/src/core/color.rs +++ b/src/core/color.rs @@ -67,6 +67,26 @@ impl DivAssign for LinearColor { } } +impl From for image::Rgb { + fn from(color: LinearColor) -> Self { + fn clamp(v: f32) -> f32 { + // FIXME: use clamp from nightly + if v > 1. { + 1. + } else if v < 0. { + 0. + } else { + v + } + }; + image::Rgb([ + (clamp(color.r) * 255.) as u8, + (clamp(color.g) * 255.) as u8, + (clamp(color.b) * 255.) as u8, + ]) + } +} + #[cfg(test)] mod test { use super::*;