Skip to content

Commit

Permalink
fix: crash on systemPreferences.getAccentColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 4, 2019
1 parent 0755857 commit bdff1c9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
22 changes: 4 additions & 18 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -16,6 +16,7 @@

#include "atom/browser/mac/atom_application.h"
#include "atom/browser/mac/dict_util.h"
#include "atom/browser/ui/cocoa/NSColor+Hex.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "base/mac/scoped_cftyperef.h"
Expand Down Expand Up @@ -114,21 +115,6 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

// Convert color to RGBA value like "aabbccdd"
std::string ToRGBA(NSColor* color) {
return base::StringPrintf(
"%02X%02X%02X%02X", (int)(color.redComponent * 0xFF),
(int)(color.greenComponent * 0xFF), (int)(color.blueComponent * 0xFF),
(int)(color.alphaComponent * 0xFF));
}

// Convert color to RGB hex value like "#ABCDEF"
std::string ToRGBHex(NSColor* color) {
return base::StringPrintf("#%02X%02X%02X", (int)(color.redComponent * 0xFF),
(int)(color.greenComponent * 0xFF),
(int)(color.blueComponent * 0xFF));
}

} // namespace

void SystemPreferences::PostNotification(const std::string& name,
Expand Down Expand Up @@ -408,7 +394,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
if (@available(macOS 10.14, *))
sysColor = [NSColor controlAccentColor];

return ToRGBA(sysColor);
return base::SysNSStringToUTF8([sysColor RGBAValue]);
}

std::string SystemPreferences::GetSystemColor(const std::string& color,
Expand Down Expand Up @@ -437,7 +423,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return "";
}

return ToRGBHex(sysColor);
return base::SysNSStringToUTF8([sysColor hexadecimalValue]);
}

bool SystemPreferences::CanPromptTouchID() {
Expand Down Expand Up @@ -590,7 +576,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return "";
}

return ToRGBHex(sysColor);
return base::SysNSStringToUTF8([sysColor hexadecimalValue]);
}

std::string SystemPreferences::GetMediaAccessStatus(
Expand Down
1 change: 1 addition & 0 deletions atom/browser/ui/cocoa/NSColor+Hex.h
Expand Up @@ -10,6 +10,7 @@
#import <Cocoa/Cocoa.h>

@interface NSColor (Hex)
- (NSString*)hexadecimalValue;
+ (NSColor*)colorWithHexColorString:(NSString*)hex;
@end

Expand Down
60 changes: 60 additions & 0 deletions atom/browser/ui/cocoa/NSColor+Hex.mm
Expand Up @@ -8,6 +8,66 @@

@implementation NSColor (Hex)

- (NSString*)RGBAValue {
double redFloatValue, greenFloatValue, blueFloatValue, alphaFloatValue;
int redIntValue, greenIntValue, blueIntValue, alphaIntValue;
NSString *redHexValue, *greenHexValue, *blueHexValue, *alphaHexValue;

NSColor* convertedColor =
[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

if (convertedColor) {
[convertedColor getRed:&redFloatValue
green:&greenFloatValue
blue:&blueFloatValue
alpha:&alphaFloatValue];

redIntValue = redFloatValue * 255.99999f;
greenIntValue = greenFloatValue * 255.99999f;
blueIntValue = blueFloatValue * 255.99999f;
alphaIntValue = alphaFloatValue * 255.99999f;

redHexValue = [NSString stringWithFormat:@"%02x", redIntValue];
greenHexValue = [NSString stringWithFormat:@"%02x", greenIntValue];
blueHexValue = [NSString stringWithFormat:@"%02x", blueIntValue];
alphaHexValue = [NSString stringWithFormat:@"%02x", alphaIntValue];

return [NSString stringWithFormat:@"%@%@%@%@", redHexValue, greenHexValue,
blueHexValue, alphaHexValue];
}

return nil;
}

- (NSString*)hexadecimalValue {
double redFloatValue, greenFloatValue, blueFloatValue;
int redIntValue, greenIntValue, blueIntValue;
NSString *redHexValue, *greenHexValue, *blueHexValue;

NSColor* convertedColor =
[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

if (convertedColor) {
[convertedColor getRed:&redFloatValue
green:&greenFloatValue
blue:&blueFloatValue
alpha:NULL];

redIntValue = redFloatValue * 255.99999f;
greenIntValue = greenFloatValue * 255.99999f;
blueIntValue = blueFloatValue * 255.99999f;

redHexValue = [NSString stringWithFormat:@"%02x", redIntValue];
greenHexValue = [NSString stringWithFormat:@"%02x", greenIntValue];
blueHexValue = [NSString stringWithFormat:@"%02x", blueIntValue];

return [NSString
stringWithFormat:@"#%@%@%@", redHexValue, greenHexValue, blueHexValue];
}

return nil;
}

+ (NSColor*)colorWithHexColorString:(NSString*)inColorString {
unsigned colorCode = 0;
unsigned char redByte, greenByte, blueByte;
Expand Down

0 comments on commit bdff1c9

Please sign in to comment.