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
2 changes: 1 addition & 1 deletion crates/opencode-i18n/locales/rune/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
"rune.settings.developer.title": "Developer",
"rune.settings.experimental.row.sidebarSections.description": "Select multiple side tabs with ⌘/⇧-click, run batch actions, and group them under minimal collapsible headers.",
"rune.settings.experimental.row.sidebarSections.title": "Sidebar sections",
"rune.settings.experimental.row.readability.description": "Calmer contrast, narrower prose measure, and tuned typography for long reading sessions.",
"rune.settings.experimental.row.readability.description": "Narrower prose measure, calmer inline code, and tuned typography and table spacing.",
"rune.settings.experimental.row.readability.title": "High-readability timeline",
"rune.settings.experimental.row.smoothTimeline.description": "Ease pending and queued messages into place as they arrive.",
"rune.settings.experimental.row.smoothTimeline.title": "Smooth timeline",
Expand Down
6 changes: 2 additions & 4 deletions crates/opencode-ui/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,7 @@ fn build(theme: usize, dark: bool, raised: bool, readable: bool) -> Pal {
dot: c("grey-300"),
},
};
if readable {
crate::readability::calibrate(&mut pal);
}
pal.readable = readable;
pal
}

Expand Down Expand Up @@ -680,7 +678,7 @@ fn kit_from(p: &Pal) -> Kit {
// The kit's controls ring in the app's `border-focus` (`assets/theme.json`), the same as
// `crate::focus`, in the default theme too.
k.c.border_focus = p.v2.border_focus;
if p.theme == 0 && !p.readable {
if p.theme == 0 {
return k;
}
let c = &mut k.c;
Expand Down
291 changes: 5 additions & 286 deletions crates/opencode-ui/src/readability.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! High-readability timeline mode: vision-science calibrated palette tokens for default `oc-2`,
//! non-destructive OKLCH-L APCA guardrails for custom themes, and per-frame typography state.
//! High-readability timeline mode: per-frame typography and layout state.
//!
//! Keeps the user's active theme and color scheme untouched; only typography (size, leading,
//! weight, tabular/disambiguated numerals, inline-code sizing, table cell leading) and prose
//! measure (`560 pt`) adapt when enabled.

use std::cell::Cell;

use rune::Color;

use crate::palette::Pal;

thread_local! {
static ACTIVE: Cell<bool> = const { Cell::new(false) };
/// Whether the active text renderer dilates light stems (e.g. `FontSmoothing::On` / `Windfoil`).
Expand All @@ -32,284 +31,4 @@ pub fn dilated() -> bool {
/// Compact stamp for memoization tuples (`timeline/mod.rs`, `session.rs`, `composer.rs`).
pub fn stamp() -> u8 {
u8::from(ACTIVE.get()) | (u8::from(DILATED.get()) << 1)
}

/// Calibrates `p` in place when High-readability mode is active:
/// - Default `oc-2` (`theme == 0`): applies the verified dark/light Soft-Slate / Soft-Paper palette
/// (halation-free prose, APCA-floored muted/faint/punctuation, subtle chrome-vs-content depth).
/// - Custom themes (`theme > 0`): preserves surfaces and syntax hues (`Δh = 0°`), clamping only
/// out-of-band reading inks along OKLCH `L` to meet APCA/WCAG comfort floors and ceilings.
pub(crate) fn calibrate(p: &mut Pal) {
p.readable = true;
if p.forced {
return;
}
if p.theme == 0 {
calibrate_default(p);
} else {
calibrate_custom(p);
}
}

fn hex(s: &str, fallback: Color) -> Color {
rune::color::hex(s).unwrap_or(fallback)
}

fn calibrate_default(p: &mut Pal) {
if p.dark {
// Dark Soft Slate (h ≈ 255°, C ≈ 0.005 surfaces / near-achromatic inks):
// bg_deep #0d0e10 (L 0.160), bg_base #17191b (L 0.212), code_bg #121315 (L 0.187),
// text #dee0e1 (L 0.903, Lc -86.5), muted #b6b7b9 (Lc -62.2), faint #959698 (Lc -44.4).
p.bg_deep = hex("#0d0e10", p.bg_deep);
p.bg_base = hex("#17191b", p.bg_base);
p.layer1 = hex("#212428", p.layer1);
p.layer2 = hex("#2a2d33", p.layer2);
p.layer3 = hex("#353941", p.layer3);
p.code_bg = hex("#121315", p.code_bg);
p.frost = p.bg_base.alpha(0.92);

p.text = hex("#dee0e1", p.text);
p.muted = hex("#b6b7b9", p.muted);
p.faint = hex("#959698", p.faint);
p.icon = p.text;
p.icon_muted = p.muted;
p.icon_faint = p.faint;
p.v1_text_base = p.text;
p.v1_text_weak = p.muted;
p.term_fg = p.text;
p.inline_code_bg = p.text.alpha(0.08);
p.border = hex("#2d3138", p.border);
p.border_muted = hex("#262930", p.border_muted);

p.syntax.punctuation = hex("#9a9c9d", p.syntax.punctuation);
p.syntax.operator = hex("#9a9c9d", p.syntax.operator);
p.syntax.comment = hex("#9fa1a4", p.syntax.comment);
p.syntax.parameter = hex("#b9bbbe", p.syntax.parameter);
p.syntax.regexp = hex("#b9bbbe", p.syntax.regexp);
p.syntax.variable = hex("#dcdfe1", p.syntax.variable);
p.syntax.ty = hex("#aba7f9", p.syntax.ty);
p.diff.add_word_text = p.text;
p.diff.del_word_text = p.text;
} else {
// Light Soft Paper: eliminates #ffffff glare (+104.8 -> +98.3) and fixes sub-AA faint
// (#808080 3.95:1 -> #6e7278 4.55:1) and syntax tokens (property/string/keyword >= 4.5:1).
p.bg_deep = hex("#eff1f5", p.bg_deep);
p.bg_base = hex("#f7f8fa", p.bg_base);
p.layer1 = hex("#f1f3f6", p.layer1);
p.layer2 = hex("#eaedf2", p.layer2);
p.layer3 = hex("#e2e6ec", p.layer3);
p.code_bg = hex("#f2f4f7", p.code_bg);
p.frost = p.bg_base.alpha(0.92);

p.text = hex("#222428", p.text);
p.muted = hex("#53575f", p.muted);
p.faint = hex("#6e7278", p.faint);
p.icon = p.text;
p.icon_muted = p.muted;
p.icon_faint = p.faint;
p.v1_text_base = p.text;
p.v1_text_weak = p.muted;
p.term_fg = p.text;
p.inline_code_bg = p.text.alpha(0.07);
p.border = hex("#dde1e7", p.border);
p.border_muted = hex("#e6e9ee", p.border_muted);

p.syntax.property = hex("#a5470a", p.syntax.property);
p.syntax.string = hex("#0c7d38", p.syntax.string);
p.syntax.keyword = hex("#b8327d", p.syntax.keyword);
p.syntax.constant = hex("#006d72", p.syntax.constant);
p.syntax.punctuation = hex("#5e626a", p.syntax.punctuation);
p.syntax.operator = hex("#5e626a", p.syntax.operator);
p.syntax.parameter = hex("#52565e", p.syntax.parameter);
p.syntax.regexp = hex("#52565e", p.syntax.regexp);
p.syntax.variable = hex("#222428", p.syntax.variable);
}
}

fn calibrate_custom(p: &mut Pal) {
let bg = p.bg_base;
let code_bg = p.code_bg;
if p.dark {
p.text = clamp_apca_dark(p.text, bg, 75.0, 90.0);
p.muted = floor_apca_dark(p.muted, bg, 58.0);
let faint_bg = floor_apca_dark(p.faint, bg, 42.0);
p.faint = floor_apca_dark(faint_bg, p.layer1, 40.0);
p.icon = p.text;
p.icon_muted = p.muted;
p.icon_faint = p.faint;
p.v1_text_base = p.text;
p.v1_text_weak = p.muted;
p.syntax.punctuation = floor_apca_dark(p.syntax.punctuation, code_bg, 44.0);
p.syntax.operator = floor_apca_dark(p.syntax.operator, code_bg, 44.0);
p.syntax.comment = floor_apca_dark(p.syntax.comment, code_bg, 46.0);
p.syntax.parameter = floor_apca_dark(p.syntax.parameter, code_bg, 46.0);
p.syntax.string = floor_apca_dark(p.syntax.string, code_bg, 42.0);
p.syntax.keyword = floor_apca_dark(p.syntax.keyword, code_bg, 42.0);
p.syntax.property = floor_apca_dark(p.syntax.property, code_bg, 42.0);
p.syntax.ty = floor_apca_dark(p.syntax.ty, code_bg, 42.0);
p.syntax.constant = floor_apca_dark(p.syntax.constant, code_bg, 42.0);
} else {
p.text = cap_apca_light(p.text, bg, 99.0);
p.muted = floor_apca_light(p.muted, bg, 75.0);
p.faint = floor_wcag_light(p.faint, bg, 4.5);
p.icon = p.text;
p.icon_muted = p.muted;
p.icon_faint = p.faint;
p.v1_text_base = p.text;
p.v1_text_weak = p.muted;
for tok in [
&mut p.syntax.punctuation,
&mut p.syntax.operator,
&mut p.syntax.comment,
&mut p.syntax.parameter,
&mut p.syntax.string,
&mut p.syntax.keyword,
&mut p.syntax.property,
&mut p.syntax.ty,
&mut p.syntax.constant,
] {
*tok = floor_wcag_light(*tok, code_bg, 4.5);
}
}
}

fn srgb_to_linear(u: f32) -> f32 {
if u <= 0.04045 { u / 12.92 } else { ((u + 0.055) / 1.055).powf(2.4) }
}

fn linear_to_srgb(u: f32) -> f32 {
let u = u.clamp(0.0, 1.0);
if u <= 0.0031308 { 12.92 * u } else { 1.055 * u.powf(1.0 / 2.4) - 0.055 }
}

fn to_srgb_channels(c: Color) -> [f32; 3] {
if rune::theme::blends_srgb() {
[c.r, c.g, c.b]
} else {
[linear_to_srgb(c.r), linear_to_srgb(c.g), linear_to_srgb(c.b)]
}
}

fn from_srgb_channels(rgb: [f32; 3], a: f32) -> Color {
if rune::theme::blends_srgb() {
Color { r: rgb[0], g: rgb[1], b: rgb[2], a }
} else {
Color { r: srgb_to_linear(rgb[0]), g: srgb_to_linear(rgb[1]), b: srgb_to_linear(rgb[2]), a }
}
}

fn apca_lc(fg: Color, bg: Color) -> f32 {
let y_apca = |c: Color| -> f32 {
let [r, g, b] = to_srgb_channels(c);
let y = 0.2126729 * r.clamp(0.0, 1.0).powf(2.4)
+ 0.7151522 * g.clamp(0.0, 1.0).powf(2.4)
+ 0.0721750 * b.clamp(0.0, 1.0).powf(2.4);
if y < 0.022 { y + (0.022 - y).powf(1.414) } else { y }
};
let (yt, yb) = (y_apca(fg), y_apca(bg));
if (yb - yt).abs() < 0.0005 {
return 0.0;
}
if yb > yt {
let sapc = (yb.powf(0.56) - yt.powf(0.57)) * 1.14;
if sapc < 0.1 { 0.0 } else { (sapc - 0.027) * 100.0 }
} else {
let sapc = (yb.powf(0.65) - yt.powf(0.62)) * 1.14;
if sapc > -0.1 { 0.0 } else { (sapc + 0.027) * 100.0 }
}
}

fn wcag_ratio(fg: Color, bg: Color) -> f32 {
let lum = |c: Color| -> f32 {
let [r, g, b] = to_srgb_channels(c);
0.2126 * srgb_to_linear(r) + 0.7152 * srgb_to_linear(g) + 0.0722 * srgb_to_linear(b)
};
let (l1, l2) = (lum(fg), lum(bg));
(l1.max(l2) + 0.05) / (l1.min(l2) + 0.05)
}

fn srgb_to_oklch(c: Color) -> [f32; 3] {
let [sr, sg, sb] = to_srgb_channels(c);
let (r, g, b) = (srgb_to_linear(sr), srgb_to_linear(sg), srgb_to_linear(sb));
let l = (0.41222147 * r + 0.53633254 * g + 0.05144599 * b).cbrt();
let m = (0.21190350 * r + 0.68069955 * g + 0.10739696 * b).cbrt();
let s = (0.08830246 * r + 0.28171884 * g + 0.62997870 * b).cbrt();
let ok_l = 0.21045426 * l + 0.79361779 * m - 0.00407205 * s;
let ok_a = 1.97799850 * l - 2.42859221 * m + 0.45059371 * s;
let ok_b = 0.02590404 * l + 0.78277177 * m - 0.80867577 * s;
[ok_l, (ok_a * ok_a + ok_b * ok_b).sqrt(), ok_b.atan2(ok_a)]
}

fn oklch_to_color(ok_l: f32, ok_c: f32, ok_h: f32, a: f32) -> Color {
let (cos_h, sin_h) = (ok_h.cos(), ok_h.sin());
let mut lo = 0.0_f32;
let mut hi = ok_c.max(0.0);
let eval = |ch: f32| -> [f32; 3] {
let (oa, ob) = (ch * cos_h, ch * sin_h);
let l = (ok_l + 0.39633778 * oa + 0.21580376 * ob).powi(3);
let m = (ok_l - 0.10556135 * oa - 0.06385417 * ob).powi(3);
let s = (ok_l - 0.08948418 * oa - 1.29148555 * ob).powi(3);
[
4.07674166 * l - 3.30771159 * m + 0.23096993 * s,
-1.26843800 * l + 2.60975740 * m - 0.34131940 * s,
-0.00419609 * l - 0.70341861 * m + 1.70761470 * s,
]
};
let in_gamut = |lin: [f32; 3]| lin.iter().all(|&v| (-1e-4..=1.0001).contains(&v));
let lin = if in_gamut(eval(hi)) {
eval(hi)
} else {
for _ in 0..16 {
let mid = 0.5 * (lo + hi);
if in_gamut(eval(mid)) { lo = mid; } else { hi = mid; }
}
eval(lo)
};
from_srgb_channels([linear_to_srgb(lin[0]), linear_to_srgb(lin[1]), linear_to_srgb(lin[2])], a)
}

fn search_l(fg: Color, lo: f32, hi: f32, pred: impl Fn(Color) -> bool) -> Color {
let [_, c, h] = srgb_to_oklch(fg);
let (mut a, mut b) = (lo, hi);
for _ in 0..24 {
let mid = 0.5 * (a + b);
if pred(oklch_to_color(mid, c, h, fg.a)) { b = mid; } else { a = mid; }
}
oklch_to_color(b, c, h, fg.a)
}

fn floor_apca_dark(fg: Color, bg: Color, min_abs_lc: f32) -> Color {
if (-apca_lc(fg, bg)) >= min_abs_lc { return fg; }
let [l0, ..] = srgb_to_oklch(fg);
search_l(fg, l0, 0.96, |c| (-apca_lc(c, bg)) >= min_abs_lc)
}

fn clamp_apca_dark(fg: Color, bg: Color, min_abs_lc: f32, max_abs_lc: f32) -> Color {
let cur = -apca_lc(fg, bg);
if cur < min_abs_lc {
return floor_apca_dark(fg, bg, min_abs_lc);
}
if cur <= max_abs_lc {
return fg;
}
let [l0, ..] = srgb_to_oklch(fg);
search_l(fg, 0.35, l0, |c| (-apca_lc(c, bg)) >= max_abs_lc)
}

fn cap_apca_light(fg: Color, bg: Color, max_lc: f32) -> Color {
if apca_lc(fg, bg) <= max_lc { return fg; }
let [l0, ..] = srgb_to_oklch(fg);
search_l(fg, l0, 0.55, |c| apca_lc(c, bg) <= max_lc)
}

fn floor_apca_light(fg: Color, bg: Color, min_lc: f32) -> Color {
if apca_lc(fg, bg) >= min_lc { return fg; }
let [l0, ..] = srgb_to_oklch(fg);
search_l(fg, 0.15, l0, |c| apca_lc(c, bg) <= min_lc)
}

fn floor_wcag_light(fg: Color, bg: Color, min_ratio: f32) -> Color {
if wcag_ratio(fg, bg) >= min_ratio { return fg; }
let [l0, ..] = srgb_to_oklch(fg);
search_l(fg, 0.15, l0, |c| wcag_ratio(c, bg) <= min_ratio)
}
Loading