use super::model::{Block, FDoc, Inline, Rgb, RunFmt};
const DARK_BRIGHTNESS: f32 = 128.0;
const WHITE: Rgb = [255, 255, 255];
pub(super) fn apply(doc: &mut FDoc) {
for section in &mut doc.sections {
blocks(&mut section.blocks, None);
}
for part in &mut doc.parts {
blocks(part, None);
}
for (_, notes) in doc.footnotes.iter_mut().chain(doc.endnotes.iter_mut()) {
blocks(notes, None);
}
}
fn is_dark(color: Rgb) -> bool {
let [red, green, blue] = color.map(f32::from);
0.299 * red + 0.587 * green + 0.114 * blue < DARK_BRIGHTNESS
}
fn blocks(list: &mut [Block], background: Option<Rgb>) {
for block in list {
match block {
Block::Para(para) => {
let background = para.props.shading.or(background);
if let Some(number) = &mut para.number {
run(&mut number.fmt, background);
}
run(&mut para.mark, background);
for inline in &mut para.inlines {
inline_fmt(inline, background);
}
}
Block::Table(table) => {
for row in &mut table.rows {
for cell in &mut row.cells {
blocks(&mut cell.blocks, cell.shading.or(background));
}
}
}
}
}
}
fn inline_fmt(inline: &mut Inline, background: Option<Rgb>) {
match inline {
Inline::Text { fmt, .. }
| Inline::Tab { fmt }
| Inline::Break { fmt, .. }
| Inline::Field { fmt, .. }
| Inline::Image { fmt, .. }
| Inline::NoteRef { fmt, .. }
| Inline::NoteNumber { fmt }
| Inline::Math { fmt, .. } => run(fmt, background),
Inline::Shape(shape) => {
let fill = shape.fill;
blocks(&mut shape.blocks, fill);
}
}
}
fn run(fmt: &mut RunFmt, background: Option<Rgb>) {
if fmt.color.is_none() && fmt.highlight.or(fmt.shading).or(background).is_some_and(is_dark) {
fmt.color = Some(WHITE);
}
}
#[cfg(test)]
mod tests {
use super::super::model::{Cell, Para, Row, Section, Table};
use super::*;
fn doc_with_cell(shading: Rgb) -> FDoc {
let para = Para {
inlines: vec![Inline::Text {
text: "Section A".into(),
fmt: RunFmt::default(),
}],
..Default::default()
};
let cell = Cell {
shading: Some(shading),
blocks: vec![Block::Para(Box::new(para))],
..Default::default()
};
let table = Table {
rows: vec![Row {
cells: vec![cell],
..Default::default()
}],
..Default::default()
};
FDoc {
sections: vec![Section {
blocks: vec![Block::Table(table)],
..Default::default()
}],
..Default::default()
}
}
fn text_color(doc: &FDoc) -> Option<Rgb> {
let Block::Table(table) = &doc.sections[0].blocks[0] else {
return None;
};
let Block::Para(para) = &table.rows[0].cells[0].blocks[0] else {
return None;
};
let Inline::Text { fmt, .. } = ¶.inlines[0] else {
return None;
};
fmt.color
}
#[test]
fn automatic_text_is_white_on_dark_cells() {
let mut dark = doc_with_cell([0, 0, 0]);
apply(&mut dark);
assert_eq!(text_color(&dark), Some(WHITE));
let mut light = doc_with_cell([230, 230, 230]);
apply(&mut light);
assert_eq!(text_color(&light), None);
}
}