Switch to sRGBA everywhere

This commit is contained in:
Joscha 2024-03-08 17:40:17 +01:00
parent 859fdb4bfe
commit 1e73c01845
6 changed files with 52 additions and 44 deletions

View file

@ -1,7 +1,6 @@
use cosmic_text::{Attrs, Metrics};
use palette::Srgb;
use palette::Srgba;
use showbits_common::{
color,
widgets::{FontStuff, HasFontStuff, Text},
Tree, WidgetExt,
};
@ -67,7 +66,7 @@ impl Drawer {
}
fn on_test(&mut self) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(Srgb::new(1.0, 1.0, 1.0));
let mut tree = Tree::<Context>::new(Srgba::new(1.0, 1.0, 1.0, 1.0));
let root = Text::simple(
&mut self.ctx.font_stuff,

View file

@ -5,8 +5,8 @@ use escpos::{
printer::Printer as EPrinter,
utils::{PageCode, Protocol, GS},
};
use image::{Rgb, RgbImage};
use showbits_common::Tree;
use image::{Rgba, RgbaImage};
use showbits_common::{color, Tree};
use taffy::{AvailableSpace, NodeId, Size};
pub struct Printer {
@ -91,7 +91,7 @@ impl Printer {
/// https://download4.epson.biz/sec_pubs/pos/reference_en/escpos/gs_lv_0.html
fn print_image_to_printer(
printer: &mut EPrinter<FileDriver>,
image: &RgbImage,
image: &RgbaImage,
) -> anyhow::Result<()> {
assert_eq!(Self::WIDTH % 8, 0);
assert_eq!(image.width(), Self::WIDTH);
@ -122,7 +122,7 @@ impl Printer {
Ok(())
}
fn get_horizontal_byte_starting_at(image: &RgbImage, x: u32, y: u32) -> u8 {
fn get_horizontal_byte_starting_at(image: &RgbaImage, x: u32, y: u32) -> u8 {
let p7 = Self::pixel_to_bit(*image.get_pixel(x, y));
let p6 = Self::pixel_to_bit(*image.get_pixel(x + 1, y));
let p5 = Self::pixel_to_bit(*image.get_pixel(x + 2, y));
@ -148,9 +148,9 @@ impl Printer {
///
/// Instead of doing the physically accurate thing, I do what makes the most
/// sense visually.
fn pixel_to_bit(pixel: Rgb<u8>) -> bool {
let [r, g, b] = pixel.0;
let sum = (r as u32) + (g as u32) + (b as u32);
sum <= 3 * 255 / 2 // true == black
fn pixel_to_bit(pixel: Rgba<u8>) -> bool {
let color = color::from_image_color(pixel);
let avg = (color.red + color.green + color.blue) / 3.0;
avg < 0.5 // true == black
}
}