ColorfulX is a high-performance library designed for creating vibrant & animated mesh gradient views.
https://github.com/Lakr233/ColorfulX.git
ColorfulX is a Metal-backed gradient renderer for Apple platforms. It combines LAB color interpolation, spring-based animation, and a configurable compute pipeline to produce vivid multicolor backgrounds from SwiftUI, UIKit, and AppKit.
AnimatedMulticolorGradientView drives time-based updates with frame limiting, noise, bias, and transition controls.ColorfulView exposes the renderer to SwiftUI using familiar bindings, while MulticolorGradient covers static gradients.ColorfulPreset) and the ColorfulColors protocol make it easy to capture repeatable palettes.Example/ColorfulApp) showcases live controls for every parameter, including frame limiting and render scaling.// swift-tools-version: 5.9 in Package.swift).Package.swift).Add ColorfulX to your package dependencies:
.package(url: "https://github.com/Lakr233/ColorfulX.git", from: "5.8.0")
Add ColorfulX to any target that needs the library:
.target(
name: "MyApp",
dependencies: ["ColorfulX"]
)
Xcode users can also add the package through File β Add Packagesβ¦ and paste the repository URL.
Example/ColorfulApp.xcodeproj or the workspace inside Example/ColorfulApp.xcworkspace.ChessboardView) helps visualise transparency on visionOS/macOS.You do import ColorfulX and then ColorfulView(color: .aurora). Done.
import ColorfulX
import SwiftUI
struct AnimatedGradientDemo: View {
@State private var preset: ColorfulPreset = .aurora
@State private var speed: Double = 1.0
@State private var bias: Double = 0.01
@State private var noise: Double = 8.0
@State private var transition: Double = 3.5
@State private var frameLimit: Int = 60
@State private var renderScale: Double = 1.0
var body: some View {
ColorfulView(
color: $preset,
speed: $speed,
bias: $bias,
noise: $noise,
transitionSpeed: $transition,
frameLimit: $frameLimit,
renderScale: $renderScale
)
.ignoresSafeArea()
}
}
ColorfulView also accepts bindings to [Color], constant presets (ColorfulPreset), or any custom type conforming to ColorfulColors. The animationDirector parameter allows customizing animation behavior (e.g., using SpeckleAnimationRoundedRectangleDirector for path-based movement). Internally the view converts data to ColorVector instances and feeds AnimatedMulticolorGradientViewRepresentable.
Generally, we recommend using ColorfulView with speed set to 0 for that.
For non-animated backgrounds use MulticolorGradient (SwiftUI wrapper for MulticolorGradientView):
import ColorfulX
import SwiftUI
import ColorVector
struct StaticGradientDemo: View {
private let parameters = MulticolorGradientView.Parameters(
points: [
.init(
color: ColorVector(UIColor.systemPink, usingSpace: .lab),
position: .init(x: 0.0, y: 0.0)
),
.init(
color: ColorVector(UIColor.systemBlue, usingSpace: .lab),
position: .init(x: 1.0, y: 1.0)
)
],
bias: 0.01,
power: 4,
noise: 0
)
var body: some View {
MulticolorGradient(parameters: parameters)
.ignoresSafeArea()
}
}
The underlying shader supports up to eight color stops (Uniforms.COLOR_SLOT). When fewer colors are supplied, the view can repeat stops to fill the pipeline.
import ColorfulX
let animatedView = AnimatedMulticolorGradientView()
animatedView.setColors(ColorfulPreset.aurora)
animatedView.speed = 1.2
animatedView.bias = 0.01
animatedView.noise = 12
animatedView.transitionSpeed = 4.0
animatedView.frameLimit = 60
animatedView.renderScale = 1.0
setColors accepts ColorfulPreset, any ColorfulColors value, [ColorVector], or [ColorElement] (UIColor / NSColor). Pass animated: false to swap palettes instantly or repeats: false to avoid repeating colors when providing fewer than eight stops.
import ColorfulX
import ColorVector
let staticView = MulticolorGradientView()
staticView.parameters = .init(
points: [
.init(color: ColorVector(UIColor.systemOrange, usingSpace: .lab), position: .init(x: 0.0, y: 0.0)),
.init(color: ColorVector(UIColor.systemTeal, usingSpace: .lab), position: .init(x: 1.0, y: 0.5)),
.init(color: ColorVector(UIColor.systemPurple, usingSpace: .lab), position: .init(x: 0.2, y: 1.0))
],
bias: 0.015,
power: 4,
noise: 0
)
MulticolorGradientView renders immediately when parameters change and respects renderScale (via metalLink.scaleFactor) for performance tuning.
Sources/ColorfulX/ColorfulPreset.swift. Each case conforms to ColorfulColors, exposes a human-readable hint, and maps to up to eight LAB colors.ColorfulColors:enum MarketingTheme: ColorfulColors {
case hero
var colors: [ColorElement] {
[
make(227, 108, 155),
make(134, 90, 214),
make(73, 204, 236),
make(35, 219, 167)
]
}
}
ColorfulView(color: MarketingTheme.hero)) or UIKit/AppKit (animatedView.setColors(MarketingTheme.hero)).| Parameter | Applies To | Description |
|---|---|---|
speed | Animated views | Scales how quickly speckles traverse the canvas (moveDelta in AnimatedMulticolorGradientView+Update). |
bias | Animated & static | Controls gradient spread (passed to shader bias). Lower values harden the gradient shape; typical range 0.00001 ... 0.01. |
power | Static (MulticolorGradientView.Parameters) | Shapes falloff curve; default is 4. |
noise | Animated & static | Adds procedural noise in the shader; higher values cost more GPU time. |
transitionSpeed | Animated views | Determines how fast colors interpolate when palettes change. |
frameLimit | Animated views | Caps rendering frequency without altering vsync scheduling. Use 0 for unlimited. |
renderScale | Animated & static | Adjusts Metal drawable scale; lowering improves performance at the cost of resolution. |
repeats | Animated views | Fills unused color slots by repeating colors; disable when the number of stops is stable. |
frameLimit or renderScale on battery-constrained devices.speed to 0 for a frozen animated view, or rely on MulticolorGradient for static scenes.speed > 0 and that the view remains in a window; AnimatedMulticolorGradientView pauses when detached.animated: true (default) and ensure transitionSpeed is greater than zero when calling setColors.renderScale toward 1.0 or higher when using heavy noise.repeats is true or provide at least as many colors as needed (max eight).Bug reports and pull requests are welcome on GitHub issues. When contributing:
ColorfulX is released under the MIT License. See LICENSE.