Swift package: Colors

If you are developing for multiple Apple patforms, you may have noticed that when it comes to Colors, there are some differences between the different platforms.
For example, the UIColor class is used in iOS, NSColor in macOS, and Color in SwiftUI.

Especially in UIKit not all UIColors are available through all platforms.
For example, UIColor.systemGray2 is available for iOS, iPadOS, visionOS but not for tvOS and watchOS.
This can be a bit annoying when you want to write unified code for all Apple platforms.

Therefore i created a Swift Package Colors that provides a unified API for working with colors in Swift.
It is an extension to Color class in SwiftUI and provides a similar API for all Apple platforms.
If the selected color is native to the current platform it will use a call to the native color, if not it will use a dynamic (light/dark)-mode color from the Colors package.

The colors which are currently supported are

lightText, darkText, placeholderText, label, secondaryLabel, tertiaryLabel, quaternaryLabel, systemBackground, secondarySystemBackground, tertiarySystemBackground, systemFill, secondarySystemFill, tertiarySystemFill, quaternarySystemFill, systemGroupedBackground, secondarySystemGroupedBackground, tertiarySystemGroupedBackground, systemGray, systemGray2, systemGray3, systemGray4, systemGray5, systemGray6, separator, opaqueSeparator, link, systemBlue, systemCyan, systemMint, systemPurple, systemGreen, systemYellow, systemOrange, systemPink, systemRed, systemTeal, systemIndigo, scrubberTexturedBackground, textBackgroundColor, controlTextColor, quaternaryLabelColor, findHighlightColor, highlightColor, shadowColor, windowFrameTextColor, windowBackgroundColor, keyboardFocusIndicatorColor, separatorColor, selectedControlColor, controlBackgroundColor, secondaryLabelColor, tertiaryLabelColor, gridColor, alternateSelectedControlTextColor, unemphasizedSelectedContentBackgroundColor, textColor, systemBrown, selectedContentBackgroundColor, selectedTextColor, labelColor, placeholderTextColor, unemphasizedSelectedTextBackgroundColor, disabledControlTextColor, headerTextColor, linkColor, selectedTextBackgroundColor, unemphasizedSelectedTextColor, controlColor, selectedControlTextColor, underPageBackgroundColor, selectedMenuItemTextColor.

I hope this package will help you to write more unified code for your Apple platforms. :)

The package is available on GitHub and can be installed via Swift Package Manager. The package is open source and contributions are welcome.

GitHub: GitHub - 0xWDG/Colors: Colors is a Swift Package to enable all system colors in SwiftUI trough a Color extension. Colors which were previously only available in UIColor/NSColor are now available in Color as well.

How to use

import SwiftUI
import Colors

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
                .foregroundColor(Color.disabledControlTextColor)
                // Works on all Apple platforms instead of only on macOS
                // macOS: NSColor.disabledControlTextColor
                // Colors: Color.disabledControlTextColor
        }
        .padding()
    }
}

To add/extract color from UIColor/NSColor

Extract from UIKit:

UIColor.systemPink.createInitializerFor(color: "systemPink")

Extract from AppKit:

NSColor.systemPink.createInitializerFor(color: "systemPink")

Output:

/// A color that represents the system-provided systemPink color.
public static let systemPink = Color.dynamicColor(
    light: .init(red: 1.00, green: 0.18, blue: 0.33, alpha: 1.00),
    dark: .init(red: 1.00, green: 0.18, blue: 0.33, alpha: 1.00)
)
2 Likes