Skip to content

Commit 4c1d165

Browse files
committed
Correct ColorHexString with only 2 components
1 parent 811b4e7 commit 4c1d165

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

AppDevKitTests/AppDevCommonKit/UIColor+ADKHexPresentationSpec.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@
135135
UIColor *expectedColor = [UIColor colorWithRed:123.0f / 255.0f green:0.0 / 255.0f blue:153.0f / 255.0f alpha:1.0f];
136136
[[[expectedColor ADKHexString] should] equal:@"7B0099"];
137137
});
138+
139+
it(@"given white color, should get string FFFFFF", ^{
140+
UIColor *expectedColor = [UIColor whiteColor];
141+
[[[expectedColor ADKHexString] should] equal:@"FFFFFF"];
142+
});
143+
144+
it(@"given black color, should get string 000000", ^{
145+
UIColor *expectedColor = [UIColor blackColor];
146+
[[[expectedColor ADKHexString] should] equal:@"000000"];
147+
});
138148
});
139149

140150
describe(@"Test ADKColorWithRGBHexString", ^{

AppDevPods/AppDevCommonKit/UIColor+ADKHexPresentation.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,19 @@ - (BOOL)ADKIsEqualToColor:(UIColor *)anotherColor
219219
- (NSString *)ADKHexString
220220
{
221221
const CGFloat *components = CGColorGetComponents(self.CGColor);
222+
CGFloat red, green, blue;
222223

223-
CGFloat red = components[0];
224-
CGFloat greeen = components[1];
225-
CGFloat blue = components[2];
224+
if (CGColorGetNumberOfComponents(self.CGColor) == 2) {
225+
red = components[0];
226+
green = components[0];
227+
blue = components[0];
228+
} else {
229+
red = components[0];
230+
green = components[1];
231+
blue = components[2];
232+
}
226233

227-
return [NSString stringWithFormat:@"%02lX%02lX%02lX", lroundf(red * 255), lroundf(greeen * 255), lroundf(blue * 255)];
234+
return [NSString stringWithFormat:@"%02lX%02lX%02lX", lroundf(red * 255), lroundf(green * 255), lroundf(blue * 255)];
228235
}
229236

230237
@end

0 commit comments

Comments
 (0)