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

@ -29,19 +29,6 @@ pub struct Appearance {
text_color: Color, text_color: Color,
} }
impl Appearance {
fn text_color_u32(&self) -> u32 {
let channel = |f: f32, shift: i32| -> u32 {
(cmp::max(0, cmp::min(255, (f * 255.0) as i32)) << shift) as u32
};
channel(self.text_color.b, 0) |
channel(self.text_color.g, 8) |
channel(self.text_color.r, 16) |
channel(self.text_color.a, 24)
}
}
pub trait StyleSheet { pub trait StyleSheet {
fn appearance(&self) -> Appearance; fn appearance(&self) -> Appearance;
} }
@ -138,7 +125,12 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
let appearance = theme.appearance(); let appearance = theme.appearance();
let text_color_u32 = appearance.text_color_u32(); let text_color = cosmic_text::Color::rgba(
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
);
let instant = Instant::now(); let instant = Instant::now();
@ -166,12 +158,12 @@ where
let buffer_x = layout.bounds().x; let buffer_x = layout.bounds().x;
let buffer_y = layout.bounds().y; let buffer_y = layout.bounds().y;
buffer.draw(&mut cache, text_color_u32, |x, y, w, h, color| { buffer.draw(&mut cache, text_color, |x, y, w, h, color| {
let a = (color >> 24) as u8; let a = color.a();
if a > 0 { if a > 0 {
let r = (color >> 16) as u8; let r = color.r();
let g = (color >> 8) as u8; let g = color.g();
let b = color as u8; let b = color.b();
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds: Rectangle::new( bounds: Rectangle::new(

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics}; use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag}; use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, thread, time::{Duration, Instant}}; use std::{env, fs, thread, time::{Duration, Instant}};
fn main() { fn main() {
@ -30,7 +30,7 @@ fn main() {
) )
.unwrap(); .unwrap();
let bg_color = Color::rgb(0x34, 0x34, 0x34); let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let font_sizes = [ let font_sizes = [
TextMetrics::new(10, 14).scale(display_scale), // Caption TextMetrics::new(10, 14).scale(display_scale), // Caption
@ -88,8 +88,8 @@ fn main() {
window.set(bg_color); window.set(bg_color);
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| { buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
window.rect(line_x + x, y, w, h, Color { data: color }); window.rect(line_x + x, y, w, h, orbclient::Color { data: color.0 })
}); });
/*TODO /*TODO

View file

@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics}; use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag}; use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, thread, time::{Duration, Instant}}; use std::{env, fs, process, thread, time::{Duration, Instant}};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) { fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) {
let bg_color = Color::rgb(0x34, 0x34, 0x34); let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
if buffer.cursor_moved { if buffer.cursor_moved {
@ -21,8 +21,8 @@ fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut Sw
window.set(bg_color); window.set(bg_color);
buffer.draw(swash_cache, font_color.data, |x, y, w, h, color| { buffer.draw(swash_cache, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, Color { data: color }); window.rect(x, y, w, h, orbclient::Color { data: color.0 });
}); });
window.sync(); window.sync();

View file

@ -130,7 +130,7 @@ fn main() {
let mut mouse_left = false; let mut mouse_left = false;
loop { loop {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34); let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF); let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
if buffer.cursor_moved { if buffer.cursor_moved {
buffer.shape_until_cursor(); buffer.shape_until_cursor();
@ -144,8 +144,8 @@ fn main() {
window.set(bg_color); window.set(bg_color);
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| { buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color }); window.rect(x, y, w, h, orbclient::Color { data: color.0 });
}); });
window.sync(); window.sync();

View file

@ -83,7 +83,7 @@ fn main() {
buffer.set_text(&text); buffer.set_text(&text);
let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00); let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00);
let mut font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF); let mut font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let now = Instant::now(); let now = Instant::now();
@ -103,7 +103,7 @@ fn main() {
} }
if let Some(foreground) = theme.settings.foreground { if let Some(foreground) = theme.settings.foreground {
font_color = orbclient::Color::rgba( font_color = Color::rgba(
foreground.r, foreground.r,
foreground.g, foreground.g,
foreground.b, foreground.b,
@ -225,8 +225,8 @@ fn main() {
window.set(bg_color); window.set(bg_color);
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| { buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color }); window.rect(x, y, w, h, orbclient::Color { data: color.0 });
}); });
window.sync(); window.sync();

View file

@ -1 +1 @@
RUST_LOG=cosmic_text=debug cargo run --release --package rich-text -- "$@" RUST_LOG=cosmic_text=debug,rich_text=debug cargo run --release --package rich-text -- "$@"

View file

@ -6,10 +6,14 @@ pub use fontdb::{Family, Stretch, Style, Weight};
pub struct Color(pub u32); pub struct Color(pub u32);
impl Color { impl Color {
/// Create new color with red, green, and blue components
#[inline]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self { pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::rgba(r, g, b, 0xFF) 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 { pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self( Self(
((a as u32) << 24) | ((a as u32) << 24) |
@ -18,6 +22,30 @@ impl Color {
(b as u32) (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)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]

View file

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

View file

@ -5,7 +5,7 @@ use swash::scale::{ScaleContext, image::Content};
use swash::scale::{Render, Source, StrikeWith}; use swash::scale::{Render, Source, StrikeWith};
use swash::zeno::{Format, Vector}; 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}; 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 /// 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, &mut self,
cache_key: CacheKey, cache_key: CacheKey,
base: u32, base: Color,
mut f: F mut f: F
) { ) {
if let Some(image) = self.get_image(cache_key) { 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_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 { for off_x in 0..image.placement.width as i32 {
//TODO: blend base alpha? //TODO: blend base alpha?
let color = (image.data[i] as u32) << 24 | base & 0xFFFFFF; f(
f(x + off_x, y + off_y, color); x + off_x,
y + off_y,
Color(
((image.data[i] as u32) << 24) |
base.0 & 0xFFFFFF
)
);
i += 1; i += 1;
} }
} }
@ -103,11 +109,16 @@ impl<'a> SwashCache<'a> {
for off_y in 0..image.placement.height as i32 { for off_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 { for off_x in 0..image.placement.width as i32 {
//TODO: blend base alpha? //TODO: blend base alpha?
let color = (image.data[i + 3] as u32) << 24 f(
| (image.data[i] as u32) << 16 x + off_x,
| (image.data[i + 1] as u32) << 8 y + off_y,
| (image.data[i + 2] as u32); Color::rgba(
f(x + off_x, y + off_y, color); image.data[i + 2],
image.data[i + 1],
image.data[i],
image.data[i + 3]
)
);
i += 4; i += 4;
} }
} }