Skip to content

Commit 3ece923

Browse files
committed
Use Color in more places
1 parent 4edca94 commit 3ece923

File tree

9 files changed

+84
-53
lines changed

9 files changed

+84
-53
lines changed

examples/editor-libcosmic/src/text_box.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ pub struct Appearance {
2929
text_color: Color,
3030
}
3131

32-
impl Appearance {
33-
fn text_color_u32(&self) -> u32 {
34-
let channel = |f: f32, shift: i32| -> u32 {
35-
(cmp::max(0, cmp::min(255, (f * 255.0) as i32)) << shift) as u32
36-
};
37-
38-
channel(self.text_color.b, 0) |
39-
channel(self.text_color.g, 8) |
40-
channel(self.text_color.r, 16) |
41-
channel(self.text_color.a, 24)
42-
}
43-
}
44-
4532
pub trait StyleSheet {
4633
fn appearance(&self) -> Appearance;
4734
}
@@ -138,7 +125,12 @@ where
138125
_viewport: &Rectangle,
139126
) {
140127
let appearance = theme.appearance();
141-
let text_color_u32 = appearance.text_color_u32();
128+
let text_color = cosmic_text::Color::rgba(
129+
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
130+
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
131+
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
132+
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
133+
);
142134

143135
let instant = Instant::now();
144136

@@ -166,12 +158,12 @@ where
166158

167159
let buffer_x = layout.bounds().x;
168160
let buffer_y = layout.bounds().y;
169-
buffer.draw(&mut cache, text_color_u32, |x, y, w, h, color| {
170-
let a = (color >> 24) as u8;
161+
buffer.draw(&mut cache, text_color, |x, y, w, h, color| {
162+
let a = color.a();
171163
if a > 0 {
172-
let r = (color >> 16) as u8;
173-
let g = (color >> 8) as u8;
174-
let b = color as u8;
164+
let r = color.r();
165+
let g = color.g();
166+
let b = color.b();
175167
renderer.fill_quad(
176168
renderer::Quad {
177169
bounds: Rectangle::new(

examples/editor-orbclient/src/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
4-
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
3+
use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
4+
use orbclient::{EventOption, Renderer, Window, WindowFlag};
55
use std::{env, fs, thread, time::{Duration, Instant}};
66

77
fn main() {
@@ -30,7 +30,7 @@ fn main() {
3030
)
3131
.unwrap();
3232

33-
let bg_color = Color::rgb(0x34, 0x34, 0x34);
33+
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
3434
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
3535
let font_sizes = [
3636
TextMetrics::new(10, 14).scale(display_scale), // Caption
@@ -88,8 +88,8 @@ fn main() {
8888

8989
window.set(bg_color);
9090

91-
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
92-
window.rect(line_x + x, y, w, h, Color { data: color });
91+
buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
92+
window.rect(line_x + x, y, w, h, orbclient::Color { data: color.0 })
9393
});
9494

9595
/*TODO

examples/editor-test/src/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
4-
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
3+
use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
4+
use orbclient::{EventOption, Renderer, Window, WindowFlag};
55
use std::{env, fs, process, thread, time::{Duration, Instant}};
66
use unicode_segmentation::UnicodeSegmentation;
77

88
fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) {
9-
let bg_color = Color::rgb(0x34, 0x34, 0x34);
9+
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
1010
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
1111

1212
if buffer.cursor_moved {
@@ -21,8 +21,8 @@ fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut Sw
2121

2222
window.set(bg_color);
2323

24-
buffer.draw(swash_cache, font_color.data, |x, y, w, h, color| {
25-
window.rect(x, y, w, h, Color { data: color });
24+
buffer.draw(swash_cache, font_color, |x, y, w, h, color| {
25+
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
2626
});
2727

2828
window.sync();

examples/rich-text/src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn main() {
130130
let mut mouse_left = false;
131131
loop {
132132
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
133-
let font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF);
133+
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
134134

135135
if buffer.cursor_moved {
136136
buffer.shape_until_cursor();
@@ -144,8 +144,8 @@ fn main() {
144144

145145
window.set(bg_color);
146146

147-
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
148-
window.rect(x, y, w, h, orbclient::Color { data: color });
147+
buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
148+
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
149149
});
150150

151151
window.sync();

examples/syntax/src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn main() {
8383
buffer.set_text(&text);
8484

8585
let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00);
86-
let mut font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF);
86+
let mut font_color = Color::rgb(0xFF, 0xFF, 0xFF);
8787

8888
let now = Instant::now();
8989

@@ -103,7 +103,7 @@ fn main() {
103103
}
104104

105105
if let Some(foreground) = theme.settings.foreground {
106-
font_color = orbclient::Color::rgba(
106+
font_color = Color::rgba(
107107
foreground.r,
108108
foreground.g,
109109
foreground.b,
@@ -225,8 +225,8 @@ fn main() {
225225

226226
window.set(bg_color);
227227

228-
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
229-
window.rect(x, y, w, h, orbclient::Color { data: color });
228+
buffer.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
229+
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
230230
});
231231

232232
window.sync();

rich-text.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
RUST_LOG=cosmic_text=debug cargo run --release --package rich-text -- "$@"
1+
RUST_LOG=cosmic_text=debug,rich_text=debug cargo run --release --package rich-text -- "$@"

src/attrs.rs

+28
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ pub use fontdb::{Family, Stretch, Style, Weight};
66
pub struct Color(pub u32);
77

88
impl Color {
9+
/// Create new color with red, green, and blue components
10+
#[inline]
911
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
1012
Self::rgba(r, g, b, 0xFF)
1113
}
1214

15+
/// Create new color with red, green, blue, and alpha components
16+
#[inline]
1317
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
1418
Self(
1519
((a as u32) << 24) |
@@ -18,6 +22,30 @@ impl Color {
1822
(b as u32)
1923
)
2024
}
25+
26+
/// Get the red component
27+
#[inline]
28+
pub fn r(&self) -> u8 {
29+
((self.0 & 0x00FF0000) >> 16) as u8
30+
}
31+
32+
/// Get the green component
33+
#[inline]
34+
pub fn g(&self) -> u8 {
35+
((self.0 & 0x0000FF00) >> 8) as u8
36+
}
37+
38+
/// Get the blue component
39+
#[inline]
40+
pub fn b(&self) -> u8 {
41+
(self.0 & 0x000000FF) as u8
42+
}
43+
44+
/// Get the alpha component
45+
#[inline]
46+
pub fn a(&self) -> u8 {
47+
((self.0 & 0xFF000000) >> 24) as u8
48+
}
2149
}
2250

2351
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]

src/buffer.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
};
88
use unicode_segmentation::UnicodeSegmentation;
99

10-
use crate::{Attrs, AttrsList, FontSystem, LayoutGlyph, LayoutLine, ShapeLine};
10+
use crate::{Attrs, AttrsList, Color, FontSystem, LayoutGlyph, LayoutLine, ShapeLine};
1111

1212
/// An action to perform on a [TextBuffer]
1313
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -963,8 +963,8 @@ impl<'a> TextBuffer<'a> {
963963

964964
/// Draw the buffer
965965
#[cfg(feature = "swash")]
966-
pub fn draw<F>(&self, cache: &mut crate::SwashCache, color: u32, mut f: F)
967-
where F: FnMut(i32, i32, u32, u32, u32)
966+
pub fn draw<F>(&self, cache: &mut crate::SwashCache, color: Color, mut f: F)
967+
where F: FnMut(i32, i32, u32, u32, Color)
968968
{
969969
let font_size = self.metrics.font_size;
970970
let line_height = self.metrics.line_height;
@@ -1054,7 +1054,7 @@ impl<'a> TextBuffer<'a> {
10541054
line_y - font_size,
10551055
cmp::max(0, max - min) as u32,
10561056
line_height as u32,
1057-
0x33_00_00_00 | (color & 0xFF_FF_FF)
1057+
Color::rgba(color.r(), color.g(), color.b(), 0x33)
10581058
);
10591059
}
10601060
c_x += c_w;
@@ -1080,7 +1080,7 @@ impl<'a> TextBuffer<'a> {
10801080
line_y - font_size,
10811081
cmp::max(0, max - min) as u32,
10821082
line_height as u32,
1083-
0x33_00_00_00 | (color & 0xFF_FF_FF)
1083+
Color::rgba(color.r(), color.g(), color.b(), 0x33)
10841084
);
10851085
}
10861086
}
@@ -1126,7 +1126,7 @@ impl<'a> TextBuffer<'a> {
11261126
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
11271127

11281128
let glyph_color = match glyph.color_opt {
1129-
Some(some) => some.0,
1129+
Some(some) => some,
11301130
None => color,
11311131
};
11321132

src/swash.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use swash::scale::{ScaleContext, image::Content};
55
use swash::scale::{Render, Source, StrikeWith};
66
use swash::zeno::{Format, Vector};
77

8-
use crate::{CacheKey, FontSystem};
8+
use crate::{CacheKey, Color, FontSystem};
99

1010
pub use swash::scale::image::{Content as SwashContent, Image as SwashImage};
1111

@@ -76,10 +76,10 @@ impl<'a> SwashCache<'a> {
7676
}
7777

7878
/// Enumerate pixels in an Image, use `with_image` for better performance
79-
pub fn with_pixels<F: FnMut(i32, i32, u32)>(
79+
pub fn with_pixels<F: FnMut(i32, i32, Color)>(
8080
&mut self,
8181
cache_key: CacheKey,
82-
base: u32,
82+
base: Color,
8383
mut f: F
8484
) {
8585
if let Some(image) = self.get_image(cache_key) {
@@ -92,8 +92,14 @@ impl<'a> SwashCache<'a> {
9292
for off_y in 0..image.placement.height as i32 {
9393
for off_x in 0..image.placement.width as i32 {
9494
//TODO: blend base alpha?
95-
let color = (image.data[i] as u32) << 24 | base & 0xFFFFFF;
96-
f(x + off_x, y + off_y, color);
95+
f(
96+
x + off_x,
97+
y + off_y,
98+
Color(
99+
((image.data[i] as u32) << 24) |
100+
base.0 & 0xFFFFFF
101+
)
102+
);
97103
i += 1;
98104
}
99105
}
@@ -103,11 +109,16 @@ impl<'a> SwashCache<'a> {
103109
for off_y in 0..image.placement.height as i32 {
104110
for off_x in 0..image.placement.width as i32 {
105111
//TODO: blend base alpha?
106-
let color = (image.data[i + 3] as u32) << 24
107-
| (image.data[i] as u32) << 16
108-
| (image.data[i + 1] as u32) << 8
109-
| (image.data[i + 2] as u32);
110-
f(x + off_x, y + off_y, color);
112+
f(
113+
x + off_x,
114+
y + off_y,
115+
Color::rgba(
116+
image.data[i + 2],
117+
image.data[i + 1],
118+
image.data[i],
119+
image.data[i + 3]
120+
)
121+
);
111122
i += 4;
112123
}
113124
}

0 commit comments

Comments
 (0)