Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/engine/src/cmd/layout/publication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
Broken::from(crate::render::report::wrap_with(&t, iw, width))
};
let kh = lines.lines.len() as f64 * pitch(pt) + 1.0 * k;
let mut kind = pub_text(lines, pt, accent, true, TextAlign::Left);
let colour = accent_ink(accent, page_colour.unwrap_or(Rgba::rgb(0xFF, 0xFF, 0xFF)), ink);
let mut kind = pub_text(lines, pt, colour, true, TextAlign::Left);
// The stored text is in capitals but for its tokens, which the case
// covers (#3126). With nothing left to raise, no case: a saved project
// asks for none.
Expand Down
30 changes: 30 additions & 0 deletions crates/engine/src/cmd/layout/publication/sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,33 @@ pub(in crate::cmd::layout) const VISUAL_CENTRE_DOWN: f64 = 0.45;
pub(in crate::cmd::layout) fn visual_centre_offset(at: (f64, f64), (w, h): (f64, f64)) -> f64 {
(at.0 - w / 2.0).hypot(at.1 - h * VISUAL_CENTRE_DOWN)
}

/// `accent` as a small text on `paper`: as given where it reaches [`TEXT_CONTRAST`] against it,
/// else mixed toward `ink`, that paper's text colour, in twentieths until it does. A paper on
/// which no mix reaches it (a mid grey) gives `ink`, as the other texts have.
pub(in crate::cmd::layout) fn accent_ink(accent: Rgba, paper: Rgba, ink: Rgba) -> Rgba {
(0..20u8)
.map(|step| {
let t = f64::from(step) / 20.0;
let mix = |a: u8, b: u8| (f64::from(a) + (f64::from(b) - f64::from(a)) * t).round() as u8;
Rgba::rgb(mix(accent.r(), ink.r()), mix(accent.g(), ink.g()), mix(accent.b(), ink.b()))
})
.find(|&c| contrast(c, paper) >= TEXT_CONTRAST)
.unwrap_or(ink)
}

/// The least contrast WCAG asks of a small text against its background.
const TEXT_CONTRAST: f64 = 4.5;

/// The WCAG contrast ratio of two opaque colours, 1 to 21.
fn contrast(a: Rgba, b: Rgba) -> f64 {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let luminance = |c: Rgba| {
let linear = |v: u8| {
let v = f64::from(v) / 255.0;
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
};
0.2126 * linear(c.r()) + 0.7152 * linear(c.g()) + 0.0722 * linear(c.b())
};
let (a, b) = (luminance(a), luminance(b));
(a.max(b) + 0.05) / (a.min(b) + 0.05)
}
53 changes: 53 additions & 0 deletions crates/engine/src/tests/layout_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,59 @@ fn a_kicker_sits_over_the_title_in_spaced_capitals_of_the_accent() {
}
}

/// The WCAG contrast ratio of two opaque colours.
fn contrast(a: Rgba, b: Rgba) -> f64 {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let luminance = |c: Rgba| {
let linear = |v: u8| {
let v = f64::from(v) / 255.0;
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
};
0.2126 * linear(c.r()) + 0.7152 * linear(c.g()) + 0.0722 * linear(c.b())
};
let (a, b) = (luminance(a), luminance(b));
(a.max(b) + 0.05) / (a.min(b) + 0.05)
}

/// The kicker's colour on an edge sheet with this paper and, when given, this accent.
fn kicker_colour(paper: &str, accent: Option<&str>) -> Rgba {
let mut s = world();
let mut p = json!({"kicker": "Day 22", "paper": paper});
if let Some(a) = accent {
p["accent"] = json!(a);
}
edge(&mut s, p).unwrap();
text_colour(last(&s), "Kicker")
}

#[test]
fn the_default_accent_is_lightened_to_read_on_a_dark_paper() {
assert_eq!(kicker_colour("#1B2430", None), Rgba::rgb(0xC7, 0x76, 0x65));
}

/// An accent the paper doesn't let read moves toward that paper's ink, only until it reads.
#[test]
fn a_kicker_accent_that_does_not_read_moves_toward_the_ink() {
for (paper, accent, ink) in [("#1B2430", "#1F6F8B", LIGHT_INK), ("#F4EFE6", "#F2C14E", INK)] {
let (paper_colour, given) = (Rgba::from_hex(&paper[1..]).unwrap(), Rgba::from_hex(&accent[1..]).unwrap());
let got = kicker_colour(paper, Some(accent));
assert!(got != given && got != ink, "{paper} {accent}: {got:?}");
assert!(contrast(got, paper_colour) >= 4.5, "{paper} {accent}: {got:?}");
}
}

#[test]
fn a_kicker_accent_that_reads_stays_as_given() {
for (paper, accent) in [("#1B2430", "#F2C14E"), ("#F4EFE6", "#1F2D4D")] {
assert_eq!(kicker_colour(paper, Some(accent)), Rgba::from_hex(&accent[1..]).unwrap(), "{paper} {accent}");
}
}

/// On a mid grey not even the ink reaches 4.5:1: the kicker takes the ink, as the title does.
#[test]
fn a_kicker_on_a_paper_no_mix_reads_on_takes_the_ink() {
assert_eq!(kicker_colour("#8A8A8A", None), LIGHT_INK);
}

#[test]
fn a_long_kicker_wraps_within_the_page() {
let mut s = world();
Expand Down
4 changes: 3 additions & 1 deletion docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
other styles and figures refuse all three. An edge sheet's graticule labels sit at the page's side
edges, so a printer with no bleed may clip them. `"accent"` (`#RRGGBB`) colours the
band, the panel's tint, the headings and rules, a poster's page and an edge sheet's kicker (a
deep brown, green or blue by default, a brick red on an edge sheet).
deep brown, green or blue by default, a brick red on an edge sheet). Where the accent would be
hard to read on an edge sheet's paper (under 4.5:1 contrast), the kicker takes it lightened or
darkened toward the texts' colour, as the sheet is made.
A figure leaves the style to its caption. In the menu, each sheet opens a list of the seven
styles, each saying what it looks like, in its default accent (`"accent"` in the command
picks another).
Expand Down
Loading