Last active
April 27, 2020 04:47
-
-
Save EyreFree/ab91a7f5b962147d0984e8ea114be766 to your computer and use it in GitHub Desktop.
iOS CIImage Replace color with another one in Swift 3.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct EFUIntPixel { | |
var red: UInt8 = 0 | |
var green: UInt8 = 0 | |
var blue: UInt8 = 0 | |
var alpha: UInt8 = 0 | |
} | |
extension CIImage { | |
// Replace color with another one | |
// https://github.com/dstarsboy/TMReplaceColorHue/blob/master/TMReplaceColorHue/ViewController.swift | |
func replace(colorOld: EFUIntPixel, colorNew: EFUIntPixel) -> CIImage? { | |
let cubeSize = 64 | |
let cubeData = { () -> [Float] in | |
let selectColor = (Float(colorOld.red) / 255.0, Float(colorOld.green) / 255.0, Float(colorOld.blue) / 255.0, Float(colorOld.alpha) / 255.0) | |
let raplaceColor = (Float(colorNew.red) / 255.0, Float(colorNew.green) / 255.0, Float(colorNew.blue) / 255.0, Float(colorNew.alpha) / 255.0) | |
var data = [Float](repeating: 0, count: cubeSize * cubeSize * cubeSize * 4) | |
var tempRGB: [Float] = [0, 0, 0] | |
var newRGB: (r : Float, g : Float, b : Float, a: Float) | |
var offset = 0 | |
for z in 0 ..< cubeSize { | |
tempRGB[2] = Float(z) / Float(cubeSize) // blue value | |
for y in 0 ..< cubeSize { | |
tempRGB[1] = Float(y) / Float(cubeSize) // green value | |
for x in 0 ..< cubeSize { | |
tempRGB[0] = Float(x) / Float(cubeSize) // red value | |
// Select colorOld | |
if tempRGB[0] == selectColor.0 && tempRGB[1] == selectColor.1 && tempRGB[2] == selectColor.2 { | |
newRGB = (raplaceColor.0, raplaceColor.1, raplaceColor.2, raplaceColor.3) | |
} else { | |
newRGB = (tempRGB[0], tempRGB[1], tempRGB[2], 1) | |
} | |
data[offset] = newRGB.r | |
data[offset + 1] = newRGB.g | |
data[offset + 2] = newRGB.b | |
data[offset + 3] = 1.0 | |
offset += 4 | |
} | |
} | |
} | |
return data | |
}() | |
let data = cubeData.withUnsafeBufferPointer { Data(buffer: $0) } as NSData | |
let colorCube = CIFilter(name: "CIColorCube")! | |
colorCube.setValue(cubeSize, forKey: "inputCubeDimension") | |
colorCube.setValue(data, forKey: "inputCubeData") | |
colorCube.setValue(self, forKey: kCIInputImageKey) | |
return colorCube.outputImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there. Could you find a working solution for doing the replacement properly?