Skip to content

Commit 659d196

Browse files
committed
tweak helper function for generating highlighted background image
for buttons
1 parent 8d8ffb5 commit 659d196

1 file changed

Lines changed: 41 additions & 62 deletions

File tree

SIAlertView/SIAlertView.m

Lines changed: 41 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ + (void)initialize
235235
appearance.seperatorColor = [UIColor colorWithWhite:0 alpha:0.1];
236236
appearance.cornerRadius = 2;
237237

238-
appearance.defaultButtonBackgroundColor = [UIColor colorWithWhite:0 alpha:0.01];
239-
appearance.cancelButtonBackgroundColor = [UIColor colorWithWhite:0 alpha:0.03];
240-
appearance.destructiveButtonBackgroundColor = [UIColor colorWithWhite:0 alpha:0.01];
241-
242238
appearance.defaultButtonBackgroundColor = [UIColor colorWithWhite:0.99 alpha:1];
243239
appearance.cancelButtonBackgroundColor = [UIColor colorWithWhite:0.97 alpha:1];
244240
appearance.destructiveButtonBackgroundColor = [UIColor colorWithWhite:0.99 alpha:1];
@@ -986,20 +982,20 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
986982
case SIAlertViewButtonTypeCancel:
987983
if (self.cancelButtonBackgroundColor) {
988984
normalImage = [self imageWithUIColor:self.cancelButtonBackgroundColor];
989-
highlightedImage = [self imageWithUIColor:[self darkerColorForColor:self.cancelButtonBackgroundColor]];
985+
highlightedImage = [self imageWithUIColor:[self highlightedColorWithColor:self.cancelButtonBackgroundColor]];
990986
}
991987
break;
992988
case SIAlertViewButtonTypeDestructive:
993989
if (self.destructiveButtonBackgroundColor) {
994990
normalImage = [self imageWithUIColor:self.destructiveButtonBackgroundColor];
995-
highlightedImage = [self imageWithUIColor:[self darkerColorForColor:self.destructiveButtonBackgroundColor]];
991+
highlightedImage = [self imageWithUIColor:[self highlightedColorWithColor:self.destructiveButtonBackgroundColor]];
996992
}
997993
break;
998994
case SIAlertViewButtonTypeDefault:
999995
default:
1000996
if (self.defaultButtonBackgroundColor) {
1001997
normalImage = [self imageWithUIColor:self.defaultButtonBackgroundColor];
1002-
highlightedImage = [self imageWithUIColor:[self darkerColorForColor:self.defaultButtonBackgroundColor]];
998+
highlightedImage = [self imageWithUIColor:[self highlightedColorWithColor:self.defaultButtonBackgroundColor]];
1003999
}
10041000
break;
10051001
}
@@ -1010,61 +1006,6 @@ - (UIButton *)buttonForItemIndex:(NSUInteger)index
10101006
return button;
10111007
}
10121008

1013-
// grabbed from http://www.cocoanetics.com/2009/10/manipulating-uicolors/
1014-
- (UIColor *)darkerColorForColor:(UIColor *)color
1015-
{
1016-
// oldComponents is the array INSIDE the original color
1017-
// changing these changes the original, so we copy it
1018-
CGFloat *oldComponents = (CGFloat *)CGColorGetComponents([color CGColor]);
1019-
CGFloat newComponents[4];
1020-
1021-
int numComponents = CGColorGetNumberOfComponents([color CGColor]);
1022-
1023-
CGFloat dark = 0.95;
1024-
1025-
switch (numComponents)
1026-
{
1027-
case 2:
1028-
{
1029-
//grayscale
1030-
newComponents[0] = oldComponents[0]*dark;
1031-
newComponents[1] = oldComponents[0]*dark;
1032-
newComponents[2] = oldComponents[0]*dark;
1033-
newComponents[3] = oldComponents[1];
1034-
break;
1035-
}
1036-
case 4:
1037-
{
1038-
//RGBA
1039-
newComponents[0] = oldComponents[0]*dark;
1040-
newComponents[1] = oldComponents[1]*dark;
1041-
newComponents[2] = oldComponents[2]*dark;
1042-
newComponents[3] = oldComponents[3];
1043-
break;
1044-
}
1045-
}
1046-
1047-
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
1048-
CGColorRef newColor = CGColorCreate(colorSpace, newComponents);
1049-
CGColorSpaceRelease(colorSpace);
1050-
1051-
UIColor *retColor = [UIColor colorWithCGColor:newColor];
1052-
CGColorRelease(newColor);
1053-
1054-
return retColor;
1055-
}
1056-
1057-
- (UIImage *)imageWithUIColor:(UIColor *)color
1058-
{
1059-
CGRect rect = CGRectMake(0, 0, 1, 1);
1060-
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
1061-
[color set];
1062-
UIRectFill(rect);
1063-
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
1064-
UIGraphicsEndImageContext();
1065-
return image;
1066-
}
1067-
10681009
#pragma mark - Actions
10691010

10701011
- (void)buttonAction:(UIButton *)button
@@ -1087,6 +1028,44 @@ - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
10871028
}
10881029
}
10891030

1031+
#pragma mark - Helpers
1032+
1033+
// auto detected darken or lighten
1034+
- (UIColor *)highlightedColorWithColor:(UIColor *)color
1035+
{
1036+
CGFloat hue;
1037+
CGFloat saturation;
1038+
CGFloat brightness;
1039+
CGFloat alpha;
1040+
CGFloat adjustment = 0.1;
1041+
1042+
int numComponents = CGColorGetNumberOfComponents([color CGColor]);
1043+
1044+
// grayscale
1045+
if (numComponents == 2) {
1046+
[color getWhite:&brightness alpha:&alpha];
1047+
brightness += brightness > 0.5 ? -adjustment : adjustment * 2; // emphasize lighten adjustment value by two
1048+
return [UIColor colorWithWhite:brightness alpha:alpha];
1049+
}
1050+
1051+
// RGBA
1052+
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
1053+
brightness += brightness > 0.5 ? -adjustment : adjustment * 2;
1054+
1055+
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
1056+
}
1057+
1058+
- (UIImage *)imageWithUIColor:(UIColor *)color
1059+
{
1060+
CGRect rect = CGRectMake(0, 0, 1, 1);
1061+
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
1062+
[color set];
1063+
UIRectFill(rect);
1064+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
1065+
UIGraphicsEndImageContext();
1066+
return image;
1067+
}
1068+
10901069
#pragma mark - UIAppearance setters
10911070

10921071
- (void)setViewBackgroundColor:(UIColor *)viewBackgroundColor

0 commit comments

Comments
 (0)