Tinting a grayscale NSImage (or CIImage)

后端 未结 8 2002
轮回少年
轮回少年 2021-01-30 09:02

I have a grayscale image which I want to use for drawing Cocoa controls. The image has various levels of gray. Where it is darkest, I want it to draw a specified tint color dark

8条回答
  •  耶瑟儿~
    2021-01-30 09:41

    A Swift implementation of bluebamboo's answer:

    func tintedImage(_ image: NSImage, tint: NSColor) -> NSImage {
        guard let tinted = image.copy() as? NSImage else { return image }
        tinted.lockFocus()
        tint.set()
    
        let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
        NSRectFillUsingOperation(imageRect, .sourceAtop)
    
        tinted.unlockFocus()
        return tinted
    }
    

提交回复
热议问题