Mac Screen Saver – Time as Color Hex Code

Mac Screen Saver – Time as Color Hex Code

Created By : Debasis Das (17-Feb-2015)

In this article we will create a simple Mac Screen Saver that will display the current time , Day, Date, Month and Year.

The time will be translated as the HexCode equivalent of Color and as the time changes the background color of the screen saver changes.

Download the Screen Saver from here KSColorHex.saver

or

Download the code here KSColorHex

TimeColorHexMacScreenSaver

Step 1: Create a new xcode project and from the template select – System Plugin – Screen Saver
TimeColoxHexMacScreenSaver1

//
//  KSColorHexView.h
//  KSColorHex
//
//  Created by Debasis Das on 2/3/15.
//  Copyright (c) 2015 Knowstack. All rights reserved.
//
#import <ScreenSaver/ScreenSaver.h>
@interface KSColorHexView : ScreenSaverView
@end

//
//  KSColorHexView.m
//  KSColorHex
//
//  Created by Debasis Das on 2/3/15.
//  Copyright (c) 2015 Knowstack. All rights reserved.
//

#import "KSColorHexView.h"

@implementation KSColorHexView

- (instancetype)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    if (self) {
        [self setAnimationTimeInterval:1];
    }
    return self;
}

- (void)startAnimation
{
    [super startAnimation];
}

- (void)stopAnimation
{
    [super stopAnimation];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"EEEE\t\tdd - MMMM - YYYY"];
    NSString *    myMonthString = [dateFormatter stringFromDate:date];
    
    [dateFormatter setDateFormat:@"HHmmss"];
    NSString *timeForColor = [dateFormatter stringFromDate:date];
    
    [dateFormatter setDateFormat:@"HH : mm : ss"];
    NSString *time = [dateFormatter stringFromDate:date];
    
    NSColor *color = [KSColorHexView colorWithHexColorString:timeForColor];
    [color set];
    NSRectFill(rect);
    [time drawAtPoint:NSMakePoint(100, self.frame.size.height/2) withAttributes:@{NSForegroundColorAttributeName:[NSColor whiteColor],NSFontAttributeName:[NSFont fontWithName:@"Helvetica Neue"     size:160.0]}];

    [[myMonthString capitalizedString] drawAtPoint:NSMakePoint(100, self.frame.size.height/3) withAttributes:@{NSForegroundColorAttributeName:[NSColor whiteColor],NSFontAttributeName:[NSFont fontWithName:@"Helvetica Neue"     size:45.0]}];

    [@"Created By: Debasis Das" drawAtPoint:NSMakePoint((self.frame.size.width - 300), (self.frame.size.height - 40.0)) withAttributes:@{NSForegroundColorAttributeName:[NSColor whiteColor],NSFontAttributeName:[NSFont fontWithName:@"Helvetica Neue Light"     size:25.0]}];

    [@"Time as Color Hex Code" drawAtPoint:NSMakePoint((self.frame.size.width - 300), (self.frame.size.height - 80.0)) withAttributes:@{NSForegroundColorAttributeName:[NSColor whiteColor],NSFontAttributeName:[NSFont fontWithName:@"Helvetica Neue Light"     size:20.0]}];

}

+ (NSColor*)colorWithHexColorString:(NSString*)inColorString
{
    NSColor* result = nil;
    unsigned colorCode = 0;
    unsigned char redByte, greenByte, blueByte;
    
    if (nil != inColorString)
    {
        NSScanner* scanner = [NSScanner scannerWithString:inColorString];
        (void) [scanner scanHexInt:&colorCode]; // ignore error
    }
    redByte = (unsigned char)(colorCode >> 16);
    greenByte = (unsigned char)(colorCode >> 8);
    blueByte = (unsigned char)(colorCode); // masks off high bits
    
    result = [NSColor
              colorWithCalibratedRed:(CGFloat)redByte / 0xff
              green:(CGFloat)greenByte / 0xff
              blue:(CGFloat)blueByte / 0xff
              alpha:1.0];
    return result;
}

- (void)animateOneFrame
{
    [self setNeedsDisplay:YES];
}

- (BOOL)hasConfigureSheet
{
    return NO;
}

- (NSWindow*)configureSheet
{
    return nil;
}
@end
Posted in Cocoa, Objective C Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news