colorgram::extract now accepts &[u8] instead of Path

This commit is contained in:
2026-01-06 05:17:23 +03:00
parent 3dfdf0a91f
commit 654a35bc1d
2 changed files with 6 additions and 7 deletions
+2 -6
View File
@@ -2,7 +2,6 @@ use image;
use std::{ use std::{
error::Error, error::Error,
fmt::{Display, Formatter, Result as fmtResult}, fmt::{Display, Formatter, Result as fmtResult},
path::Path,
}; };
pub struct Hsl { pub struct Hsl {
@@ -78,11 +77,8 @@ fn rgb_to_hsl(rgb: &Rgb) -> Hsl {
Hsl { h, s, l: l as u8 } Hsl { h, s, l: l as u8 }
} }
pub fn extract<P: AsRef<Path>>( pub fn extract(buffer: &[u8], number_of_color: usize) -> Result<Vec<Color>, Box<dyn Error>> {
path: P, let img = image::load_from_memory(buffer)?;
number_of_color: usize,
) -> Result<Vec<Color>, Box<dyn Error>> {
let img = image::open(path)?;
let img = img.to_rgb8(); let img = img.to_rgb8();
let mut samples = vec![0u32; 4 * 4096]; let mut samples = vec![0u32; 4 * 4096];
+4 -1
View File
@@ -28,7 +28,10 @@ fn main() {
assert!(input_path.is_file(), "Input path is not a file"); assert!(input_path.is_file(), "Input path is not a file");
assert!(colors_amount > 0, "Colors amount must be greater than zero"); assert!(colors_amount > 0, "Colors amount must be greater than zero");
match extract(input_path, colors_amount) { let img = image::open(input_path).unwrap();
let buf = img.as_bytes();
match extract(buf, colors_amount) {
Ok(colors) => { Ok(colors) => {
for color in colors { for color in colors {
let style = Style::new() let style = Style::new()