Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/fwrs/oklchgradient

OKLCH gradients for SwiftUI
https://github.com/fwrs/oklchgradient

design gradient metal oklch shaders swift swiftui

Last synced: 4 days ago
JSON representation

OKLCH gradients for SwiftUI

Awesome Lists containing this project

README

        

# OKLCH gradients for SwiftUI

This is a drop-in replacement for SwiftUI's built-in [LinearGradient](https://developer.apple.com/documentation/swiftui/lineargradient), [RadialGradient](https://developer.apple.com/documentation/swiftui/radialgradient), [EllipticalGradient](https://developer.apple.com/documentation/swiftui/ellipticalgradient) and [AngularGradient](https://developer.apple.com/documentation/swiftui/angulargradient) shape styles that utilizes OKLCH color blending to create more visually appealing gradients.

Implemented using iOS 17's [Shader](https://developer.apple.com/documentation/swiftui/shader) API, since that seems to be the only way to write shape styles with custom rendering without relying on private APIs.

Install using SPM:

```swift
dependencies: [
.package(url: "https://github.com/fwrs/OKLCHGradient.git", .upToNextMajor(from: "1.0.8"))
]
```

To use just prepend `OKLCH` to the gradient struct name:

```swift
// change

Rectangle()
.background(LinearGradient(
colors: [.blue, .yellow],
startPoint: .leading,
endPoint: .trailing
))

// to

import OKLCHGradient

Rectangle()
.background(OKLCHLinearGradient(
colors: [.blue, .yellow],
startPoint: .leading,
endPoint: .trailing
))
```

And enjoy the difference:

Screenshot comparing a regular SwiftUI gradient to an OKLCH gradient. The regular gradient utilizes a grey color as an intermediate between blue and yellow colors, while OKLCH uses green, which is the color positioned between blue and yellow on a standard color wheel.

> [!NOTE]
> At the moment it's not possible to pass AnyGradient structs to OKLCH gradients because there isn't a way to read color stops from an AnyGradient using public APIs. This functionality is limited to Apple's own built-in gradients.