forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTermLogoGenerator.m
83 lines (65 loc) · 2.52 KB
/
iTermLogoGenerator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#import "iTermLogoGenerator.h"
#import "NSColor+iTerm.h"
#import "NSImage+iTerm.h"
#import <QuartzCore/QuartzCore.h>
// key->NSImage
static NSMutableDictionary *gLogoCache;
@implementation iTermLogoGenerator
- (void)dealloc {
[_textColor release];
[_backgroundColor release];
[_tabColor release];
[_cursorColor release];
[super dealloc];
}
- (NSString *)keyForColor:(NSColor *)color {
// Quantizing at 4 bits per component should be enough to produce good logos, while hopefully
// getting us wins here and there.
int r = 16 * [color redComponent];
int g = 16 * [color greenComponent];
int b = 16 * [color blueComponent];
return [NSString stringWithFormat:@"%d,%d,%d", r, g, b];
}
- (NSString *)cacheKey {
return [NSString stringWithFormat:@"%@ %@ %@ %@",
[self keyForColor:self.textColor],
[self keyForColor:self.cursorColor],
[self keyForColor:self.backgroundColor],
[self keyForColor:self.tabColor]];
}
- (NSImage *)generatedImage {
NSString *key = [self cacheKey];
NSImage *cachedImage = gLogoCache[key];
if (cachedImage) {
return cachedImage;
}
const CGFloat width = 48;
const CGFloat height = 48;
NSImage *image = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] autorelease];
[image lockFocus];
NSImage *frame = [NSImage it_imageNamed:@"LogoFrame.png" forClass:self.class];
NSImage *shadow = [NSImage it_imageNamed:@"LogoShadow.png" forClass:self.class];
[_backgroundColor set];
NSBezierPath *path = [NSBezierPath bezierPath];
[path appendBezierPathWithRoundedRect:NSMakeRect(3, 6, 42, 36) xRadius:2 yRadius:2];
[path fill];
[frame drawInRect:NSMakeRect(0, 0, width, height)];
if (self.tabColor) {
[[self.tabColor colorWithAlphaComponent:0.5] set];
CGFloat tabHeight = 9;
NSRectFillUsingOperation(NSMakeRect(0, height - tabHeight, width, tabHeight), NSCompositingOperationSourceIn);
}
[shadow drawInRect:NSMakeRect(0, 0, width, height)];
NSString *prompt = @"$";
[prompt drawAtPoint:NSMakePoint(7, 25) withAttributes:@{ NSFontAttributeName: [NSFont fontWithName:@"Helvetica Neue" size:12],
NSForegroundColorAttributeName: self.textColor }];
[self.cursorColor set];
NSRectFill(NSMakeRect(15.5, 27, 5.5, 11));
[image unlockFocus];
if (!gLogoCache) {
gLogoCache = [[NSMutableDictionary alloc] init];
}
gLogoCache[key] = image;
return image;
}
@end