31

我有一个灰度图像,我想用它来绘制 Cocoa 控件。图像具有各种灰度等级。在最暗的地方,我希望它绘制最暗的指定色调。我希望它在源图像为白色的地方是透明的。

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

到目前为止,我已经探索了几种选择:

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

  • 使用 CIMultiplyCompositing CIFilter 为图像着色 - > 我不能 [CIImage drawAtPoint:fromRect:operation:fraction:] 仅绘制图像的一部分。同样适用于 NSImage -> 我偶尔会遇到我无法理解的崩溃

  • 将灰度图像转换为蒙版。即黑色应该是不透明的。白色应该是透明的。灰色应该有中间的 alpha 值。-> 这似乎是最好的解决方案 -> 尽我所能,我无法做到这一点。

4

8 回答 8

41

上述解决方案对我不起作用。但是这个更简单的解决方案对我很有用

- (NSImage *)imageTintedWithColor:(NSColor *)tint
{
    NSImage *image = [self copy];
    if (tint) {
        [image lockFocus];
        [tint set];
        NSRect imageRect = {NSZeroPoint, [image size]};
        NSRectFillUsingOperation(imageRect, NSCompositeSourceIn);
        [image unlockFocus];
    }
    return image;
}
于 2013-04-22T01:18:15.143 回答
14

bluebamboo 答案的 Swift 实现:

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
}
于 2014-09-20T20:14:12.400 回答
9
- (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];
}
于 2009-09-12T14:06:36.660 回答
7

我想用具有 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
        }
    }
}
于 2017-06-29T19:36:13.863 回答
4

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];
于 2009-09-12T02:23:41.393 回答
4

Extension 形式的 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, .CompositeSourceIn)
        copiedImage.unlockFocus()

        return copiedImage
    }
}
于 2016-03-02T14:56:53.983 回答
2

Swift 5版本,它也处理 tint 颜色的 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
}
于 2020-04-14T16:54:41.493 回答
0

对于更细粒度,您可以使用多项式颜色方法,使用我在以下建议的方法:如何以不同的颜色显示旋转的 NSProgressIndicator?

于 2015-09-04T11:09:11.953 回答