Add util module
This commit is contained in:
parent
2a174de7bd
commit
618d6ac4e9
3 changed files with 34 additions and 6 deletions
11
src/bw.rs
11
src/bw.rs
|
|
@ -1,6 +1,8 @@
|
||||||
use image::RgbaImage;
|
use image::RgbaImage;
|
||||||
use palette::{Hsl, Hsv, IntoColor, Lab, LinSrgb, Oklab, Srgb};
|
use palette::{Hsl, Hsv, IntoColor, Lab, LinSrgb, Oklab, Srgb};
|
||||||
|
|
||||||
|
use crate::util;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum Method {
|
pub enum Method {
|
||||||
SrgbAverage,
|
SrgbAverage,
|
||||||
|
|
@ -51,11 +53,8 @@ impl Method {
|
||||||
|
|
||||||
pub fn bw(image: &mut RgbaImage, method: Method) {
|
pub fn bw(image: &mut RgbaImage, method: Method) {
|
||||||
for pixel in image.pixels_mut() {
|
for pixel in image.pixels_mut() {
|
||||||
let [r, g, b, _] = pixel.0;
|
let srgb = util::pixel_to_srgb(*pixel);
|
||||||
let srgb = Srgb::new(r, g, b).into_format::<f32>();
|
let srgb = method.to_bw(srgb);
|
||||||
let srgb = method.to_bw(srgb).into_format::<u8>();
|
util::update_pixel_with_srgb(pixel, srgb);
|
||||||
pixel.0[0] = srgb.red;
|
|
||||||
pixel.0[1] = srgb.green;
|
|
||||||
pixel.0[2] = srgb.blue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,4 @@
|
||||||
#![warn(clippy::use_self)]
|
#![warn(clippy::use_self)]
|
||||||
|
|
||||||
pub mod bw;
|
pub mod bw;
|
||||||
|
mod util;
|
||||||
|
|
|
||||||
28
src/util.rs
Normal file
28
src/util.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
use image::Rgba;
|
||||||
|
use palette::{IntoColor, Srgb};
|
||||||
|
|
||||||
|
pub fn pixel_to_srgb(pixel: Rgba<u8>) -> Srgb {
|
||||||
|
let [r, g, b, _] = pixel.0;
|
||||||
|
Srgb::new(r, g, b).into_format::<f32>()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_pixel_with_srgb(pixel: &mut Rgba<u8>, srgb: Srgb) {
|
||||||
|
let srgb = srgb.into_format::<u8>();
|
||||||
|
pixel.0[0] = srgb.red;
|
||||||
|
pixel.0[1] = srgb.green;
|
||||||
|
pixel.0[2] = srgb.blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pixel_to_color<C>(pixel: Rgba<u8>) -> C
|
||||||
|
where
|
||||||
|
Srgb: IntoColor<C>,
|
||||||
|
{
|
||||||
|
pixel_to_srgb(pixel).into_color()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_pixel_with_color<C>(pixel: &mut Rgba<u8>, color: C)
|
||||||
|
where
|
||||||
|
C: IntoColor<Srgb>,
|
||||||
|
{
|
||||||
|
update_pixel_with_srgb(pixel, color.into_color())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue