gpt4 book ai didi

cocoa - 绘制 Inset NSShadow 和 Inset Stroke

转载 作者:行者123 更新时间:2023-12-03 16:03:40 30 4
gpt4 key购买 nike

我有一个 NSBezierPath,我想在路径内绘制插图阴影(类似于 Photoshop)。

有办法做到这一点吗?另外,我知道您可以-描边路径,但是您可以在路径内部描边(类似于Photoshop中的描边内部)吗?

更新3

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

Final Result这是我的最终结果。看起来不错。我必须将阴影更改为 White @ 1.0 Alpha 才能使其正常工作。尽管菜单栏项目的阴影 alpha 范数是 0.5,但它看起来还不错。

非常感谢Joshua Nozzi .

最佳答案

我认为您可以通过在贝塞尔曲线路径上设置剪辑以将其用作蒙版并抚摸阴影来实现此目的,然后根据需要添加正常描边。

根据更新的代码进行更新:

给你。今晚我感觉自己有点拖延。 :-)

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

关于cocoa - 绘制 Inset NSShadow 和 Inset Stroke,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4205662/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com