Skip to content

Commit

Permalink
Add support for OKLCH color format
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
sindresorhus committed Jan 27, 2024
1 parent bb51b72 commit bd9ba52
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 321 deletions.
6 changes: 3 additions & 3 deletions Color Picker/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct AppMain: App {
// TODO: Change the default from LCH to OKLCH.
// We set this so we can change it later on.
SSApp.runOnce(identifier: "asdsadewr34323432432") {
Defaults[.shownColorFormats] = Defaults[.shownColorFormats]
Defaults[.shownColorFormats] = SSApp.isFirstLaunch ? [.hex, .hsl, .rgb, .oklch] : Defaults[.shownColorFormats]
}
}

Expand Down Expand Up @@ -51,7 +51,7 @@ struct AppMain: App {
Button("Paste") {
appState.pasteColor()
}
.help("Paste color in the format Hex, HSL, RGB, or LCH")
.help("Paste color in the format Hex, HSL, RGB, OKLCH, or LCH")
.keyboardShortcut("v", modifiers: [.shift, .command])
.disabled(Color.Resolved.fromPasteboardGraceful(.general) == nil)
Divider()
Expand All @@ -67,7 +67,7 @@ struct AppMain: App {
.keyboardShortcut("t", modifiers: [.control, .command])
}
CommandGroup(replacing: .help) {
Link("What is LCH color?", destination: "https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/")
Link("What is OKLCH color?", destination: "https://evilmartians.com/chronicles/oklch-in-css-why-quit-rgb-hsl")
Link("FAQ", destination: "https://github.com/sindresorhus/System-Color-Picker#faq")
Link("Website", destination: "https://sindresorhus.com/system-color-picker")
Divider()
Expand Down
12 changes: 10 additions & 2 deletions Color Picker/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ final class AppState {
colorPanel.standardWindowButton(.miniaturizeButton)?.isHidden = true
colorPanel.standardWindowButton(.zoomButton)?.isHidden = true
colorPanel.tabbingMode = .disallowed
colorPanel.collectionBehavior = [
.canJoinAllSpaces,

var collectionBehavior: NSWindow.CollectionBehavior = [
.fullScreenAuxiliary
// We cannot enable tiling as then it doesn't show up in fullscreen spaces. (macOS 12.5)
// .fullScreenAllowsTiling
]

if Defaults[.showOnAllSpaces] {
// If we remove this, the window cannot be dragged if it's moved into a fullscreen space. (macOS 14.3)
collectionBehavior.insert(.canJoinAllSpaces)
}

colorPanel.collectionBehavior = collectionBehavior

colorPanel.center()
colorPanel.makeMain()

Expand Down
8 changes: 8 additions & 0 deletions Color Picker/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ extension Defaults.Keys {
static let quitAfterPicking = Key<Bool>("quitAfterPicking", default: false)
static let showAccessibilityColorName = Key<Bool>("showAccessibilityColorName", default: false)
static let stickyPaletteName = Key<String?>("stickyPaletteName")

// Hidden settings
static let showOnAllSpaces = Key<Bool>("showOnAllSpaces", default: true)
}

extension KeyboardShortcuts.Name {
Expand All @@ -35,6 +38,7 @@ enum ColorFormat: String, CaseIterable, Defaults.Serializable {
case hex
case hsl
case rgb
case oklch
case lch

var title: String {
Expand All @@ -45,6 +49,8 @@ enum ColorFormat: String, CaseIterable, Defaults.Serializable {
"HSL"
case .rgb:
"RGB"
case .oklch:
"OKLCH"
case .lch:
"LCH"
}
Expand All @@ -58,6 +64,8 @@ enum ColorFormat: String, CaseIterable, Defaults.Serializable {
"s"
case .rgb:
"r"
case .oklch:
"o"
case .lch:
"l"
}
Expand Down
14 changes: 5 additions & 9 deletions Color Picker/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ struct MainScreen: View {
@Default(.showAccessibilityColorName) private var showAccessibilityColorName
@State private var isPreventingUpdate = false
@State private var focusedTextField: ColorFormat?

@State private var colorStrings: [ColorFormat: String] = [
.hex: "",
.hsl: "",
.rgb: "",
.lch: ""
]
@State private var colorStrings = EnumCaseMap<ColorFormat, String>(defaultValue: "")

let colorPanel: NSColorPanel

Expand Down Expand Up @@ -98,6 +92,8 @@ struct MainScreen: View {
Color.Resolved(cssHSLString: colorString)
case .rgb:
Color.Resolved(cssRGBString: colorString)
case .oklch:
Color.Resolved(cssOKLCHString: colorString)
case .lch:
Color.Resolved(cssLCHString: colorString)
}
Expand All @@ -114,7 +110,7 @@ struct MainScreen: View {
ForEach(ColorFormat.allCases.filter(allowedValues: shownColorFormats)) { colorFormat in
ColorInputView(
colorFormat: colorFormat,
colorString: $colorStrings[colorFormat, default: ""],
colorString: $colorStrings[colorFormat],
focusedTextField: $focusedTextField
) { newColor in
updateColorFromTextField(
Expand Down Expand Up @@ -222,7 +218,7 @@ private struct PasteColorButton: View {
.padding(8)
}
.contentShape(.rect)
.help("Paste color in the format Hex, HSL, RGB, or LCH")
.help("Paste color in the format Hex, HSL, RGB, OKLCH, or LCH")
.keyboardShortcut("v", modifiers: [.shift, .command])
.disabled(Color.Resolved.fromPasteboardGraceful(.general) == nil)
.onAppearOnScreen {
Expand Down
2 changes: 1 addition & 1 deletion Color Picker/SettingsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private struct ColorSettings: View {
.help("Use the legacy “hsl(198, 28%, 50%)” syntax instead of the modern “hsl(198deg 28% 50%)” syntax. This setting is meant for users that need to support older browsers. All modern browsers support the modern syntax.")
}
Section {} footer: {
Link("What is LCH color?", destination: "https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/")
Link("What is OKLCH color?", destination: "https://evilmartians.com/chronicles/oklch-in-css-why-quit-rgb-hsl")
.controlSize(.small)
}
}
Expand Down
Loading

0 comments on commit bd9ba52

Please sign in to comment.