fix: render light palettes with matching appearance
This commit is contained in:
@@ -10,6 +10,28 @@ struct ColorScheme {
|
||||
var glow: NSColor
|
||||
var keyBackground: NSColor
|
||||
var error: NSColor
|
||||
enum Variant: String {
|
||||
case light, dark
|
||||
}
|
||||
|
||||
var variant: Variant? = nil
|
||||
|
||||
var isLight: Bool {
|
||||
if let variant { return variant == .light }
|
||||
return Self.relativeLuminance(background) > Self.relativeLuminance(text)
|
||||
}
|
||||
|
||||
static func relativeLuminance(_ color: NSColor) -> CGFloat {
|
||||
let rgb = color.usingColorSpace(.sRGB)!
|
||||
return relativeLuminance(red: rgb.redComponent, green: rgb.greenComponent, blue: rgb.blueComponent)
|
||||
}
|
||||
|
||||
static func relativeLuminance(red: CGFloat, green: CGFloat, blue: CGFloat) -> CGFloat {
|
||||
func linear(_ value: CGFloat) -> CGFloat {
|
||||
value <= 0.04045 ? value / 12.92 : pow((value + 0.055) / 1.055, 2.4)
|
||||
}
|
||||
return 0.2126 * linear(red) + 0.7152 * linear(green) + 0.0722 * linear(blue)
|
||||
}
|
||||
|
||||
static let sunset = ColorScheme(
|
||||
background: NSColor(srgbRed: 0.12, green: 0.09, blue: 0.08, alpha: 1),
|
||||
@@ -49,7 +71,13 @@ struct ColorScheme {
|
||||
if theme == "tint" {
|
||||
let themeURL = directory.appendingPathComponent("theme.json")
|
||||
if let generated = try readObject(at: themeURL, missingAllowed: false) {
|
||||
try validateKeys(generated, allowed: ["colors"], at: themeURL, prefix: "")
|
||||
try validateKeys(generated, allowed: ["variant", "colors"], at: themeURL, prefix: "")
|
||||
if let value = generated["variant"] {
|
||||
guard let name = value as? String, let variant = Variant(rawValue: name) else {
|
||||
throw ColorSchemeError(path: themeURL.path, reason: "variant must be \"light\" or \"dark\".")
|
||||
}
|
||||
result.variant = variant
|
||||
}
|
||||
try result.applyColors(from: generated, at: themeURL)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user