Tinting a grayscale NSImage (or CIImage)

后端 未结 8 1931
轮回少年
轮回少年 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:46

    The CIMultiplyCompositing filter is definitely the way to do this. If it's crashing, can you post a stack trace? I use CIFilters heavily and don't have crashing issues.

    //assume inputImage is the greyscale CIImage you want to tint
    
    CIImage* outputImage = nil;
    
    //create some green
    CIFilter* greenGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
    CIColor* green = [CIColor colorWithRed:0.30 green:0.596 blue:0.172];
    [greenGenerator setValue:green forKey:@"inputColor"];
    CIImage* greenImage = [greenGenerator valueForKey:@"outputImage"];
    
    //apply a multiply filter
    CIFilter* filter = [CIFilter filterWithName:@"CIMultiplyCompositing"];
    [filter setValue:greenImage forKey:@"inputImage"];
    [filter setValue:inputImage forKey:@"inputBackgroundImage"];
    outputImage = [filter valueForKey:@"outputImage"];
    
    [outputImage drawAtPoint:NSZeroPoint fromRect:NSRectFromCGRect([outputImage extent]) operation:NSCompositeCopy fraction:1.0];
    

提交回复
热议问题