You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-1Lines changed: 54 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,60 @@ ColorSet
11
11
About
12
12
-----
13
13
14
-
...
14
+
**`ColorSet` is a macOS utility and framework allowing developers to manage custom interface colors with ease.**
15
+
16
+
### Rationale
17
+
18
+
As of macOS 10.14 Mojave, Apple introduced a system-wide dark-mode.
19
+
Applications linked with the macOS 10.14 SDK now needs to react to the system appearance changes.
20
+
21
+
While built-in AppKit UI elements handle this perfectly, an application often relies on custom controls, custom drawing code, or at last custom colors.
22
+
23
+
As an example, the following code will perfectly react to the dark mode.
24
+
This is a system color, so everything is handled automatically by AppKit:
25
+
26
+
let color = NSColor.textColor
27
+
28
+
While the following won't change, as the color components are hard-coded:
29
+
30
+
let color = NSColor( deviceRed: 1, green: 0, blue: 0, alpha: 1 )
31
+
32
+
Apple provides a nice solution, relying on Asset Catalog files.
33
+
As of Xcode 10, you can create custom colors in Asset Catalogs, identified by a name, and optionally containing variants for a specific UI theme or device.
34
+
35
+
**Unfortunately, this feature is only available for applications running on macOS 10.13 and later.**
36
+
It basically means you'll have to drop support for macOS 10.12 and below.
37
+
38
+
If you need to support macOS 10.12 or below, **Apple's official recommendation (as seen during WWDC18) is to actually hard-code colors using conditional platform-detection code**.
39
+
40
+
**So here comes `ColorSet`.**
41
+
42
+
### ColorSet & ColorSetKit
43
+
44
+
`ColorSet` comes as a macOS application and as a macOS framework.
45
+
46
+
The application lets you create files containing colors and variants, just as an Asset Catalog File, while the framework allows you, in your application, to retrieve colors from the `.colorset` file using names, just as the macOS 10.13 SDK.
47
+
48
+
### Creating a `.colorset` file
49
+
50
+
The application lets you create as many colors as you want, each with a unique name.
51
+
For each color, an optional dark variant can be set:
52
+
53
+

54
+
55
+
### Using ColorSetKit
56
+
57
+
By default, `ColorSetKit` will look for a file named `Colors.colorset` in your application's bundle.
58
+
59
+
Colors can be retrieved using a custom method on `NSColor`:
60
+
61
+
let color = NSColor.colorFromColorSet( "MyCustomColor" )
62
+
63
+
By themselves, colors won't react to UI theme changes.
64
+
But the `colorFromColorSet` method will return the correct color, based on the current UI theme.
65
+
66
+
This means that if you use custom drawing code, you shouldn't cache color objects, but instead call `colorFromColorSet` directly from your drawing code.
67
+
This way, the correct color will be used each time your view is redrawn (which happens automatically when the UI theme changes).
0 commit comments