swift – Why is iOS CILightenBlendMode altering the sunshine shade?

0
17
swift – Why is iOS CILightenBlendMode altering the sunshine shade?


I’ve the next supply picture:

enter image description here

I’m attempting to transform it to:

enter image description here

I’ve constructed this code:

import CoreImage

if let picture = UIImage(named: "demo9"), let editted = applyDuotoneEffect(to: picture) {
    imageView.picture = editted
}

func applyDuotoneEffect(to picture: UIImage) -> UIImage? {
    guard let ciImage = CIImage(picture: picture) else { return nil }
    
    let context = CIContext(choices: nil)
    
    let grayscaleFilter = CIFilter(title: "CIPhotoEffectMono", parameters: [kCIInputImageKey: ciImage])
    
    let solidLightColorImage = CIImage(shade: CIColor(cgColor: ("efa403".toUIColor() ?? .crimson).cgColor)).cropped(to: ciImage.extent)
    let multiplyFilter = CIFilter(title: "CIMultiplyBlendMode", parameters: [kCIInputImageKey: grayscaleFilter?.outputImage as Any, kCIInputBackgroundImageKey: solidLightColorImage as Any])
       
    
    let solidDarkColorImage = CIImage(shade: "290a59".toCIColor() ?? .black).cropped(to: ciImage.extent)
    let lightenFilter = CIFilter(title: "CILightenBlendMode", parameters: [kCIInputImageKey: multiplyFilter?.outputImage as Any, kCIInputBackgroundImageKey: solidDarkColorImage as Any])
    
    guard let outputImage = lightenFilter?.outputImage,
          let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
    
    return UIImage(cgImage: cgImage)
}

extension String {
    func toUIColor() -> UIColor? {
        var cString = trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
        
        if (cString.hasPrefix("#")) {
            cString.take away(at: cString.startIndex)
        }
        
        if ((cString.rely) != 6) {
            return nil
        }
        
        var rgbValue: UInt64 = 0
        Scanner(string: cString).scanHexInt64(&rgbValue)
        
        return UIColor(
            crimson: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            inexperienced: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}

Nonetheless, the yellow shade background would not look correct within the output:

enter image description here

It seems that the CILightenBlendMode filter adjustments the yellow shade too regardless that I anticipated it to solely change the blacks to purple. What am I doing fallacious?

LEAVE A REPLY

Please enter your comment!
Please enter your name here