Skip to content

Commit

Permalink
fix: crash on systemPreferences.getAccentColor() (#18194)
Browse files Browse the repository at this point in the history
  • Loading branch information
trop[bot] authored and codebytere committed May 7, 2019
1 parent 83a4b73 commit 7a759ea
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
22 changes: 4 additions & 18 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -11,6 +11,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/sdk_forward_declarations.h"
Expand Down Expand Up @@ -106,21 +107,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 @@ -400,7 +386,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 @@ -429,7 +415,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return "";
}

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

// static
Expand Down Expand Up @@ -519,7 +505,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return "";
}

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

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

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

Expand Down
56 changes: 52 additions & 4 deletions atom/browser/ui/cocoa/NSColor+Hex.mm
Expand Up @@ -8,17 +8,65 @@

@implementation NSColor (Hex)

- (NSString*)RGBAValue {
double redFloatValue, greenFloatValue, blueFloatValue, alphaFloatValue;

NSColor* convertedColor =
[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

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

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

return
[NSString stringWithFormat:@"%02x%02x%02x%02x", redIntValue,
greenIntValue, blueIntValue, alphaIntValue];
}

return nil;
}

- (NSString*)hexadecimalValue {
double redFloatValue, greenFloatValue, blueFloatValue;

NSColor* convertedColor =
[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

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

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

return [NSString stringWithFormat:@"#%02x%02x%02x", redIntValue,
greenIntValue, blueIntValue];
}

return nil;
}

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

if (inColorString) {
NSScanner* scanner = [NSScanner scannerWithString:inColorString];
(void)[scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsigned char)(colorCode >> 16);
greenByte = (unsigned char)(colorCode >> 8);
blueByte = (unsigned char)(colorCode); // masks off high bits

unsigned char redByte = (unsigned char)(colorCode >> 16);
unsigned char greenByte = (unsigned char)(colorCode >> 8);
unsigned char blueByte = (unsigned char)(colorCode); // masks off high bits

return [NSColor colorWithCalibratedRed:(CGFloat)redByte / 0xff
green:(CGFloat)greenByte / 0xff
Expand Down

0 comments on commit 7a759ea

Please sign in to comment.