From 181dbccdc76ae407dc3654250d431f392ace4e28 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 3 May 2019 20:16:05 -0700 Subject: [PATCH] fix: crash on systemPreferences.getAccentColor() --- .../api/atom_api_system_preferences_mac.mm | 22 ++---- atom/browser/ui/cocoa/NSColor+Hex.h | 2 + atom/browser/ui/cocoa/NSColor+Hex.mm | 67 +++++++++++++++++-- 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 4b00710b77f1c..783171731249c 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -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" @@ -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, @@ -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, @@ -437,7 +423,7 @@ AVMediaType ParseMediaType(const std::string& media_type) { return ""; } - return ToRGBHex(sysColor); + return base::SysNSStringToUTF8([sysColor hexadecimalValue]); } bool SystemPreferences::CanPromptTouchID() { @@ -590,7 +576,7 @@ AVMediaType ParseMediaType(const std::string& media_type) { return ""; } - return ToRGBHex(sysColor); + return base::SysNSStringToUTF8([sysColor hexadecimalValue]); } std::string SystemPreferences::GetMediaAccessStatus( diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index 3584f034e0890..7557728afa3a2 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -10,6 +10,8 @@ #import @interface NSColor (Hex) +- (NSString*)hexadecimalValue; +- (NSString*)RGBAValue; + (NSColor*)colorWithHexColorString:(NSString*)hex; @end diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm index 6dd63fa386a69..0aa2b2a4976f8 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -8,17 +8,76 @@ @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; + + NSString* redHexValue = [NSString stringWithFormat:@"%02x", redIntValue]; + NSString* greenHexValue = + [NSString stringWithFormat:@"%02x", greenIntValue]; + NSString* blueHexValue = [NSString stringWithFormat:@"%02x", blueIntValue]; + NSString* alphaHexValue = + [NSString stringWithFormat:@"%02x", alphaIntValue]; + + return [NSString stringWithFormat:@"%@%@%@%@", redHexValue, greenHexValue, + blueHexValue, alphaHexValue]; + } + + 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; + + NSString* redHexValue = [NSString stringWithFormat:@"%02x", redIntValue]; + NSString* greenHexValue = + [NSString stringWithFormat:@"%02x", greenIntValue]; + NSString* 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; 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