This repository was archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlibcolorpicker.mm
More file actions
executable file
·191 lines (155 loc) · 6.21 KB
/
libcolorpicker.mm
File metadata and controls
executable file
·191 lines (155 loc) · 6.21 KB
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
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>
#import <iostream>
#import <string>
#import <vector>
int convertFromHex(std::string hex) {
int value = 0;
int a = 0;
int b = ((int)hex.length()) - 1;
for (; b >= 0; a++, b--) {
if (hex[b] >= '0' && hex[b] <= '9')
value += (hex[b] - '0') * (1 << (a * 4));
else {
switch (hex[b]) {
case 'A':
case 'a':
value += 10 * (1 << (a * 4));
break;
case 'B':
case 'b':
value += 11 * (1 << (a * 4));
break;
case 'C':
case 'c':
value += 12 * (1 << (a * 4));
break;
case 'D':
case 'd':
value += 13 * (1 << (a * 4));
break;
case 'E':
case 'e':
value += 14 * (1 << (a * 4));
break;
case 'F':
case 'f':
value += 15 * (1 << (a * 4));
break;
default:
NSLog(@"Error, invalid char '%d' in hex number", hex[a]);
break;
}
}
}
return value;
}
void hextodec(std::string hex, std::vector<unsigned char>& rgb) {
// since there is no prefix attached to hex, use this code
int prefix_len = 0;
std::string redString = hex.substr(0 + prefix_len, 2);
std::string greenString = hex.substr(2 + prefix_len, 2);
std::string blueString = hex.substr(4 + prefix_len, 2);
/*
if the prefix # was attached to hex, use the following code
string redString = hex.substr(1, 2);
string greenString = hex.substr(3, 2);
string blueString = hex.substr(5, 2);
*/
unsigned char red = (unsigned char)(convertFromHex(redString));
unsigned char green = (unsigned char)(convertFromHex(greenString));
unsigned char blue = (unsigned char)(convertFromHex(blueString));
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
}
extern "C" UIColor *colorFromHex(NSString *hexString);
extern "C" UIColor *colorFromDefaultsWithKey(NSString *defaults, NSString *key, NSString *fallback);
extern "C" UIColor *LCPParseColorString(NSString *colorStringFromPrefs, NSString *colorStringFallback);
UIColor *colorFromHex(NSString *hexString) {
if (hexString.length > 0) {
if ([hexString hasPrefix:@"#"])
hexString = [hexString substringFromIndex:1];
std::string hexColor;
std::vector<unsigned char> rgbColor(3);
hexColor = hexString.UTF8String;
if (hexColor.length() != 6) {
std::string sixDigitHexColor = "";
for (int i = 0; 6 > i; i++) {
switch (i) {
case 0:
sixDigitHexColor.append(hexColor.substr(i, 1));
sixDigitHexColor.append(hexColor.substr(i, 1));
break;
case 1:
sixDigitHexColor.append(hexColor.substr(i, 1));
sixDigitHexColor.append(hexColor.substr(i, 1));
break;
case 2:
sixDigitHexColor.append(hexColor.substr(i, 1));
sixDigitHexColor.append(hexColor.substr(i, 1));
break;
default:
break;
}
}
hexColor = sixDigitHexColor;
}
hextodec(hexColor, rgbColor);
return [UIColor colorWithRed:int(rgbColor[0]) / 255.f
green:int(rgbColor[1]) / 255.f
blue:int(rgbColor[2]) / 255.f
alpha:1];
} else { // Random
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
}
// do not use this method anymore
UIColor *colorFromDefaultsWithKey(NSString *defaults, NSString *key, NSString *fallback) {
NSMutableDictionary *preferencesPlist = [NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", defaults]];
//fallback
UIColor *fallbackColor = colorFromHex(fallback);
CGFloat currentAlpha = 1.0f;
if (preferencesPlist && [preferencesPlist objectForKey:key]) {
NSString *value = [preferencesPlist objectForKey:key];
NSArray *colorAndOrAlpha = [value componentsSeparatedByString:@":"];
if ([value rangeOfString:@":"].location != NSNotFound) {
if ([colorAndOrAlpha objectAtIndex:1])
currentAlpha = [colorAndOrAlpha[1] floatValue];
else
currentAlpha = 1;
}
if (!value)
return fallbackColor;
NSString *color = colorAndOrAlpha[0];
return [colorFromHex(color) colorWithAlphaComponent:currentAlpha];
} else {
return fallbackColor;
}
}
UIColor *LCPParseColorString(NSString *colorStringFromPrefs, NSString *colorStringFallback) {
//fallback
UIColor *fallbackColor = colorFromHex(colorStringFallback);
CGFloat currentAlpha = 1.0f;
if (colorStringFromPrefs && colorStringFromPrefs.length > 0) {
NSString *value = colorStringFromPrefs;
if (!value || value.length == 0)
return fallbackColor;
NSArray *colorAndOrAlpha = [value componentsSeparatedByString:@":"];
if ([value rangeOfString:@":"].location != NSNotFound) {
if ([colorAndOrAlpha objectAtIndex:1])
currentAlpha = [colorAndOrAlpha[1] floatValue];
else
currentAlpha = 1.0f;
}
if (!value)
return fallbackColor;
NSString *color = colorAndOrAlpha[0];
return [colorFromHex(color) colorWithAlphaComponent:currentAlpha];
} else {
return fallbackColor;
}
}