Hey I am new in programming and I want to know if it's possible to create a gradient effect having 2 colors (red and blue for example) of type UInt32. Any suggestions? Thanks
ADDITION TO QUESTION
The idea is that I have only pixels informations:
func fillRegion(pixelX: Int, pixelY: Int, withColor color: UIColor) {
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
let newColor = (UInt32)(alpha*255)<<24 | (UInt32)(red*255)<<16 | (UInt32)(green*255)<<8 | (UInt32)(blue*255)<<0
let pixelColor = regionsData.advanced(by: (pixelY * imageHeight) + pixelX).pointee
if pixelColor == blackColor { return }
var pointerRegionsData: UnsafeMutablePointer<UInt32> = regionsData
var pointerImageData: UnsafeMutablePointer<UInt32> = imageData
var pixelsChanged = false
for i in 0...(imageHeight * imageHeight - 1) {
if pointerRegionsData.pointee == pixelColor {
pointerImageData = imageData.advanced(by: i)
if pointerImageData.pointee != newColor {
pointerImageData.pointee = newColor
pixelsChanged = true
}
}
pointerRegionsData = pointerRegionsData.successor()
}
if pixelsChanged {
self.image = UIImage(cgImage: imageContext.makeImage()!)
DispatchQueue.main.async {
CATransaction.setDisableActions(true)
self.layer.contents = self.image.cgImage
self.onImageDraw?(self.image)
}
self.playTapSound()
}
}
now it's filling with simple color, but I need to set a gradient somehow, I do not have a proper form, and I can't manage to set CAShapeLayer... as I have only pixel number information, and his color, though I am able to change the color here :
if pointerImageData.pointee != newColor {
pointerImageData.pointee = newColor
pixelsChanged = true
}
But I don't know how to make a smooth changing of color, to make gradient effect, or to identify somehow the perimeter and set up a layer, dunno, any ideas will be as gold.