Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Capability to Show Color Representation in HEX Format, Resolve Issue #84 #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
@interface HYPUIHelpers : NSObject

+(NSString *)rgbTextForColor:(UIColor *)color;
+(NSString *)hexTextForColor:(UIColor *)color;

@end
20 changes: 20 additions & 0 deletions AttributesInspector/AttributesViewer/Helpers/HYPUIHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,24 @@ +(NSString *)rgbTextForColor:(UIColor *)color
return nil;
}

+(NSString *)hexTextForColor:(UIColor *)color
{
CGFloat red = 0;
CGFloat green = 0;
CGFloat blue = 0;
CGFloat alpha = 0;

if ([color getRed:&red green:&green blue:&blue alpha:&alpha])
{
int r = round(red * 255);
int g = round(green * 255);
int b = round(blue * 255);

NSString *hexString = [NSString stringWithFormat:@"#%02X%02X%02X", r, g, b];
return hexString;
}

return nil;
}

@end
24 changes: 24 additions & 0 deletions AttributesInspector/AttributesViewer/PreviewViews/HYPTextPreview.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,35 @@ -(void)setupWithFont:(UIFont *)font color:(UIColor *)color
if (colorText)
{
self.colorLabel.text = [NSString stringWithFormat:@"RGBA %@", colorText];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
self.colorLabel.userInteractionEnabled = YES;
[self.colorLabel addGestureRecognizer:tapGesture];
}
else
{
self.colorLabel.text = @"--";
}
}

-(void)labelTapped:(UITapGestureRecognizer *)gestureRecognizer
{
if ([self.colorLabel.text hasPrefix:@"RGBA"])
{
NSString *colorText = [HYPUIHelpers hexTextForColor:_textColor];
if (colorText)
{
self.colorLabel.text = [NSString stringWithFormat:@"HEX %@", colorText];
}
}
else
{
NSString *colorText = [HYPUIHelpers rgbTextForColor:_textColor];
if (colorText)
{
self.colorLabel.text = [NSString stringWithFormat:@"RGBA %@", colorText];
}
}
}

@end
23 changes: 23 additions & 0 deletions AttributesInspector/AttributesViewer/PreviewViews/HYPViewPreview.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ -(instancetype)initWithPreviewTargetView:(UIView *)view
if (rgbText)
{
_colorLabel.text = [NSString stringWithFormat:@"RGBA %@", rgbText];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
_colorLabel.userInteractionEnabled = YES;
[_colorLabel addGestureRecognizer:tapGesture];
}
else
{
Expand All @@ -57,5 +61,24 @@ -(instancetype)initWithPreviewTargetView:(UIView *)view
return self;
}

-(void)labelTapped:(UITapGestureRecognizer *)gestureRecognizer
{
if ([self.colorLabel.text hasPrefix:@"RGBA"])
{
NSString *colorText = [HYPUIHelpers hexTextForColor:_previewTarget.backgroundColor];
if (colorText)
{
self.colorLabel.text = [NSString stringWithFormat:@"HEX %@", colorText];
}
}
else
{
NSString *colorText = [HYPUIHelpers rgbTextForColor:_previewTarget.backgroundColor];
if (colorText)
{
self.colorLabel.text = [NSString stringWithFormat:@"RGBA %@", colorText];
}
}
}

@end