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..bf9b59fb90169 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -10,6 +10,7 @@ #import @interface NSColor (Hex) +- (NSString*)hexadecimalValue; + (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..90e94fade7f5f 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -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;