Skip to content

Commit e4c788c

Browse files
committed
Make it easier to reconstruct QuantizationResult
1 parent 1f1e11a commit e4c788c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/quant.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::attr::{Attributes, ControlFlow};
22
use crate::error::*;
3-
use crate::hist::HistogramInternal;
3+
use crate::hist::{HistogramInternal, Histogram};
44
use crate::image::Image;
55
use crate::kmeans::Kmeans;
66
use crate::mediancut::mediancut;
@@ -288,6 +288,21 @@ impl QuantizationResult {
288288
pub fn palette_len(&mut self) -> usize {
289289
self.palette.len()
290290
}
291+
292+
/// Shortcut for making [`Histogram`] with `add_fixed_color`
293+
///
294+
/// Set `gamma` to `0.` for sRGB colors.
295+
pub fn from_palette(attr: &Attributes, palette: &[RGBA], gamma: f64) -> Result<Self, Error> {
296+
if palette.len() > MAX_COLORS {
297+
return Err(Unsupported);
298+
}
299+
300+
let mut hist = Histogram::new(attr);
301+
for &c in palette {
302+
hist.add_fixed_color(c, gamma)?;
303+
}
304+
hist.quantize(attr)
305+
}
291306
}
292307

293308
impl Clone for QuantizationResult {

0 commit comments

Comments
 (0)