Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/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.
https://github.com/0xwdg/colors

0xwdg appkit color hacktoberfest nscolor package spm swift swiftlang swiftui uicolor uikit xcode

Last synced: 3 months ago
JSON representation

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.

Awesome Lists containing this project

README

        

# 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.

[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2F0xWDG%2FColors%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/0xWDG/Colors)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2F0xWDG%2FColors%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/0xWDG/Colors)
[![Swift Package Manager](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://swift.org/package-manager)
![License](https://img.shields.io/github/license/0xWDG/Colors)

Available colors 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`.

## Requirements

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

## Installation (Pakage.swift)

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

```swift
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**:
```swift
UIColor.systemPink.createInitializerFor(color: "systemPink")
```

Extract from **AppKit**:
```swift
NSColor.systemPink.createInitializerFor(color: "systemPink")
```

Output:
```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)
)
```

## 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)
)
```
2. 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

We can get in touch via [Mastodon](https://mastodon.social/@0xWDG), [Twitter/X](https://twitter.com/0xWDG), [Discord](https://discordapp.com/users/918438083861573692), [Email](mailto:[email protected]), [Website](https://wesleydegroot.nl).

Interested learning more about Swift? [Check out my blog](https://wesleydegroot.nl/blog/).