diff --git a/BlurUIKit/Internal/GradientImageRenderer.swift b/BlurUIKit/Internal/GradientImageRenderer.swift index 2bc8dac..c082345 100644 --- a/BlurUIKit/Internal/GradientImageRenderer.swift +++ b/BlurUIKit/Internal/GradientImageRenderer.swift @@ -76,10 +76,10 @@ import UIKit bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue ), let buffer = context.data else { return nil } - let pixels = buffer.assumingMemoryBound(to: UInt8.self) + let pixels: UnsafeMutablePointer = buffer.assumingMemoryBound(to: UInt8.self) // Clamp startLocation to valid range - let clampedStartLocation = min(max(startLocation, 0.0), 1.0) + let clampedStartLocation: CGFloat = min(max(startLocation, 0.0), 1.0) // Precompute reciprocals used in the gradient calculation let lengthReciprocal: CGFloat = length > 1 ? 1.0 / CGFloat(length - 1) : 0.0 @@ -91,29 +91,37 @@ import UIKit guard length > 1, clampedStartLocation > 0.0 else { return 0 } return min(Int(clampedStartLocation * CGFloat(length - 1)) + 1, length) }() - - // Bulk-fill the constant region before the gradient (no per-pixel math needed) - if gradientStartPixel > 0 { - let constantAlpha: UInt8 = reversed ? 0 : 255 - for i in stride(from: 0, to: gradientStartPixel * bytesPerPixel, by: bytesPerPixel) { - pixels[i] = 0 - pixels[i + 1] = constantAlpha - } + + let gradientStart: Int = reversed ? 0 : gradientStartPixel + let gradientEnd: Int = reversed ? (length - gradientStartPixel) : length + let constantStart: Int = reversed ? gradientEnd : 0 + let constantEnd: Int = reversed ? length : gradientStartPixel + + // Constant opaque region + for i in stride(from: constantStart * bytesPerPixel, + to: constantEnd * bytesPerPixel, + by: bytesPerPixel) { + pixels[i] = 0 + pixels[i + 1] = 255 } - - // Generate the gradient transition for the remaining pixels - for i in gradientStartPixel..