macos: only set titlebar color with enough luminance to avoid #1549

This commit is contained in:
Mitchell Hashimoto
2024-03-26 11:13:39 -07:00
parent 8f873dd488
commit f7129880f5
2 changed files with 21 additions and 7 deletions

View File

@ -246,8 +246,12 @@ class TerminalController: NSWindowController, NSWindowDelegate,
// when cascading.
window.center()
// Set the background color of the window
window.backgroundColor = NSColor(ghostty.config.backgroundColor)
// Set the background color of the window. We only do this if the lum is
// over 0.1 to prevent: https://github.com/mitchellh/ghostty/issues/1549
let bgColor = NSColor(ghostty.config.backgroundColor)
if (bgColor.luminance > 0.1) {
window.backgroundColor = NSColor(ghostty.config.backgroundColor)
}
// This makes sure our titlebar renders correctly when there is a transparent background
window.titlebarOpacity = ghostty.config.backgroundOpacity

View File

@ -2,19 +2,29 @@ import Foundation
extension OSColor {
var isLightColor: Bool {
return self.luminance > 0.5
}
var luminance: Double {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)
let luminance = (0.299 * r) + (0.587 * g) + (0.114 * b)
return luminance > 0.5
// getRed:green:blue:alpha requires sRGB space
guard let rgb = self.usingColorSpace(.sRGB) else { return 0 }
rgb.getRed(&r, green: &g, blue: &b, alpha: &a)
return (0.299 * r) + (0.587 * g) + (0.114 * b)
}
func darken(by amount: CGFloat) -> OSColor {
var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
self.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
return OSColor(hue: h, saturation: s, brightness: min(b * (1 - amount), 1), alpha: a)
return OSColor(
hue: h,
saturation: s,
brightness: min(b * (1 - amount), 1),
alpha: a
)
}
}