Use Color in more places

This commit is contained in:
Jeremy Soller 2022-10-27 09:07:47 -06:00
parent 4edca946ea
commit 3ece9236b3
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
9 changed files with 84 additions and 53 deletions

View file

@ -6,10 +6,14 @@ pub use fontdb::{Family, Stretch, Style, Weight};
pub struct Color(pub u32);
impl Color {
/// Create new color with red, green, and blue components
#[inline]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::rgba(r, g, b, 0xFF)
}
/// Create new color with red, green, blue, and alpha components
#[inline]
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self(
((a as u32) << 24) |
@ -18,6 +22,30 @@ impl Color {
(b as u32)
)
}
/// Get the red component
#[inline]
pub fn r(&self) -> u8 {
((self.0 & 0x00FF0000) >> 16) as u8
}
/// Get the green component
#[inline]
pub fn g(&self) -> u8 {
((self.0 & 0x0000FF00) >> 8) as u8
}
/// Get the blue component
#[inline]
pub fn b(&self) -> u8 {
(self.0 & 0x000000FF) as u8
}
/// Get the alpha component
#[inline]
pub fn a(&self) -> u8 {
((self.0 & 0xFF000000) >> 24) as u8
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]

View file

@ -7,7 +7,7 @@ use std::{
};
use unicode_segmentation::UnicodeSegmentation;
use crate::{Attrs, AttrsList, FontSystem, LayoutGlyph, LayoutLine, ShapeLine};
use crate::{Attrs, AttrsList, Color, FontSystem, LayoutGlyph, LayoutLine, ShapeLine};
/// An action to perform on a [TextBuffer]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@ -963,8 +963,8 @@ impl<'a> TextBuffer<'a> {
/// Draw the buffer
#[cfg(feature = "swash")]
pub fn draw<F>(&self, cache: &mut crate::SwashCache, color: u32, mut f: F)
where F: FnMut(i32, i32, u32, u32, u32)
pub fn draw<F>(&self, cache: &mut crate::SwashCache, color: Color, mut f: F)
where F: FnMut(i32, i32, u32, u32, Color)
{
let font_size = self.metrics.font_size;
let line_height = self.metrics.line_height;
@ -1054,7 +1054,7 @@ impl<'a> TextBuffer<'a> {
line_y - font_size,
cmp::max(0, max - min) as u32,
line_height as u32,
0x33_00_00_00 | (color & 0xFF_FF_FF)
Color::rgba(color.r(), color.g(), color.b(), 0x33)
);
}
c_x += c_w;
@ -1080,7 +1080,7 @@ impl<'a> TextBuffer<'a> {
line_y - font_size,
cmp::max(0, max - min) as u32,
line_height as u32,
0x33_00_00_00 | (color & 0xFF_FF_FF)
Color::rgba(color.r(), color.g(), color.b(), 0x33)
);
}
}
@ -1126,7 +1126,7 @@ impl<'a> TextBuffer<'a> {
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
let glyph_color = match glyph.color_opt {
Some(some) => some.0,
Some(some) => some,
None => color,
};

View file

@ -5,7 +5,7 @@ use swash::scale::{ScaleContext, image::Content};
use swash::scale::{Render, Source, StrikeWith};
use swash::zeno::{Format, Vector};
use crate::{CacheKey, FontSystem};
use crate::{CacheKey, Color, FontSystem};
pub use swash::scale::image::{Content as SwashContent, Image as SwashImage};
@ -76,10 +76,10 @@ impl<'a> SwashCache<'a> {
}
/// Enumerate pixels in an Image, use `with_image` for better performance
pub fn with_pixels<F: FnMut(i32, i32, u32)>(
pub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self,
cache_key: CacheKey,
base: u32,
base: Color,
mut f: F
) {
if let Some(image) = self.get_image(cache_key) {
@ -92,8 +92,14 @@ impl<'a> SwashCache<'a> {
for off_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 {
//TODO: blend base alpha?
let color = (image.data[i] as u32) << 24 | base & 0xFFFFFF;
f(x + off_x, y + off_y, color);
f(
x + off_x,
y + off_y,
Color(
((image.data[i] as u32) << 24) |
base.0 & 0xFFFFFF
)
);
i += 1;
}
}
@ -103,11 +109,16 @@ impl<'a> SwashCache<'a> {
for off_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 {
//TODO: blend base alpha?
let color = (image.data[i + 3] as u32) << 24
| (image.data[i] as u32) << 16
| (image.data[i + 1] as u32) << 8
| (image.data[i + 2] as u32);
f(x + off_x, y + off_y, color);
f(
x + off_x,
y + off_y,
Color::rgba(
image.data[i + 2],
image.data[i + 1],
image.data[i],
image.data[i + 3]
)
);
i += 4;
}
}