Skip to content

Commit

Permalink
Add preference to prefix Hex color with “#”
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
sindresorhus committed Jun 20, 2021
1 parent c8fff46 commit 6416f65
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Color Picker/ColorPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private struct BarView: View {

struct ColorPickerView: View {
@Default(.uppercaseHexColor) private var uppercaseHexColor
@Default(.hashPrefixInHexColor) private var hashPrefixInHexColor
@Default(.legacyColorSyntax) private var legacyColorSyntax
@Default(.shownColorFormats) private var shownColorFormats
@State private var hexColor = ""
Expand Down Expand Up @@ -199,6 +200,9 @@ struct ColorPickerView: View {
.onChange(of: uppercaseHexColor) { _ in
updateColorsFromPanel()
}
.onChange(of: hashPrefixInHexColor) { _ in
updateColorsFromPanel()
}
.onChange(of: legacyColorSyntax) { _ in
updateColorsFromPanel()
}
Expand Down Expand Up @@ -249,7 +253,12 @@ struct ColorPickerView: View {

extension NSColorPanel {
var hexColorString: String {
color.usingColorSpace(.sRGB)!.format(.hex(isUppercased: Defaults[.uppercaseHexColor]))
color.usingColorSpace(.sRGB)!.format(
.hex(
isUppercased: Defaults[.uppercaseHexColor],
hasPrefix: Defaults[.hashPrefixInHexColor]
)
)
}

var hslColorString: String {
Expand Down
1 change: 1 addition & 0 deletions Color Picker/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extension Defaults.Keys {
static let colorFormatToCopyAfterPicking = Key<CopyColorFormat>("colorFormatToCopyAfterPicking", default: .none)
static let stayOnTop = Key<Bool>("stayOnTop", default: true)
static let uppercaseHexColor = Key<Bool>("uppercaseHexColor", default: false)
static let hashPrefixInHexColor = Key<Bool>("hashPrefixInHexColor", default: false)
static let legacyColorSyntax = Key<Bool>("legacyColorSyntax", default: false)
static let shownColorFormats = Key<Set<ColorFormat>>("shownColorFormats", default: [.hex, .hsl, .rgb, .lch])
}
Expand Down
1 change: 1 addition & 0 deletions Color Picker/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private struct GeneralSettings: View {
Defaults.Toggle("Stay on top", key: .stayOnTop)
.help("Make the color picker window stay on top of all other windows.")
Defaults.Toggle("Uppercase Hex color", key: .uppercaseHexColor)
Defaults.Toggle("Prefix Hex color with #", key: .hashPrefixInHexColor)
Defaults.Toggle("Use legacy syntax for HSL and RGB", key: .legacyColorSyntax)
.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.")
Link("What is LCH color?", destination: "https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/")
Expand Down

0 comments on commit 6416f65

Please sign in to comment.