Use Color in more places
This commit is contained in:
parent
4edca946ea
commit
3ece9236b3
9 changed files with 84 additions and 53 deletions
|
|
@ -29,19 +29,6 @@ pub struct Appearance {
|
|||
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 {
|
||||
fn appearance(&self) -> Appearance;
|
||||
}
|
||||
|
|
@ -138,7 +125,12 @@ where
|
|||
_viewport: &Rectangle,
|
||||
) {
|
||||
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();
|
||||
|
||||
|
|
@ -166,12 +158,12 @@ where
|
|||
|
||||
let buffer_x = layout.bounds().x;
|
||||
let buffer_y = layout.bounds().y;
|
||||
buffer.draw(&mut cache, text_color_u32, |x, y, w, h, color| {
|
||||
let a = (color >> 24) as u8;
|
||||
buffer.draw(&mut cache, text_color, |x, y, w, h, color| {
|
||||
let a = color.a();
|
||||
if a > 0 {
|
||||
let r = (color >> 16) as u8;
|
||||
let g = (color >> 8) as u8;
|
||||
let b = color as u8;
|
||||
let r = color.r();
|
||||
let g = color.g();
|
||||
let b = color.b();
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: Rectangle::new(
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
|
||||
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
|
||||
use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
|
||||
use orbclient::{EventOption, Renderer, Window, WindowFlag};
|
||||
use std::{env, fs, thread, time::{Duration, Instant}};
|
||||
|
||||
fn main() {
|
||||
|
|
@ -30,7 +30,7 @@ fn main() {
|
|||
)
|
||||
.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_sizes = [
|
||||
TextMetrics::new(10, 14).scale(display_scale), // Caption
|
||||
|
|
@ -88,8 +88,8 @@ fn main() {
|
|||
|
||||
window.set(bg_color);
|
||||
|
||||
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
|
||||
window.rect(line_x + x, y, w, h, Color { data: color });
|
||||
buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
|
||||
window.rect(line_x + x, y, w, h, orbclient::Color { data: color.0 })
|
||||
});
|
||||
|
||||
/*TODO
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
|
||||
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
|
||||
use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
|
||||
use orbclient::{EventOption, Renderer, Window, WindowFlag};
|
||||
use std::{env, fs, process, thread, time::{Duration, Instant}};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
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);
|
||||
|
||||
if buffer.cursor_moved {
|
||||
|
|
@ -21,8 +21,8 @@ fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut Sw
|
|||
|
||||
window.set(bg_color);
|
||||
|
||||
buffer.draw(swash_cache, font_color.data, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, Color { data: color });
|
||||
buffer.draw(swash_cache, font_color, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
|
||||
});
|
||||
|
||||
window.sync();
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ fn main() {
|
|||
let mut mouse_left = false;
|
||||
loop {
|
||||
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 {
|
||||
buffer.shape_until_cursor();
|
||||
|
|
@ -144,8 +144,8 @@ fn main() {
|
|||
|
||||
window.set(bg_color);
|
||||
|
||||
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, orbclient::Color { data: color });
|
||||
buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
|
||||
});
|
||||
|
||||
window.sync();
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ fn main() {
|
|||
buffer.set_text(&text);
|
||||
|
||||
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();
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ fn main() {
|
|||
}
|
||||
|
||||
if let Some(foreground) = theme.settings.foreground {
|
||||
font_color = orbclient::Color::rgba(
|
||||
font_color = Color::rgba(
|
||||
foreground.r,
|
||||
foreground.g,
|
||||
foreground.b,
|
||||
|
|
@ -225,8 +225,8 @@ fn main() {
|
|||
|
||||
window.set(bg_color);
|
||||
|
||||
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, orbclient::Color { data: color });
|
||||
buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
|
||||
});
|
||||
|
||||
window.sync();
|
||||
|
|
|
|||
|
|
@ -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 -- "$@"
|
||||
|
|
|
|||
28
src/attrs.rs
28
src/attrs.rs
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
|||
31
src/swash.rs
31
src/swash.rs
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue