Contents

0xwdg/colors

Colors is a Swift Package to enable all system colors in SwiftUI trough a Color extension.

Requirements

  • Swift 5.9+ (Xcode 15+)
  • iOS 13+, macOS 10.15+

Installation (Pakage.swift)

dependencies: [
    .package(url: "https://github.com/0xWDG/Colors.git", branch: "main"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "Colors", package: "Colors"),
    ]),
]

Installation (Xcode)

  1. In Xcode, open your project and navigate to File β†’ Swift Packages β†’ Add Package Dependency...
  2. Paste the repository URL (https://github.com/0xWDG/Colors) and click Next.
  3. Click Finish.

Usage

import SwiftUI
import Colors

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
                .foregroundColor(Color.disabledControlTextColor)
        }
        .padding()
    }
}

Extract color from UIColor/NSColor

Use this to add new/missing colors to the BaseColor and Color extension.

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)
)

How to add a new color

  1. Add the color to the BaseColor struct.

``swift /// 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) ) ``

  1. Add the color to the Color extension.

- Use the native Color,NSColor,UIColor.colorName where possible. - Add #if os(iOS) / #if os(macOS) where needed. - Example (works on almost all versions): ``swift /// A color that represents the system-provided pink color. public static var systemPink: Color { #if os(iOS) || os(tvOS) Color(UIColor.systemPink) #elseif os(macOS) Color(NSColor.systemPink) #else BaseColor.systemPink #endif } ` - Example 2 (works from a specific iOS/macOS version): `swift /// A color that represents the system-provided cyan color. public static var systemCyan: Color { #if os(iOS) || os(tvOS) if #available(iOS 15.0, ) { Color(UIColor.systemCyan) } else { BaseColor.systemCyan } #elseif os(macOS) if #available(macOS 12.0, ) { Color(NSColor.systemCyan) } else { BaseColor.systemCyan } #else BaseColor.systemCyan #endif } ``

Contact

πŸ¦‹ @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧡 @0xWDG 🌐 wesleydegroot.nl πŸ€– Discord

Interested learning more about Swift? Check out my blog.

Package Metadata

Repository: 0xwdg/colors

Default branch: main

README: README.md