- use css colors to convert color names into rgb values

This commit is contained in:
Wim Pomp
2025-08-21 22:11:45 +02:00
parent b09f804a3f
commit 067368e06c
7 changed files with 149 additions and 85 deletions

View File

@@ -1,11 +1,12 @@
#[cfg(feature = "python")]
mod py;
use anyhow::Result;
use anyhow::{Result, anyhow};
use chrono::Utc;
use css_color::Srgb;
use flate2::write::ZlibEncoder;
use ndarray::{s, ArcArray2, AsArray, Ix2};
use num::{traits::ToBytes, Complex, FromPrimitive, Rational32};
use ndarray::{ArcArray2, AsArray, Ix2, s};
use num::{Complex, FromPrimitive, Rational32, traits::ToBytes};
use rayon::prelude::*;
use std::collections::HashSet;
use std::fs::{File, OpenOptions};
@@ -16,10 +17,10 @@ use std::time::Duration;
use std::{cmp::Ordering, collections::HashMap};
use std::{
thread,
thread::{available_parallelism, sleep, JoinHandle},
thread::{JoinHandle, available_parallelism, sleep},
};
use zstd::zstd_safe::CompressionLevel;
use zstd::{stream::Encoder, DEFAULT_COMPRESSION_LEVEL};
use zstd::{DEFAULT_COMPRESSION_LEVEL, stream::Encoder};
const TAG_SIZE: usize = 20;
const OFFSET_SIZE: usize = 8;
@@ -707,6 +708,38 @@ impl IJTiffFile {
self.compression = compression;
}
/// set colors from css color names and #C01085
pub fn set_colors(&mut self, colors: &[String]) -> Result<()> {
self.colors = Colors::Colors(
colors
.iter()
.map(|c| {
let lc = c.to_lowercase();
let c = match lc.as_str() {
"r" => "#ff0000",
"g" => "#008000",
"b" => "#0000ff",
"c" => "#00bfbf",
"m" => "#bf00bf",
"y" => "#bfbf00",
"k" => "#000000",
"w" => "#ffffff",
_ => c,
};
match c.parse::<Srgb>() {
Ok(c) => Ok(vec![
(255.0 * c.red).round() as u8,
(255.0 * c.green).round() as u8,
(255.0 * c.blue).round() as u8,
]),
Err(_) => Err(anyhow!("could not parse color: {}", c)),
}
})
.collect::<Result<Vec<_>>>()?,
);
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");