meaning-matters
meaning-matters

Reputation: 22976

Apply CIHardLightBlendMode with 50% alpha

I'm blending a CIImage with a speckled grey (film grain like) file:

    var hardLightBlendFilter = CIFilter(name: "CIHardLightBlendMode")!
    var inputImage: CIImage = ...
    let grainImage = CIImage(cgImage: (UIImage(named: "Grain")?.cgImage)!)

    hardLightBlendFilter.setValue(inputImage, forKey: kCIInputBackgroundImageKey)
    hardLightBlendFilter.setValue(grainImage, forKey: kCIInputImageKey)

How can I apply this effect by only 50%?

I tried to find a way to set the alpha of grainImage to see what the effect of that would be. But couldn't figure that out yet.

Any ideas?

Upvotes: 0

Views: 175

Answers (1)

Frank Rupprecht
Frank Rupprecht

Reputation: 10408

It's not very intuitive, but you can use the CIColorMatrix filter to manipulate the image's alpha value:

let colorMatrixFilter = CIFilter(name: "CIColorMatrix")!
colorMatrixFilter.setValue(grainImage, forKey: kCIInputImageKey)
colorMatrixFilter.setValue(CIVector(x: 0.0, y: 0.0, z: 0.0, w: 0.5), forKey: "inputAVector") // where 0.5 is the factor applied to alpha
let transparentGainImage = colorMatrixFilter.outputImage!

Upvotes: 1

Related Questions