A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
//! Colour arithmetic for the interface's own chrome.
use window::color::LinearRgba;
/// `from` moved `t` of the way towards `to`, **in sRGB space**, alpha
/// included.
///
/// The blend must happen in sRGB, not in the linear values these colours
/// are stored as. Lerping 10% of the way from near-black to near-white in
/// *linear* space lands around 34% in sRGB — so greys meant to sit just off the
/// page came out as mid-greys.
pub(crate) fn mix(from: LinearRgba, to: LinearRgba, t: f32) -> LinearRgba {
let t = t.clamp(0.0, 1.0);
let lerp = |a: f32, b: f32| srgb_decode(srgb_encode(a) + (srgb_encode(b) - srgb_encode(a)) * t);
LinearRgba::with_components(
lerp(from.0, to.0),
lerp(from.1, to.1),
lerp(from.2, to.2),
from.3 + (to.3 - from.3) * t,
)
}
/// `ground` moved `step` of perceived lightness (OkLab L; a negative step
/// darkens), keeping its tint. A step reads the same on any ground, where a
/// grey laid over it at a fixed alpha is plain on a dark one and all but
/// lost on a light one.
pub(crate) fn lightness_step(ground: LinearRgba, step: f32) -> LinearRgba {
let [l, a, b, alpha] = ground.to_oklaba();
from_oklab_clamped(l + step, a, b, alpha)
}
/// `ground` a `step` lighter, a card lifted off it -- or, where it is too
/// light for that to show (a near-white ground has no room above it), the
/// same step darker.
pub(crate) fn lifted(ground: LinearRgba, step: f32) -> LinearRgba {
let l = ground.to_oklaba()[0];
lightness_step(ground, if l + step <= 1.0 { step } else { -step })
}
/// The colour of perceived lightness `l` (OkLab L, kept within 0..=1) with
/// the tint `a`, `b`, clamped into what a screen shows.
pub(crate) fn from_oklab_clamped(l: f32, a: f32, b: f32, alpha: f32) -> LinearRgba {
let out = LinearRgba::from_oklaba(l.clamp(0.0, 1.0), a, b, alpha);
LinearRgba::with_components(
out.0.clamp(0.0, 1.0),
out.1.clamp(0.0, 1.0),
out.2.clamp(0.0, 1.0),
alpha,
)
}
// The standard sRGB transfer function, written out rather than reached for on
// `LinearRgba`/`SrgbaTuple`: those two use different curves (one exact, one a
// gamma-2.2 approximation) and so do not round-trip. These are exact inverses,
// which is what a blend needs.
pub(crate) fn srgb_encode(c: f32) -> f32 {
if c <= 0.003_130_8 {
c * 12.92
} else {
1.055 * c.powf(1.0 / 2.4) - 0.055
}
}
pub(crate) fn srgb_decode(c: f32) -> f32 {
if c <= 0.040_45 {
c / 12.92
} else {
((c + 0.055) / 1.055).powf(2.4)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mix_blends_perceptually_not_in_linear_space() {
let black = LinearRgba::with_srgba(0, 0, 0, 255);
let white = LinearRgba::with_srgba(255, 255, 255, 255);
let tenth = srgb_encode(mix(black, white, 0.10).0);
assert!(
(tenth - 0.10).abs() < 0.02,
"10% of the way to white should be ~10% in sRGB, got {}",
tenth
);
// Halfway is mid-grey to the eye, not the much lighter linear midpoint.
let half = srgb_encode(mix(black, white, 0.5).0);
assert!((half - 0.5).abs() < 0.02, "midpoint drifted to {}", half);
}
#[test]
fn mix_carries_alpha_along() {
let clear = LinearRgba::with_components(1.0, 1.0, 1.0, 0.2);
let solid = LinearRgba::with_components(1.0, 1.0, 1.0, 1.0);
assert!((mix(clear, solid, 0.5).3 - 0.6).abs() < 1e-6);
assert_eq!(mix(clear, clear, 0.7).3, 0.2);
}
#[test]
fn a_lightness_step_is_the_same_on_dark_and_light_and_keeps_the_tint() {
let l = |c: LinearRgba| c.to_oklaba()[0];
let dark = LinearRgba::with_srgba(18, 18, 20, 255);
let light = LinearRgba::with_srgba(226, 226, 232, 255);
assert!((l(lightness_step(dark, 0.075)) - l(dark) - 0.075).abs() < 1e-3);
let darker = lightness_step(light, -0.075);
assert!((l(light) - l(darker) - 0.075).abs() < 1e-3);
// A blue-grey ground steps to a blue-grey.
assert!(darker.2 > darker.0);
assert_eq!(darker.3, 1.0);
}
#[test]
fn a_lift_shows_even_on_a_near_white_ground() {
let l = |c: LinearRgba| c.to_oklaba()[0];
let dark = LinearRgba::with_srgba(18, 18, 20, 255);
assert!((l(lifted(dark, 0.075)) - l(dark) - 0.075).abs() < 1e-3);
// No room above white: a step down instead of none at all.
let white = LinearRgba::with_srgba(252, 252, 253, 255);
assert!((l(white) - l(lifted(white, 0.075)) - 0.075).abs() < 1e-3);
}
}