ios - Changing specific pixels in a UIImage based on RBG values to a different RBG color in swift -


i making application changes colors on map highlight country. code system 1 country have specific color , if condition allows it, color changed yellow , rest green. similar tagging system highlight specific countries based on users input. have seen post on before , modified use parameters detection color. https://codedump.io/share/gesdbwfbgt3t/1/change-color-of-certain-pixels-in-a-uiimage

here modified code:

func processpixelsinimage(inputimage: uiimage, r: uint8, b: uint8, g: uint8) -> uiimage {         let inputcgimage     = inputimage.cgimage         let colorspace       = cgcolorspacecreatedevicergb()         let width            = cgimagegetwidth(inputcgimage)         let height           = cgimagegetheight(inputcgimage)         let bytesperpixel    = 4         let bitspercomponent = 8         let bytesperrow      = bytesperpixel * width         let bitmapinfo       = cgimagealphainfo.premultipliedfirst.rawvalue | cgbitmapinfo.byteorder32little.rawvalue          let context = cgbitmapcontextcreate(nil, width, height, bitspercomponent, bytesperrow, colorspace, bitmapinfo)!         cgcontextdrawimage(context, cgrectmake(0, 0, cgfloat(width), cgfloat(height)), inputcgimage)          let pixelbuffer = unsafemutablepointer<uint32>(cgbitmapcontextgetdata(context))          var currentpixel = pixelbuffer          _ in 0 ..< int(height) {             _ in 0 ..< int(width) {                 let pixel = currentpixel.memory                  if red(pixel) == r && green(pixel) == g && blue(pixel) == b {                     currentpixel.memory = rgba(red: 0, green: 170, blue: 0, alpha: 255)                 }                 currentpixel = currentpixel.successor()             }         }          let outputcgimage = cgbitmapcontextcreateimage(context)         let outputimage = uiimage(cgimage: outputcgimage!, scale: inputimage.scale, orientation: inputimage.imageorientation)          return outputimage     }      func alpha(color: uint32) -> uint8 {         return uint8((color >> 24) & 255)     }      func red(color: uint32) -> uint8 {         return uint8((color >> 16) & 255)     }      func green(color: uint32) -> uint8 {         return uint8((color >> 8) & 255)     }      func blue(color: uint32) -> uint8 {         return uint8((color >> 0) & 255)     }      func rgba(red red: uint8, green: uint8, blue: uint8, alpha: uint8) -> uint32 {         return (uint32(alpha) << 24) | (uint32(red) << 16) | (uint32(green) << 8) | (uint32(blue) << 0)     } 

when use rgb values other color black(0,0,0) doesnt detect , change color. im not sure if different scaling compared normal cg colors rgb values because of different data types i'm still unsure of how use function. how can detect colors other black using rgb values , change them another?

i tweaked similar code change uiimage white pixels clear pixels

(swift 3)

func processpixelsinimage(_ image: uiimage) -> uiimage? { guard let inputcgimage = image.cgimage else {     //print("unable cgimage")     return nil } let colorspace       = cgcolorspacecreatedevicergb() let width            = inputcgimage.width let height           = inputcgimage.height let bytesperpixel    = 4 let bitspercomponent = 8 let bytesperrow      = bytesperpixel * width let bitmapinfo       = rgba32.bitmapinfo  guard let context = cgcontext(data: nil, width: width, height: height, bitspercomponent: bitspercomponent, bytesperrow: bytesperrow, space: colorspace, bitmapinfo: bitmapinfo) else {     //print("unable create context")     return nil } context.draw(inputcgimage, in: cgrect(x: 0, y: 0, width: width, height: height))  guard let buffer = context.data else {     //print("unable context data")     return nil }  let pixelbuffer = buffer.bindmemory(to: rgba32.self, capacity: width * height)  let white = rgba32(red: 255, green: 255, blue: 255, alpha: 255) let clear = rgba32(red: 0, green: 0, blue: 0, alpha: 0)  row in 0 ..< int(height) {     column in 0 ..< int(width) {         let offset = row * width + column         if pixelbuffer[offset] == white {             pixelbuffer[offset] = clear         }     } }  let outputcgimage = context.makeimage()! let outputimage = uiimage(cgimage: outputcgimage, scale: image.scale, orientation: image.imageorientation)  return outputimage  }   struct rgba32: equatable { var color: uint32  var red: uint8 {     return uint8((color >> 24) & 255) }  var green: uint8 {     return uint8((color >> 16) & 255) }  var blue: uint8 {     return uint8((color >> 8) & 255) }  var alpha: uint8 {     return uint8((color >> 0) & 255) }  init(red: uint8, green: uint8, blue: uint8, alpha: uint8) {     color = (uint32(red) << 24) | (uint32(green) << 16) | (uint32(blue) << 8) | (uint32(alpha) << 0) }  static let bitmapinfo = cgimagealphainfo.premultipliedlast.rawvalue | cgbitmapinfo.byteorder32little.rawvalue  static func ==(lhs: rgba32, rhs: rgba32) -> bool {     return lhs.color == rhs.color }  } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -