config: add window-theme = auto for automatic choosing based on bg color

This commit is contained in:
Mitchell Hashimoto
2024-02-01 20:43:42 -08:00
parent f0ed3d7ebe
commit e5400bad06
6 changed files with 57 additions and 14 deletions

View File

@@ -133,6 +133,14 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
c.adw_style_manager_set_color_scheme(
style_manager,
switch (config.@"window-theme") {
.auto => auto: {
const lum = config.background.toTerminalRGB().perceivedLuminance();
break :auto if (lum > 0.5)
c.ADW_COLOR_SCHEME_PREFER_LIGHT
else
c.ADW_COLOR_SCHEME_PREFER_DARK;
},
.system => c.ADW_COLOR_SCHEME_PREFER_LIGHT,
.dark => c.ADW_COLOR_SCHEME_FORCE_DARK,
.light => c.ADW_COLOR_SCHEME_FORCE_LIGHT,

View File

@@ -600,9 +600,13 @@ keybind: Keybinds = .{},
/// borders.
@"window-decoration": bool = true,
/// The theme to use for the windows. The default is `system` which means that
/// whatever the system theme is will be used. This can also be set to `light`
/// or `dark` to force a specific theme regardless of the system settings.
/// The theme to use for the windows. Valid values:
///
/// * `auto` - Determine the theme based on the configured terminal
/// background color.
/// * `system` - Use the system theme.
/// * `light` - Use the light theme regardless of system theme.
/// * `dark` - Use the dark theme regardless of system theme.
///
/// On macOS, if `macos-titlebar-tabs` is set, the window theme will be
/// automatically set based on the luminosity of the terminal background color.
@@ -610,7 +614,7 @@ keybind: Keybinds = .{},
/// non-terminal windows within Ghostty.
///
/// This is currently only supported on macOS and Linux.
@"window-theme": WindowTheme = .system,
@"window-theme": WindowTheme = .auto,
/// The colorspace to use for the terminal window. The default is `srgb` but
/// this can also be set to `display-p3` to use the Display P3 colorspace.
@@ -3342,6 +3346,7 @@ pub const OSCColorReportFormat = enum {
/// The default window theme.
pub const WindowTheme = enum {
auto,
system,
light,
dark,

View File

@@ -144,6 +144,17 @@ pub const RGB = struct {
return std.math.pow(f64, (normalized + 0.055) / 1.055, 2.4);
}
/// Calculates "perceived luminance" which is better for determining
/// light vs dark.
///
/// Source: https://www.w3.org/TR/AERT/#color-contrast
pub fn perceivedLuminance(self: RGB) f64 {
const r_f64: f64 = @floatFromInt(self.r);
const g_f64: f64 = @floatFromInt(self.g);
const b_f64: f64 = @floatFromInt(self.b);
return 0.299 * (r_f64 / 255) + 0.587 * (g_f64 / 255) + 0.114 * (b_f64 / 255);
}
test "size" {
try std.testing.expectEqual(@as(usize, 24), @bitSizeOf(RGB));
try std.testing.expectEqual(@as(usize, 3), @sizeOf(RGB));