1

I'm using the answer in "How to change a particular color in an image?" to change 2 colors in my image to other two color. This works great if the colors in my images are like green and blue, but the colors that I have to use are:

- #BBBBBB (hsla(0, 0%, 73%, 1))
- #FFFFFF (hsla(0, 100%, 100%, 1))

How do I check if the range of my colors match if the hue basically could be between 0 and 360. Do I need to compare the saturation and light also? Or do I compare the colors different way?

Code I've now is:

image.colorize {
    let hsv = UIColor.hsv($0, $1, $2)

    // Do some range checks for my two colors here set the colors if needed
}

extension UIImage {
    func colorize(transform: (inout Float, inout Float, inout Float, inout Float) -> Void) -> UIImage? {
        guard let ciImage = CIImage(image: self) else {
            return nil
        }

        let size = 64
        let cubeDataSize = size * size * size * 4
        guard let cubeData = NSMutableData(capacity: cubeDataSize * MemoryLayout<Float>.size) else {
            return nil
        }

        for z in 0..<size {
            let blue = Float(z) / Float(size - 1)
            for y in 0..<size {
                let green = Float(y) / Float(size - 1)
                for x in 0..<size {
                    let red = Float(x) / Float(size - 1)

                    var premultipliedRed = red
                    var premultipliedGreen = green
                    var premultipliedBlue = blue
                    var alpha = Float(0)

                    transform(&premultipliedRed, &premultipliedGreen, &premultipliedBlue, &alpha)

                    cubeData.append(&premultipliedRed, length: MemoryLayout<Float>.size)
                    cubeData.append(&premultipliedGreen, length: MemoryLayout<Float>.size)
                    cubeData.append(&premultipliedBlue, length: MemoryLayout<Float>.size)
                    cubeData.append(&alpha, length: MemoryLayout<Float>.size)
                }
            }
        }

        guard let filter = CIFilter(name: "CIColorCube", parameters: [
            "inputImage": ciImage,
            "inputCubeDimension": NSNumber(value: size),
            "inputCubeData": cubeData
        ]) else {
            return nil
        }

        guard let outputImage = filter.outputImage else {
            return nil
        }

        return UIImage(ciImage: outputImage)
    }
}

extension UIColor {
    static func hsv(_ red: Float,
                    _ green: Float,
                    _ blue: Float) -> (Float, Float, Float) {
        let color = UIColor(red: CGFloat(red),
                            green: CGFloat(green),
                            blue: CGFloat(blue),
                            alpha: 1)

        var hue: CGFloat = 0
        var sat: CGFloat = 0
        var brightness: CGFloat = 0
        color.getHue(&hue, saturation: &sat, brightness: &brightness, alpha: nil)

        return (Float(hue), Float(sat), Float(brightness))
    }
}
Haagenti
  • 7,974
  • 5
  • 38
  • 52
  • Does this help? https://stackoverflow.com/questions/8046643/how-to-change-a-particular-color-in-an-image/32638622#32638622 – AnupamChugh Jun 25 '20 at 17:39

0 Answers0