着色灰度NSImage(或CIImage)

时间:2009-09-11 20:36:42

标签: objective-c cocoa core-image

我有一个灰度图像,我想用它来绘制Cocoa控件。图像具有不同级别的灰度。在最黑暗的地方,我希望它能画出最暗的指定色调。我希望它在源图像为白色时是透明的。

基本上,我想重现iPhone上UINavigationBar中看到的tintColor的行为。

到目前为止,我已经探索了几个选项:

  • 使用SourceOver合成在灰度图像上绘制色调颜色 - >这需要非不透明的色调 - >结果比预期的要暗得多

  • 使用CIMultiplyCompositing CIFilter为图像着色 - >我不能[CIImage drawAtPoint:fromRect:operation:fraction:]来绘制部分图像。同样适用于NSImage - >

  • 偶尔会发生一些我无法理解的崩溃事件
  • 将灰度图像转换为蒙版。即黑色应该是不透明的。白色应该是透明的。灰色应具有中间alpha值。 - >这似乎是最好的解决方案 - >尽我所能,我无法实现这一目标。

8 个答案:

答案 0 :(得分:40)

上述解决方案对我没有用。但这个更容易的解决方案对我来说非常有用

- (NSImage *)imageTintedWithColor:(NSColor *)tint
{
    NSImage *image = [self copy];
    if (tint) {
        [image lockFocus];
        [tint set];
        NSRect imageRect = {NSZeroPoint, [image size]};
        NSRectFillUsingOperation(imageRect, NSCompositeSourceAtop);
        [image unlockFocus];
    }
    return image;
}

答案 1 :(得分:13)

迅速实施bluebamboo的回答:

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
}

答案 2 :(得分:8)

- (NSImage *)imageTintedWithColor:(NSColor *)tint 
{
    if (tint != nil) {
        NSSize size = [self size];
        NSRect bounds = { NSZeroPoint, size };
        NSImage *tintedImage = [[NSImage alloc] initWithSize:size];

        [tintedImage lockFocus];

        CIFilter *colorGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
        CIColor *color = [[[CIColor alloc] initWithColor:tint] autorelease];

        [colorGenerator setValue:color forKey:@"inputColor"];

        CIFilter *monochromeFilter = [CIFilter filterWithName:@"CIColorMonochrome"];
        CIImage *baseImage = [CIImage imageWithData:[self TIFFRepresentation]];

        [monochromeFilter setValue:baseImage forKey:@"inputImage"];     
        [monochromeFilter setValue:[CIColor colorWithRed:0.75 green:0.75 blue:0.75] forKey:@"inputColor"];
        [monochromeFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputIntensity"];

        CIFilter *compositingFilter = [CIFilter filterWithName:@"CIMultiplyCompositing"];

        [compositingFilter setValue:[colorGenerator valueForKey:@"outputImage"] forKey:@"inputImage"];
        [compositingFilter setValue:[monochromeFilter valueForKey:@"outputImage"] forKey:@"inputBackgroundImage"];

        CIImage *outputImage = [compositingFilter valueForKey:@"outputImage"];

        [outputImage drawAtPoint:NSZeroPoint
                        fromRect:bounds
                       operation:NSCompositeCopy
                        fraction:1.0];

        [tintedImage unlockFocus];  

        return [tintedImage autorelease];
    }
    else {
        return [[self copy] autorelease];
    }
}

- (NSImage*)imageCroppedToRect:(NSRect)rect
{
    NSPoint point = { -rect.origin.x, -rect.origin.y };
    NSImage *croppedImage = [[NSImage alloc] initWithSize:rect.size];

    [croppedImage lockFocus];
    {
        [self compositeToPoint:point operation:NSCompositeCopy];
    }
    [croppedImage unlockFocus];

    return [croppedImage autorelease];
}

答案 3 :(得分:4)

扩展形式的Swift版本:

extension NSImage {
    func tintedImageWithColor(color:NSColor) -> NSImage {
        let size        = self.size
        let imageBounds = NSMakeRect(0, 0, size.width, size.height)
        let copiedImage = self.copy() as! NSImage

        copiedImage.lockFocus()
        color.set()
        NSRectFillUsingOperation(imageBounds, .CompositeSourceAtop)
        copiedImage.unlockFocus()

        return copiedImage
    }
}

答案 4 :(得分:3)

CIMultiplyCompositing过滤器绝对是这样做的方法。如果它崩溃了,你可以发布堆栈跟踪吗?我大量使用CIFilters并且没有崩溃问题。

//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];

答案 5 :(得分:3)

我想用一种带有alpha色调的图像着色而不会看到图像的原始颜色显示出来。以下是您可以这样做的方法:

extension NSImage {
    func tinting(with tintColor: NSColor) -> NSImage {
        guard let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil) else { return self }

        return NSImage(size: size, flipped: false) { bounds in
            guard let context = NSGraphicsContext.current()?.cgContext else { return false }

            tintColor.set()
            context.clip(to: bounds, mask: cgImage)
            context.fill(bounds)

            return true
        }
    }
}

答案 6 :(得分:1)

Swift 5 版本,该版本还可以处理色调颜色的alpha分量。

我使用它通过将模板图标转换为不同的颜色和透明度级别来支持具有多个图标状态的暗模式。例如,您可以传递NSColor(white: 0, alpha: 0.5)以获得浅色模式图标的暗淡版本,并传递NSColor(white: 1, alpha: 0.5)以获得黑暗模式下的图标暗淡版本。

func tintedImage(_ image: NSImage, color: NSColor) -> NSImage {
    let newImage = NSImage(size: image.size)
    newImage.lockFocus()

    // Draw with specified transparency
    let imageRect = NSRect(origin: .zero, size: image.size)
    image.draw(in: imageRect, from: imageRect, operation: .sourceOver, fraction: color.alphaComponent)

    // Tint with color
    color.withAlphaComponent(1).set()
    imageRect.fill(using: .sourceAtop)

    newImage.unlockFocus()
    return newImage
}

答案 7 :(得分:0)

对于更精细的粒度,您可以使用多项式颜色方法,使用我在下面建议的方法:How can I display the spinning NSProgressIndicator in a different color?