0

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.

  • 1
    What do you mean by "UInt32 Colors"? – Larme Jan 25 '17 at 11:09
  • @Larme color of type: var newColor = (UInt32)(alpha*255)<<24 | (UInt32)(red*255)<<16 | (UInt32)(green*255)<<8 | (UInt32)(blue*255)<<0 – Dumitru Rogojinaru Jan 25 '17 at 11:10
  • To do a gradient, you need an array of CGColor, if I remember correctly, so you need to convert your colorClass into UIColor, then into CGColor. – Larme Jan 25 '17 at 11:11
  • @Larme i need to make gradient Effect, as I can not use Gradient Layers... I have no other choice... – Dumitru Rogojinaru Jan 25 '17 at 11:16
  • To make that gradient effects, what kind of objects/parameters do you need? Because the iOS system usually use UIColor or CGColor, so it's unclear. It's all about conversions into the one you want. – Larme Jan 25 '17 at 11:19
  • I know how to make that with UIColor and CGColor, but now I am using UnsafeMutablePointer for regions, and I change the color of every pixel in the region I need, it works good with simple color, but I need to set up a gradient, the form of the area is not proper and differs all the time, check my answer, I posted some coding and a description. @Larme – Dumitru Rogojinaru Jan 25 '17 at 11:23

1 Answers1

0

Yes it possible using CoreGraphics or CALayer.
To simplify, you can override the layer of a view returning a CAGradientLayer.
This is an implementation:

@implementation PGSGradientView

+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (instancetype) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initializeGradient];
    }
    return self;
}

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeGradient];
    }
    return self;
}

- (void) initializeGradient {
    [(CAGradientLayer*)self.layer setLocations:locations];
    [(CAGradientLayer*)self.layer setColors:colors];

}

@end

Imagine that you have 2 arrays as properties, one is locations and the other one are the colors that you want to add in the gradindient.
locations represents:

the starting location for each color. Also, and that’s important, those numbers should mandatorily range from 0.0 to 1.0. and is paired with the colors arrays.

Andrea
  • 26,120
  • 10
  • 85
  • 131