将 macOS 屏保 BG 设置为黑色?
Make BG of macOS screensaver black?
我是 Obj-C 的新手,如果我的代码有问题,请见谅。
我制作了一个 DVD 徽标的屏幕保护程序,每次弹跳都会改变颜色。我唯一的问题是背景——它是深灰色而不是黑色。我希望有人能帮助我。
这是我的一些代码:
NSString * dvdPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"dvdlogo" ofType:@"png"];
self.dvdLogo = [[NSImage alloc] initWithContentsOfFile:dvdPath];[self hitWall];}return self;}
- (void)startAnimation
{[super startAnimation];}
- (void)stopAnimation
{[super stopAnimation];}
- (void)drawRect:(NSRect)rectParam
{const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
NSRect rect;
rect.size = NSMakeSize(self.dvdWidth, self.dvdHeight);
self.x += self.xSpeed;
self.y += self.ySpeed;
rect.origin = CGPointMake(self.x, self.y);
self.dirtyRect = rect;
[self.dvdLogo drawInRect:rect];
CGPoint centre = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
if (centre.x + self.dvdWidth / 2 >= WIDTH || centre.x - self.dvdWidth / 2 <= 0) {
self.xSpeed *= -1;
[self hitWall];}
if (centre.y + self.dvdHeight / 2 >= HEIGHT || centre.y - self.dvdHeight / 2 <= 0) {
self.ySpeed *= -1;
[self hitWall];}}
- (void)hitWall
{NSArray * colours = @[[NSColor redColor],
[NSColor blueColor],
[NSColor yellowColor],
[NSColor cyanColor],
[NSColor orangeColor],
[NSColor magentaColor],
[NSColor greenColor]];
self.dvdColor = colours[arc4random() % [colours count]];
[self.dvdLogo lockFocus];
[self.dvdColor set];
NSRect imageRect = {NSZeroPoint, [self.dvdLogo size]};
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[self.dvdLogo unlockFocus];}
- (void)animateOneFrame
{//[self setNeedsDisplay:true];
[self setNeedsDisplayInRect:self.dirtyRect];
return;}
这里是 download of the saver,如果你想看看我在说什么。
如果我遵循您的代码片段并正确解决问题,您的 drawRect
开头为:
const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
这会用深灰色填充 rectParam
定义的区域。为什么选择 15.0f/255.0f
作为 RGB 分量?黑色只是 0
但使用 blackColor
更容易获得:
[[NSColor blackColor] setFill];
HTH
我是 Obj-C 的新手,如果我的代码有问题,请见谅。
我制作了一个 DVD 徽标的屏幕保护程序,每次弹跳都会改变颜色。我唯一的问题是背景——它是深灰色而不是黑色。我希望有人能帮助我。
这是我的一些代码:
NSString * dvdPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"dvdlogo" ofType:@"png"];
self.dvdLogo = [[NSImage alloc] initWithContentsOfFile:dvdPath];[self hitWall];}return self;}
- (void)startAnimation
{[super startAnimation];}
- (void)stopAnimation
{[super stopAnimation];}
- (void)drawRect:(NSRect)rectParam
{const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
NSRect rect;
rect.size = NSMakeSize(self.dvdWidth, self.dvdHeight);
self.x += self.xSpeed;
self.y += self.ySpeed;
rect.origin = CGPointMake(self.x, self.y);
self.dirtyRect = rect;
[self.dvdLogo drawInRect:rect];
CGPoint centre = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
if (centre.x + self.dvdWidth / 2 >= WIDTH || centre.x - self.dvdWidth / 2 <= 0) {
self.xSpeed *= -1;
[self hitWall];}
if (centre.y + self.dvdHeight / 2 >= HEIGHT || centre.y - self.dvdHeight / 2 <= 0) {
self.ySpeed *= -1;
[self hitWall];}}
- (void)hitWall
{NSArray * colours = @[[NSColor redColor],
[NSColor blueColor],
[NSColor yellowColor],
[NSColor cyanColor],
[NSColor orangeColor],
[NSColor magentaColor],
[NSColor greenColor]];
self.dvdColor = colours[arc4random() % [colours count]];
[self.dvdLogo lockFocus];
[self.dvdColor set];
NSRect imageRect = {NSZeroPoint, [self.dvdLogo size]};
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[self.dvdLogo unlockFocus];}
- (void)animateOneFrame
{//[self setNeedsDisplay:true];
[self setNeedsDisplayInRect:self.dirtyRect];
return;}
这里是 download of the saver,如果你想看看我在说什么。
如果我遵循您的代码片段并正确解决问题,您的 drawRect
开头为:
const float g = 15.0f/255.0f;
[[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
NSRectFill(rectParam);
这会用深灰色填充 rectParam
定义的区域。为什么选择 15.0f/255.0f
作为 RGB 分量?黑色只是 0
但使用 blackColor
更容易获得:
[[NSColor blackColor] setFill];
HTH