Switch to sRGBA everywhere
This commit is contained in:
parent
859fdb4bfe
commit
1e73c01845
6 changed files with 52 additions and 44 deletions
|
|
@ -1,21 +1,21 @@
|
|||
use palette::Srgb;
|
||||
use palette::Srgba;
|
||||
|
||||
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 from_image_color(color: image::Rgba<u8>) -> Srgba {
|
||||
let [r, g, b, a] = color.0;
|
||||
Srgba::new(r, g, b, a).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 to_image_color(color: Srgba) -> image::Rgba<u8> {
|
||||
let color = color.into_format::<u8, u8>();
|
||||
image::Rgba([color.red, color.green, color.blue, color.alpha])
|
||||
}
|
||||
|
||||
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 from_text_color(color: cosmic_text::Color) -> Srgba {
|
||||
let [r, g, b, a] = color.as_rgba();
|
||||
Srgba::new(r, g, b, a).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)
|
||||
pub fn to_text_color(color: Srgba) -> cosmic_text::Color {
|
||||
let color = color.into_format::<u8, u8>();
|
||||
cosmic_text::Color::rgba(color.red, color.green, color.blue, color.alpha)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use image::RgbImage;
|
||||
use palette::Srgb;
|
||||
use image::RgbaImage;
|
||||
use palette::Srgba;
|
||||
use taffy::{AvailableSpace, NodeId, Point, Size, TaffyResult, TaffyTree};
|
||||
|
||||
use crate::{color, BoxedWidget, Rect, Vec2, View};
|
||||
|
|
@ -14,11 +14,11 @@ fn size_to_vec2(size: Size<f32>) -> Vec2 {
|
|||
|
||||
pub struct Tree<C> {
|
||||
tree: TaffyTree<BoxedWidget<C>>,
|
||||
background: Srgb,
|
||||
background: Srgba,
|
||||
}
|
||||
|
||||
impl<C> Tree<C> {
|
||||
pub fn new(background: Srgb) -> Self {
|
||||
pub fn new(background: Srgba) -> Self {
|
||||
Self {
|
||||
tree: TaffyTree::new(),
|
||||
background,
|
||||
|
|
@ -88,7 +88,7 @@ impl<C> Tree<C> {
|
|||
ctx: &mut C,
|
||||
root: NodeId,
|
||||
available: Size<AvailableSpace>,
|
||||
) -> anyhow::Result<RgbImage> {
|
||||
) -> anyhow::Result<RgbaImage> {
|
||||
self.layout(ctx, root, available)?;
|
||||
|
||||
let layout = self.tree.layout(root)?;
|
||||
|
|
@ -97,7 +97,8 @@ impl<C> Tree<C> {
|
|||
// TODO Check how taffy treats the border?
|
||||
|
||||
let (width, height) = size_to_vec2(layout.size).to_u32();
|
||||
let mut image = RgbImage::from_pixel(width, height, color::to_image_rgb(self.background));
|
||||
let mut image =
|
||||
RgbaImage::from_pixel(width, height, color::to_image_color(self.background));
|
||||
self.render_to_view(ctx, root, &mut View::new(&mut image))?;
|
||||
|
||||
Ok(image)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
use image::RgbImage;
|
||||
use palette::Srgb;
|
||||
use image::RgbaImage;
|
||||
use palette::{blend::Compose, Srgba};
|
||||
|
||||
use crate::{color, Rect, Vec2};
|
||||
|
||||
pub struct View<'a> {
|
||||
area: Rect,
|
||||
buffer: &'a mut RgbImage,
|
||||
buffer: &'a mut RgbaImage,
|
||||
}
|
||||
|
||||
impl<'a> View<'a> {
|
||||
pub fn new(buffer: &'a mut RgbImage) -> Self {
|
||||
pub fn new(buffer: &'a mut RgbaImage) -> Self {
|
||||
let size = Vec2::from_u32(buffer.width(), buffer.height());
|
||||
let area = Rect::from_nw(Vec2::ZERO, size);
|
||||
Self { area, buffer }
|
||||
|
|
@ -35,22 +35,30 @@ impl<'a> View<'a> {
|
|||
pos + self.area.corner_nw()
|
||||
}
|
||||
|
||||
pub fn get(&self, pos: Vec2) -> Option<Srgb> {
|
||||
pub fn get(&self, pos: Vec2) -> Option<Srgba> {
|
||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
||||
let pixel = self.buffer.get_pixel_checked(x, y)?;
|
||||
Some(color::from_image_rgb(*pixel))
|
||||
Some(color::from_image_color(*pixel))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, pos: Vec2, color: Srgb) {
|
||||
pub fn set(&mut self, pos: Vec2, color: Srgba) {
|
||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
||||
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
|
||||
*pixel = color::to_image_rgb(color);
|
||||
let below = color::from_image_color(*pixel);
|
||||
*pixel = color::to_image_color(color.atop(below));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replace(&mut self, pos: Vec2, color: Srgba) {
|
||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
||||
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
|
||||
*pixel = color::to_image_color(color);
|
||||
}
|
||||
}
|
||||
|
||||
// More complicated drawing primitives
|
||||
|
||||
pub fn rect(&mut self, area: Rect, color: Srgb) {
|
||||
pub fn rect(&mut self, area: Rect, color: Srgba) {
|
||||
let nw = area.corner_nw();
|
||||
let se = area.corner_se();
|
||||
for y in nw.y..=se.y {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, Shaping, SwashCache};
|
||||
use palette::Srgb;
|
||||
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
|
||||
use palette::Srgba;
|
||||
use taffy::prelude::{AvailableSpace, Size};
|
||||
|
||||
use crate::{color, Rect, Vec2, View, Widget};
|
||||
|
|
@ -32,12 +32,12 @@ pub trait HasFontStuff {
|
|||
|
||||
pub struct Text {
|
||||
buffer: Buffer,
|
||||
color: Srgb,
|
||||
color: Srgba,
|
||||
}
|
||||
|
||||
impl Text {
|
||||
/// Default text color.
|
||||
const COLOR: Srgb = Srgb::new(0.0, 0.0, 0.0);
|
||||
const COLOR: Srgba = Srgba::new(0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
// Default shaping strategy.
|
||||
const SHAPING: Shaping = Shaping::Advanced;
|
||||
|
|
@ -81,7 +81,7 @@ impl Text {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn color(mut self, color: Srgb) -> Self {
|
||||
pub fn color(mut self, color: Srgba) -> Self {
|
||||
self.color = color;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue