グレースケールNSImage(またはCIImage)の色付け
-
06-07-2019 - |
質問
Cocoaコントロールの描画に使用したいグレースケール画像があります。画像にはさまざまなレベルのグレーがあります。最も暗い場所では、指定した色合いを最も暗くしたいです。ソース画像が白の場所で透明にしたい。
基本的に、iPhoneのUINavigationBarで見られるtintColorの動作を再現します。
これまで、いくつかのオプションを検討しました。
-
SourceOverコンポジションを使用してグレースケール画像上に濃淡色を描画します ->これには、不透明ではない色合いが必要です ->結果は期待よりもはるかに暗くなります
-
CIMultiplyCompositing CIFilterを使用して画像に色を付ける -> [CIImage drawAtPoint:fromRect:operation:fraction:]で画像の一部のみを描画することはできません。 NSImageでも同じことがうまくいきます ->時々理解できないクラッシュが発生する
-
グレースケール画像をマスクに変換します。つまり黒は不透明でなければなりません。白は透明でなければなりません。グレーには中間のアルファ値が必要です。 ->これが最善の解決策のようです ->できる限り試してみてください、これを達成することはできません。
解決 3
- (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];
}
他のヒント
上記の解決策はうまくいきませんでした。しかし、このはるかに簡単なソリューションは私には最適です
- (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;
}
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
}
拡張機能の形式の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
}
}
CIMultiplyCompositingフィルターは間違いなくこれを行う方法です。クラッシュしている場合、スタックトレースを投稿できますか? CIFilterを頻繁に使用しますが、クラッシュの問題はありません。
//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];
画像の元の色が透けて見えることなく、アルファのある色合いで画像を色付けしたかった。その方法は次のとおりです。
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
}
}
}
よりきめ細かくするには、以下で提案したアプローチを使用して、多項式カラーアプローチを使用できます。回転しているNSProgressIndicatorを別の色で表示するにはどうすればよいですか?