- add colormap from string to rust code

This commit is contained in:
Wim Pomp
2025-08-23 23:13:57 +02:00
parent 067368e06c
commit 7ea0d1c093
5 changed files with 38 additions and 53 deletions

View File

@@ -3,6 +3,8 @@ mod py;
use anyhow::{Result, anyhow};
use chrono::Utc;
use colorcet::ColorMap;
use colorgrad::{Gradient, LinearGradient};
use css_color::Srgb;
use flate2::write::ZlibEncoder;
use ndarray::{ArcArray2, AsArray, Ix2, s};
@@ -709,11 +711,12 @@ impl IJTiffFile {
}
/// set colors from css color names and #C01085
pub fn set_colors(&mut self, colors: &[String]) -> Result<()> {
pub fn set_colors<S: AsRef<str>>(&mut self, colors: &[S]) -> Result<()> {
self.colors = Colors::Colors(
colors
.iter()
.map(|c| {
let c = c.as_ref();
let lc = c.to_lowercase();
let c = match lc.as_str() {
"r" => "#ff0000",
@@ -740,6 +743,28 @@ impl IJTiffFile {
Ok(())
}
pub fn set_colormap<S: AsRef<str>>(&mut self, name: S) -> Result<()> {
let name = name.as_ref();
let colormap: LinearGradient = name.parse::<ColorMap>()?.try_into()?;
let mut colormap = colormap
.colors(256)
.into_iter()
.map(|c| {
vec![
(c.r * 255.0).round() as u8,
(c.g * 255.0).round() as u8,
(c.b * 255.0).round() as u8,
]
})
.collect::<Vec<_>>();
if name.starts_with("glasbey") || name.ends_with("glasbey") {
colormap[0] = vec![255, 255, 255];
colormap[255] = vec![0, 0, 0];
}
self.colors = Colors::Colormap(colormap);
Ok(())
}
/// to be saved in description tag (270)
pub fn description(&self, c_size: usize, z_size: usize, t_size: usize) -> String {
let mut desc: String = String::from("ImageJ=1.11a");