From e82ec74c5cc3a9e63f058580a283bf14025c4385 Mon Sep 17 00:00:00 2001 From: thomaseliot Date: Wed, 9 Aug 2023 07:46:45 -0700 Subject: [PATCH] epaint: Add `ColorImage::from_gray` (#3166) * epaint: Add from_gray_unmultiplied function * Rename to just `from_gray` --------- Co-authored-by: Emil Ernerfeldt --- crates/epaint/src/image.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/epaint/src/image.rs b/crates/epaint/src/image.rs index 35b0885c..7ca33b3a 100644 --- a/crates/epaint/src/image.rs +++ b/crates/epaint/src/image.rs @@ -110,6 +110,15 @@ impl ColorImage { Self { size, pixels } } + /// Create a [`ColorImage`] from flat opaque gray data. + /// + /// Panics if `size[0] * size[1] != gray.len()`. + pub fn from_gray(size: [usize; 2], gray: &[u8]) -> Self { + assert_eq!(size[0] * size[1], gray.len()); + let pixels = gray.iter().map(|p| Color32::from_gray(*p)).collect(); + Self { size, pixels } + } + /// A view of the underlying data as `&[u8]` #[cfg(feature = "bytemuck")] pub fn as_raw(&self) -> &[u8] {