Caleb
Caleb

Reputation: 305

How to apply tint color to UIImage using Core Image

I am searching to find solution to apply tint color to image without changing resolution and size. 
I am creating custom filters using RGB values and CIColorControls. I am able to apply different color controls (i.e brightness, contrast & saturation).

I have the values, Contrast , Brightness, Saturation as (110%., 110%, 130%) And I have applied its working fine. This is my code so far:



func applyCustomValues(image: UIImage, brightness: Double, contrast: Double, saturation: Double) -> UIImage{

    let beginImage = CIImage(cgImage: image.cgImage!)
    let parameters = [
        "inputContrast": NSNumber(value: contrast),
        "inputBrightness": NSNumber(value: brightness),
        "inputSaturation": NSNumber(value: saturation),
        ]
    let outputImage = beginImage.applyingFilter("CIColorControls", parameters: parameters)

    let context = CIContext(options: nil)

    let outputCGImage = context.createCGImage(outputImage, from: outputImage.extent)

    return UIImage(cgImage: outputCGImage)
}

And the RGB values I have R = 243, G = 106, B = 188.

I would like to apply all those value on image and expecting output as like requirement

To apply tint color (RGB) This is my code so far:



func tint(image: UIImage, color: UIColor) -> UIImage
{
    let ciImage = CIImage(image: image)
    let filter = CIFilter(name: "CIMultiplyCompositing")
    filter?.setDefaults()

    let colorFilter = CIFilter(name: "CIConstantColorGenerator")
    let ciColor = CIColor(color: color)
    colorFilter?.setValue(ciColor, forKey: kCIInputColorKey)
    let colorImage = colorFilter?.outputImage

    filter?.setValue(colorImage, forKey: kCIInputImageKey)
    filter?.setValue(ciImage, forKey: kCIInputBackgroundImageKey)
    let context = CIContext(options: nil)
    let cgImage = context.createCGImage(filter!.outputImage!, from: (ciImage?.extent)!)
    return UIImage(cgImage: cgImage!)
}

To apply Tint and CIColorControls,

   let tintImage = tint(image:orinalImage, color: UIColor.getTintColor(r: 255/255, g: 131/255, b: 0/255, alpha: 1))

   let colorControlImage = applyCustomValues(image: tintImage, brightness: 0.11, contrast: 1.10, saturation: 1.3)

It returns the output image attcheched below,

enter image description here

Original image is,

enter image description here

expected output image Please ignore the scale of image Please ignore the scale of image

Please correct me, if I am wrong what is the correct way to achieve this.

Upvotes: 2

Views: 1727

Answers (1)

Caleb
Caleb

Reputation: 305

Finally, I got the answer what I wanted, May it helps to someone.

func colorized(with color: UIColor) -> UIImage? {
    guard
        let ciimage = CIImage(image: self),
        let colorMatrix = CIFilter(name: "CIColorMatrix")
        else { return nil }
    var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
    color.getRed(&r, green: &g, blue: &b, alpha: &a)
    colorMatrix.setDefaults()
    colorMatrix.setValue(ciimage, forKey: "inputImage")
    colorMatrix.setValue(CIVector(x: r, y: 0, z: 0, w: 0), forKey: "inputRVector")
    colorMatrix.setValue(CIVector(x: 0, y: g, z: 0, w: 0), forKey: "inputGVector")
    colorMatrix.setValue(CIVector(x: 0, y: 0, z: b, w: 0), forKey: "inputBVector")
    colorMatrix.setValue(CIVector(x: 0, y: 0, z: 0, w: a), forKey: "inputAVector")
    if let ciimage = colorMatrix.outputImage {
        return UIImage(ciImage: ciimage)
    }
    return nil
}

Upvotes: 7

Related Questions