Search code examples
objective-cxcodemacosscreensaver

Make BG of macOS screensaver black?


I'm new to Obj-C so apologies if my code is messy.

I've made a screensaver of a DVD logo that changes colour on each bounce. My only problem is the background--it's dark grey instead of black. I'm hoping somebody could help me out.

Here is some of my code:

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

Here's a download of the saver, if you wanna see what I'm talking about.


Solution

  • If I follow your code fragment and problem correctly your drawRect starts with:

    const float g = 15.0f/255.0f;
    [[NSColor colorWithRed:g green:g blue:g alpha:1] setFill];
    NSRectFill(rectParam);
    

    This fills the area defined by rectParam with dark gray. Why did you choose 15.0f/255.0f for the RGB components? Black is just 0 but more easily obtained using blackColor:

    [[NSColor blackColor] setFill];
    

    HTH