Skip to content

Commit

Permalink
Expose Repeat Option
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakr233 committed Aug 13, 2024
1 parent 7df6a12 commit 03cb783
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions Sources/ColorfulX/AnimatedMulticolorGradientView+SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ public struct AnimatedMulticolorGradientViewRepresentable {
@Binding var transitionSpeed: Double

let frameLimit: Int
let repeatToFillColorSlots: Bool

public init(
color: Binding<[ColorVector]>,
speed: Binding<Double> = .constant(1),
bias: Binding<Double> = .constant(0.01),
noise: Binding<Double> = .constant(0),
transitionSpeed: Binding<Double> = .constant(3.25),
frameLimit: Int = 0
frameLimit: Int = 0,
repeatToFillColorSlots: Bool = true
) {
_color = color
_speed = speed
Expand All @@ -32,10 +34,15 @@ public struct AnimatedMulticolorGradientViewRepresentable {
_transitionSpeed = transitionSpeed

self.frameLimit = frameLimit
self.repeatToFillColorSlots = repeatToFillColorSlots
}

public func updatePropertyToView(_ view: AnimatedMulticolorGradientView) {
view.setColors(color, interpolationEnabled: transitionSpeed > 0)
view.setColors(
color,
interpolationEnabled: transitionSpeed > 0,
repeatToFillColorSlots: repeatToFillColorSlots
)
view.speed = speed
view.bias = bias
view.noise = noise
Expand Down
4 changes: 2 additions & 2 deletions Sources/ColorfulX/AnimatedMulticolorGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ open class AnimatedMulticolorGradientView: MulticolorGradientView {
initializeRenderParameters()
}

public func setColors(_ colors: [ColorVector], interpolationEnabled: Bool = true, fillColorsIfNeeded: Bool = true) {
public func setColors(_ colors: [ColorVector], interpolationEnabled: Bool = true, repeatToFillColorSlots: Bool = true) {
var colors = colors
if colors.isEmpty { colors.append(.init(v: .zero, space: .rgb)) }
colors = colors.map { $0.color(in: .lab) }

let endingIndex = fillColorsIfNeeded ? Uniforms.COLOR_SLOT : min(colors.count, Uniforms.COLOR_SLOT)
let endingIndex = repeatToFillColorSlots ? Uniforms.COLOR_SLOT : min(colors.count, Uniforms.COLOR_SLOT)
guard endingIndex > 0 else { return }
for idx in 0 ..< endingIndex {
var read = colorElements[idx]
Expand Down

0 comments on commit 03cb783

Please sign in to comment.