add secondary color
bendn 7 weeks ago
parent 66f2377 · commit 1d4fb1a
-rw-r--r--src/cell.rs9
-rw-r--r--src/lib.rs58
-rw-r--r--tests/test.rs3
3 files changed, 44 insertions, 26 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 27f4d58..cade09a 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -2,7 +2,8 @@
#[repr(C)]
pub struct Style {
pub bg: [u8; 3],
- pub color: [u8; 3],
+ pub fg: [u8; 3],
+ pub secondary_color: [u8; 3],
// one of [Style::BOLD]..
pub flags: u8,
}
@@ -47,7 +48,8 @@ impl Default for Style {
fn default() -> Self {
Self {
bg: [0; 3],
- color: [255; 3],
+ fg: [255; 3],
+ secondary_color: [0; 3],
flags: 0,
}
}
@@ -64,6 +66,7 @@ impl Style {
pub const UNDERLINE: u8 = 1 << 3;
pub const STRIKETHROUGH: u8 = 1 << 4;
pub const UNDERCURL: u8 = 1 << 5;
+ pub const USE_SECONDARY_COLOR: u8 = 1 << 7;
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct Cell {
@@ -89,7 +92,7 @@ impl BitOr<u8> for Style {
impl BitOrAssign<(u8, [u8; 3])> for Style {
fn bitor_assign(&mut self, (f, c): (u8, [u8; 3])) {
self.flags |= f;
- self.color = c;
+ self.fg = c;
}
}
impl BitAnd<(u8, [u8; 3])> for Style {
diff --git a/src/lib.rs b/src/lib.rs
index fc0ceb5..eda9d35 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -16,6 +16,8 @@
)]
#![allow(unsafe_op_in_unsafe_fn)]
use std::iter::{successors, zip};
+
+use Default::default;
pub mod cell;
use atools::prelude::*;
use fimg::{Image, OverlayAt};
@@ -260,7 +262,13 @@ pub unsafe fn render(
.zip(0..)
.map(|(&(id, x), index)| (&col[index], id, x, index))
{
- let mut color = cell.style.color;
+ let mut color = cell.style.fg;
+ let sec =
+ if (cell.style.flags & Style::USE_SECONDARY_COLOR) != 0 {
+ cell.style.secondary_color
+ } else {
+ color
+ };
if (cell.style.flags & Style::DIM) != 0 {
color = color.map(|x| x / 2);
}
@@ -271,11 +279,12 @@ pub unsafe fn render(
fw.ceil() as u32,
((0.05 * ppem) as u32).max(1),
)
- .fill(color.join(255)),
+ .fill(sec.join(255)),
((j as f32 * fw).round() as i32 + offset_x as i32)
.max(0) as u32,
(k as f32 * (fh + line_spacing * fac)
- + (met.ascent * fac * 1.05))
+ + ((met.ascent - met.underline_offset) * fac))
+ // + (met.ascent * fac * 1.05))
.floor() as u32
+ offset_y,
)
@@ -299,7 +308,7 @@ pub unsafe fn render(
unsafe {
i.blend_alpha_and_color_at(
&buffer.as_ref(),
- color,
+ sec,
(j as f32 * fw).floor() as u32 // _
+ offset_x,
(k as f32 * (fh + line_spacing * fac)).floor()
@@ -590,40 +599,45 @@ fn x() {
Cell {
style: Style {
bg: [31, 36, 48],
- color: [255, 255, 255],
+ fg: [255, 255, 255],
flags: Style::UNDERCURL,
+ ..default()
},
letter: Some('['),
},
Cell {
style: Style {
bg: [31, 36, 48],
- color: [255, 173, 102],
+ fg: [255, 173, 102],
flags: Style::UNDERCURL,
+ ..default()
},
letter: Some('='),
},
Cell {
style: Style {
bg: [31, 36, 48],
- color: [204, 202, 194],
- flags: Style::UNDERCURL,
+ fg: [204, 202, 194],
+ secondary_color: [255; 3],
+ flags: Style::UNDERLINE | Style::USE_SECONDARY_COLOR,
},
letter: Some('s'),
},
Cell {
style: Style {
bg: [31, 36, 48],
- color: [255, 173, 102],
- flags: Style::UNDERCURL,
+ fg: [255, 173, 102],
+ secondary_color: [255; 3],
+ flags: Style::UNDERLINE | Style::USE_SECONDARY_COLOR,
},
letter: Some('>'),
},
Cell {
style: Style {
bg: [31, 36, 48],
- color: [255, 255, 255],
- flags: Style::UNDERCURL,
+ flags: Style::UNDERLINE,
+ fg: [255, 255, 255],
+ ..default()
},
letter: Some(']'),
},
@@ -631,15 +645,15 @@ fn x() {
let mut f = Fonts::new(*FONT, *FONT, *FONT, *FONT);
render_owned(&z, (2, 2), 18.0, &mut f, 2.0, true);
render_owned(&z, (2, 2), 18.0, &mut f, 2.0, true).show();
- let cells = Cell::load(include_bytes!("../cells"));
- render_owned(
- &cells,
- (33, cells.len() / 33),
- 18.0,
- &mut f,
- 10.0,
- true,
- )
- .show();
+ // let cells = Cell::load(include_bytes!("../cells"));
+ // render_owned(
+ // &cells,
+ // (33, cells.len() / 33),
+ // 18.0,
+ // &mut f,
+ // 10.0,
+ // true,
+ // )
+ // .show();
}
}
diff --git a/tests/test.rs b/tests/test.rs
index b31d69a..9dd7740 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -23,8 +23,9 @@ fn main() {
.map(|x: char| dsb::Cell {
style: Style {
bg: [255; 3],
- color: [132, 148, 164],
+ fg: [132, 148, 164],
flags: 0,
+ ..Default::default()
},
letter: Some(x),
})