绘制一个插入NSShadow和Inset Stroke [英] Draw an Inset NSShadow and Inset Stroke
问题描述
我有一个 NSBezierPath
,并且我想在路径内绘制inet阴影(类似于Photoshop)。
I have an NSBezierPath
and I want to draw in inset shadow (similar to Photoshop) inside the path.
还有没有做到这一点?此外,我知道你可以 -stroke
路径,但你可以在一个路径(类似于Photoshop中的Stroke里面)吗?
Is there anyway to do this? Also, I know you can -stroke
paths, but can you stroke inside a path (similar to Stroke Inside in Photoshop)?
static NSImage * graydient = nil;
if (!graydient) {
graydient = [[NSImage alloc] initWithSize: NSMakeSize(22, 22)];
[graydient lockFocus];
NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations: clr(@"#262729"), 0.0f, clr(@"#37383a"), 0.43f, clr(@"#37383a"), 1.0f, nil];
[gradient drawInRect: NSMakeRect(0, 4.179, 22, 13.578) angle: 90.0f];
[gradient release];
[graydient unlockFocus];
}
NSColor * gcolor = [NSColor colorWithPatternImage: graydient];
[gcolor set];
NSShadow * shadow = [NSShadow new];
[shadow setShadowColor: [NSColor colorWithDeviceWhite: 1.0f alpha: 1.0f]];
[shadow setShadowBlurRadius: 0.0f];
[shadow setShadowOffset: NSMakeSize(0, 1)];
[shadow set];
[path fill];
[NSGraphicsContext saveGraphicsState];
[[path pathFromIntersectionWithPath: [NSBezierPath bezierPathWithRect: NSInsetRect([path bounds], 0.6, 0)]] setClip];
[gcolor set];
[shadow setShadowOffset: NSMakeSize(0, 1)];
[shadow setShadowColor: [NSColor blackColor]];
[shadow set];
[outer stroke];
[NSGraphicsContext restoreGraphicsState];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeSourceOut];
[shadow set];
[[NSColor whiteColor] set];
[inner fill];
[shadow set];
[inner fill];
[NSGraphicsContext restoreGraphicsState];
这是我的最终结果。它看起来不错。我不得不改变阴影到White @ 1.0 Alpha,使它工作。即使菜单栏项目的阴影Alpha标准为0.5,也不会看起来很差。
This is my final result. It looks pretty good. I had to change the shadow to White @ 1.0 Alpha to make it work. Even though the shadow alpha norm for menu bar items is 0.5, it doesn't look half bad.
非常感谢您出席约书亚Nozzi 。
推荐答案
我想你可以做通过在bezier路径上设置剪辑以使用它作为掩码并抚摸阴影,然后根据需要添加正常笔画。
I think you can do this by setting clip on the bezier path to use it as a mask and stroking the shadow, then adding a normal stroke if desired.
基于更新的代码更新:
在这里。我今晚感到拖延。 : - )
Here you go. I'm feeling procrastinate-y tonight. :-)
// Describe an inset rect (adjust for pixel border)
NSRect whiteRect = NSInsetRect([self bounds], 20, 20);
whiteRect.origin.x += 0.5;
whiteRect.origin.y += 0.5;
// Create and fill the shown path
NSBezierPath * path = [NSBezierPath bezierPathWithRect:whiteRect];
[[NSColor whiteColor] set];
[path fill];
// Save the graphics state for shadow
[NSGraphicsContext saveGraphicsState];
// Set the shown path as the clip
[path setClip];
// Create and stroke the shadow
NSShadow * shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor redColor]];
[shadow setShadowBlurRadius:10.0];
[shadow set];
[path stroke];
// Restore the graphics state
[NSGraphicsContext restoreGraphicsState];
// Add a nice stroke for a border
[path setLineWidth:1.0];
[[NSColor grayColor] set];
[path stroke];
这篇关于绘制一个插入NSShadow和Inset Stroke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!