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,
}
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(

View file

@ -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

View file

@ -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();

View file

@ -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();

View file

@ -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();