-
Notifications
You must be signed in to change notification settings - Fork 5
/
FTColorPanel.m
200 lines (167 loc) · 8.28 KB
/
FTColorPanel.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#import "FTColorPanel.h"
#define CONTENT_BOX_INDEX 4
#define CONTENT_BOX_OFFSET 31
#define SPACING 7
@implementation FTColorPanel
-(FTColorPanel *)init {
if ([super init]) {
_colorMode = HEX_COLOR_MODE;
[self setStyleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask];
[self setHidesOnDeactivate: NO];
[self setFloatingPanel: NO];
[self setShowsAlpha: YES];
// Add a constraint to the 'content box', which holds the selected mode's
// content view, to make place for the new divider and text field.
id contentBox = [[[self contentView] subviews] objectAtIndex: CONTENT_BOX_INDEX];
NSLayoutConstraint *contentBoxConstraint = [NSLayoutConstraint constraintWithItem:[[[self contentView] subviews] firstObject]
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:contentBox
attribute:NSLayoutAttributeBottom
multiplier:1
constant:(CONTENT_BOX_OFFSET + SPACING + 3)];
[[self contentView] addConstraint:contentBoxConstraint];
// Add a new divider under the opacity slider
float totalWidth = [[self contentView] frame].size.width;
NSRect newDividerFrame = NSMakeRect(0, 0, totalWidth, 1);
NSBox *newDivider = [[NSBox alloc] initWithFrame: newDividerFrame];
[newDivider setBoxType: NSBoxSeparator];
[newDivider setAutoresizingMask: NSViewWidthSizable];
[contentBox addSubview: newDivider];
// Add the new text field underneath the new divider and the existing divider above
float fontSize = [NSFont smallSystemFontSize];
colorCodeField = [[NSTextField alloc] init];
// set the text properties
[[colorCodeField cell] setFont: [NSFont systemFontOfSize: fontSize]];
// make it look like a label as in IB
[colorCodeField setBezeled: NO];
[colorCodeField setDrawsBackground: NO];
[colorCodeField setEditable: NO];
[colorCodeField setSelectable: YES];
colorCodeField.translatesAutoresizingMaskIntoConstraints = NO;
colorSpaceButton = [[NSButton alloc] init];
[colorSpaceButton setFont:[NSFont systemFontOfSize: fontSize]];
[colorSpaceButton setEnabled:YES];
[colorSpaceButton setBezelStyle:NSRoundRectBezelStyle];
colorSpaceButton.translatesAutoresizingMaskIntoConstraints = NO;
[colorSpaceButton setAction:@selector(colorSpaceButtonPressed)];
NSView *colorCodeView = [[NSView alloc] init];
colorCodeView.translatesAutoresizingMaskIntoConstraints = NO;
[colorCodeView addSubview: colorCodeField];
[colorCodeView addSubview: colorSpaceButton];
[[self contentView] addSubview: colorCodeView];
[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[field]->=padding-[button]-padding-|"
options:NSLayoutFormatAlignAllBaseline
metrics:@{@"padding": @10}
views:@{
@"field": colorCodeField,
@"button": colorSpaceButton
}
]
];
[self.contentView addConstraints: @[
[NSLayoutConstraint constraintWithItem:colorCodeView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:colorCodeView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:fontSize + 8],
[NSLayoutConstraint constraintWithItem:colorCodeView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:contentBox
attribute:NSLayoutAttributeBottom
multiplier:1
constant:SPACING - 2]
]];
[self updateStringRepresentationOfColor];
return self;
}
return nil;
}
-(int)colorMode {
return _colorMode;
}
-(void)setColorMode:(int)colorMode {
_colorMode = colorMode;
[self updateStringRepresentationOfColor];
}
-(void)setColor:(NSColor *)color {
[super setColor: color];
[self updateStringRepresentationOfColor];
}
-(NSString *)representationStringOfCurrentColorMode:(BOOL)shortVersion {
return [self representationStringOfColorInMode:_colorMode shortVersion:shortVersion];
}
-(NSString *)representationStringOfColorInMode:(int)colorMode shortVersion:(BOOL)shortVersion {
NSColor *color = [self color];
switch (colorMode) {
case HEX_COLOR_MODE: return [color toHexString];
case HEX_WITHOUT_HASH_COLOR_MODE: return [color toHexStringWithoutHash];
case RGB_COLOR_MODE: return [color toRGBString: shortVersion];
case RGBA_COLOR_MODE: return [color toRGBAString: shortVersion];
case HSL_COLOR_MODE: return [color toHSLString: shortVersion];
case HSLA_COLOR_MODE: return [color toHSLAString: shortVersion];
case OBJC_NSCOLOR_COLOR_MODE: return [color toObjcNSColor: shortVersion];
case SWIFT_NSCOLOR_COLOR_MODE: return [color toSwiftNSColor: shortVersion];
case MOTION_NSCOLOR_COLOR_MODE: return [color toMotionNSColor: shortVersion];
case OBJC_UICOLOR_COLOR_MODE: return [color toObjcUIColor: shortVersion];
case SWIFT_UICOLOR_COLOR_MODE: return [color toSwiftUIColor: shortVersion];
case MOTION_UICOLOR_COLOR_MODE: return [color toMotionUIColor: shortVersion];
}
return nil;
}
-(BOOL) hasCorrectColorSpace {
return [[[self color] colorSpace] isEqualTo: [self desiredColorSpace]];
}
-(NSString *) titleForColorSpace:(NSColorSpace *)colorSpace {
if([colorSpace isEqual:[NSColorSpace sRGBColorSpace]]){
return @"sRGB";
} else {
return @"Gen.";
}
}
-(NSString *)toolTipForColorSpace:(NSColorSpace *)colorSpace {
return [NSString stringWithFormat:@"Convert to %@", [colorSpace localizedName]];
}
-(NSColorSpace *) desiredColorSpace {
switch (_colorMode) {
case HEX_COLOR_MODE:
case HEX_WITHOUT_HASH_COLOR_MODE:
case RGB_COLOR_MODE:
case RGBA_COLOR_MODE:
case HSL_COLOR_MODE:
case HSLA_COLOR_MODE:
case OBJC_UICOLOR_COLOR_MODE:
case SWIFT_UICOLOR_COLOR_MODE:
case MOTION_UICOLOR_COLOR_MODE:
return [NSColorSpace sRGBColorSpace];
case OBJC_NSCOLOR_COLOR_MODE:
case SWIFT_NSCOLOR_COLOR_MODE:
case MOTION_NSCOLOR_COLOR_MODE:
return [NSColorSpace genericRGBColorSpace];
}
return nil;
}
-(void)updateStringRepresentationOfColor {
[colorSpaceButton setTitle: [self titleForColorSpace:[self desiredColorSpace]]];
[colorSpaceButton setToolTip:[self toolTipForColorSpace:[self desiredColorSpace]]];
if([self hasCorrectColorSpace]) {
[colorSpaceButton setEnabled:NO];
} else {
[colorSpaceButton setEnabled:YES];
}
[colorCodeField setStringValue: [self representationStringOfCurrentColorMode: YES]];
}
-(void)colorSpaceButtonPressed {
[self setColor: [[self color] colorUsingColorSpace:[self desiredColorSpace]]];
}
@end