/
CDColor.m
127 lines (107 loc) · 2.9 KB
/
CDColor.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// CDColor.m
// cocoadialog
//
// Copyright (c) 2004-2017 Mark A. Stratman <mark@sporkstorms.org>, Mark Carver <mark.carver@me.com>.
// All rights reserved.
// Licensed under GPL-2.
#import "CDColor.h"
@implementation CDColor
#pragma mark - Public static methods
+ (instancetype) color {
return [[self alloc] init];
}
+ (instancetype) fg:(CDColorFg)fg {
return [self fg:fg bg:CDColorBgNone styles:@[]];
}
+ (instancetype) fg:(CDColorFg)fg bg:(CDColorBg)bg {
return [self fg:fg bg:bg styles:@[]];
}
+ (instancetype) fg:(CDColorFg)fg bg:(CDColorBg)bg style:(CDColorStyle)style {
return [self fg:fg bg:bg styles:@[[CDColor style:style]]];
}
+ (instancetype) fg:(CDColorFg)fg bg:(CDColorBg)bg styles:(NSArray<NSNumber *> *)styles {
CDColor *instance = [[self alloc] init];
if (instance) {
instance.fg = fg;
instance.bg = bg;
[instance addStyles:styles];
}
return instance;
}
+ (NSNumber *) style:(CDColorStyle)style {
return [NSNumber numberWithInt:style];
}
#pragma mark - Properties
- (BOOL) isApplied {
return _fg != CDColorFgNone || _bg != CDColorBgNone || _styles.count;
}
#pragma mark - Public instance methods
- (instancetype) init {
self = [super init];
if (self) {
[self reset];
}
return self;
}
- (void) addStyle:(CDColorStyle)style {
if (style != CDColorStyleNone && ![self hasStyle:style]) {
[_styles addObject:[NSNumber numberWithInt:style]];
}
}
- (void) addStyles:(NSArray<NSNumber *> *)styles {
for (unsigned int i = 0; i < styles.count; i++) {
CDColorStyle style = [styles[i] intValue];
[self addStyle: style];
}
}
- (BOOL) hasStyle:(CDColorStyle)style {
for (unsigned int i = 0; i < _styles.count; i++) {
if (style == [_styles[i] intValue]) {
return YES;
}
}
return NO;
}
- (BOOL) hasStyles:(NSArray<NSNumber *> *)styles {
BOOL hasStyles = YES;
for (unsigned int i = 0; i < styles.count; i++) {
CDColorStyle style = [styles[i] intValue];
if (![self hasStyle: style]) {
hasStyles = NO;
break;
}
}
return hasStyles;
}
- (void)merge:(CDColor *)color {
_fg = color.fg;
_bg = color.bg;
[self addStyles:color.styles];
}
- (void) removeStyle:(CDColorStyle)style {
int index = -1;
for (unsigned int i = 0; i < _styles.count; i++) {
if (style == [_styles[i] intValue]) {
index = i;
break;
}
}
if (index != -1) {
[_styles removeObjectAtIndex:index];
}
}
- (void) removeStyles:(NSArray<NSNumber *> *)styles {
for (unsigned int i = 0; i < styles.count; i++) {
CDColorStyle style = [styles[i] intValue];
[self removeStyle: style];
}
}
- (void)removeAllStyles {
_styles = [NSMutableArray array];
}
- (void) reset {
_fg = CDColorFgNone;
_bg = CDColorBgNone;
_styles = [NSMutableArray array];
}
@end