Extract color conversion functions
This commit is contained in:
parent
6ab43a8d5e
commit
c9dd8051dd
4 changed files with 28 additions and 20 deletions
21
showbits-common/src/color.rs
Normal file
21
showbits-common/src/color.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use palette::Srgb;
|
||||
|
||||
pub fn from_image_rgb(color: image::Rgb<u8>) -> Srgb {
|
||||
let [r, g, b] = color.0;
|
||||
Srgb::new(r, g, b).into_format()
|
||||
}
|
||||
|
||||
pub fn to_image_rgb(color: Srgb) -> image::Rgb<u8> {
|
||||
let color = color.into_format::<u8>();
|
||||
image::Rgb([color.red, color.green, color.blue])
|
||||
}
|
||||
|
||||
pub fn from_text_color(color: cosmic_text::Color) -> Srgb {
|
||||
let [r, g, b, _] = color.as_rgba();
|
||||
Srgb::new(r, g, b).into_format()
|
||||
}
|
||||
|
||||
pub fn to_text_color(color: Srgb) -> cosmic_text::Color {
|
||||
let color = color.into_format::<u8>();
|
||||
cosmic_text::Color::rgb(color.red, color.green, color.blue)
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
pub use crate::{node::*, rect::*, tree::*, vec2::*, view::*, widget::*};
|
||||
|
||||
pub mod color;
|
||||
mod node;
|
||||
mod rect;
|
||||
mod tree;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use image::RgbImage;
|
||||
use palette::Srgb;
|
||||
|
||||
use crate::{Rect, Vec2};
|
||||
use crate::{color, Rect, Vec2};
|
||||
|
||||
pub struct View<'a> {
|
||||
area: Rect,
|
||||
|
|
@ -38,16 +38,13 @@ impl<'a> View<'a> {
|
|||
pub fn get(&self, pos: Vec2) -> Option<Srgb> {
|
||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
||||
let pixel = self.buffer.get_pixel_checked(x, y)?;
|
||||
let [r, g, b] = pixel.0;
|
||||
let color = Srgb::new(r, g, b);
|
||||
Some(color.into_format())
|
||||
Some(color::from_image_rgb(*pixel))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, pos: Vec2, color: Srgb) {
|
||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
||||
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
|
||||
let color = color.into_format::<u8>();
|
||||
pixel.0 = [color.red, color.green, color.blue];
|
||||
*pixel = color::to_image_rgb(color);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,21 +2,10 @@ use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, Shaping, SwashCache
|
|||
use palette::Srgb;
|
||||
use taffy::prelude::{AvailableSpace, Size};
|
||||
|
||||
use crate::{Rect, Vec2, View, Widget};
|
||||
use crate::{color, Rect, Vec2, View, Widget};
|
||||
|
||||
// https://github.com/DioxusLabs/taffy/blob/main/examples/cosmic_text.rs
|
||||
|
||||
fn srgb_to_color(color: Srgb) -> Color {
|
||||
let color = color.into_format::<u8>();
|
||||
let (r, g, b) = color.into_components();
|
||||
Color::rgb(r, g, b)
|
||||
}
|
||||
|
||||
fn color_to_srgb(color: Color) -> Srgb {
|
||||
let (r, g, b, _) = color.as_rgba_tuple();
|
||||
Srgb::new(r, g, b).into_format()
|
||||
}
|
||||
|
||||
pub struct FontStuff {
|
||||
font_system: FontSystem,
|
||||
swash_cache: SwashCache,
|
||||
|
|
@ -136,9 +125,9 @@ impl<C: HasFontStuff> Widget<C> for Text {
|
|||
self.buffer.set_size(fs, size.x as f32, size.y as f32);
|
||||
self.buffer.shape_until_scroll(fs, true);
|
||||
|
||||
let color = srgb_to_color(self.color);
|
||||
let color = color::to_text_color(self.color);
|
||||
self.buffer.draw(fs, sc, color, |x, y, w, h, color| {
|
||||
let color = color_to_srgb(color);
|
||||
let color = color::from_text_color(color);
|
||||
let area = Rect::from_nw(Vec2::new(x, y), Vec2::from_u32(w, h));
|
||||
view.rect(area, color);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue