Browse Source

refactor: leverage Skia helpers for NSColor ANSI logic (#37488)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
trop[bot] 2 years ago
parent
commit
eeea337757

+ 0 - 2
filenames.gni

@@ -175,8 +175,6 @@ filenames = {
     "shell/browser/ui/cocoa/electron_touch_bar.mm",
     "shell/browser/ui/cocoa/event_dispatching_window.h",
     "shell/browser/ui/cocoa/event_dispatching_window.mm",
-    "shell/browser/ui/cocoa/NSColor+Hex.h",
-    "shell/browser/ui/cocoa/NSColor+Hex.mm",
     "shell/browser/ui/cocoa/NSString+ANSI.h",
     "shell/browser/ui/cocoa/NSString+ANSI.mm",
     "shell/browser/ui/cocoa/root_view_mac.h",

+ 0 - 1
shell/browser/api/electron_api_system_preferences_mac.mm

@@ -25,7 +25,6 @@
 #include "net/base/mac/url_conversions.h"
 #include "shell/browser/mac/dict_util.h"
 #include "shell/browser/mac/electron_application.h"
-#include "shell/browser/ui/cocoa/NSColor+Hex.h"
 #include "shell/common/color_util.h"
 #include "shell/common/gin_converters/gurl_converter.h"
 #include "shell/common/gin_converters/value_converter.h"

+ 0 - 18
shell/browser/ui/cocoa/NSColor+Hex.h

@@ -1,18 +0,0 @@
-// Created by Mathias Leppich on 03/02/14.
-// Copyright (c) 2014 Bit Bar. All rights reserved.
-// Copyright (c) 2017 GitHub, Inc.
-// Use of this source code is governed by the MIT license that can be
-// found in the LICENSE file.
-
-#ifndef ELECTRON_SHELL_BROWSER_UI_COCOA_NSCOLOR_HEX_H_
-#define ELECTRON_SHELL_BROWSER_UI_COCOA_NSCOLOR_HEX_H_
-
-#import <Cocoa/Cocoa.h>
-
-@interface NSColor (Hex)
-- (NSString*)hexadecimalValue;
-- (NSString*)RGBAValue;
-+ (NSColor*)colorWithHexColorString:(NSString*)hex;
-@end
-
-#endif  // ELECTRON_SHELL_BROWSER_UI_COCOA_NSCOLOR_HEX_H_

+ 0 - 77
shell/browser/ui/cocoa/NSColor+Hex.mm

@@ -1,77 +0,0 @@
-// Created by Mathias Leppich on 03/02/14.
-// Copyright (c) 2014 Bit Bar. All rights reserved.
-// Copyright (c) 2017 GitHub, Inc.
-// Use of this source code is governed by the MIT license that can be
-// found in the LICENSE file.
-
-#include "shell/browser/ui/cocoa/NSColor+Hex.h"
-
-@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;
-
-  if (inColorString) {
-    NSScanner* scanner = [NSScanner scannerWithString:inColorString];
-    (void)[scanner scanHexInt:&colorCode];  // ignore error
-  }
-
-  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
-                                    blue:(CGFloat)blueByte / 0xff
-                                   alpha:1.0];
-}
-
-@end

+ 37 - 17
shell/browser/ui/cocoa/NSString+ANSI.mm

@@ -4,9 +4,12 @@
 // Use of this source code is governed by the MIT license that can be
 // found in the LICENSE file.
 
+#import <Cocoa/Cocoa.h>
+
 #include "base/mac/scoped_nsobject.h"
-#include "shell/browser/ui/cocoa/NSColor+Hex.h"
+#include "content/public/common/color_parser.h"
 #include "shell/browser/ui/cocoa/NSString+ANSI.h"
+#include "skia/ext/skia_utils_mac.h"
 
 @implementation NSMutableDictionary (ANSI)
 
@@ -18,6 +21,7 @@
 
   for (NSString* codeString in codeArray) {
     int code = codeString.intValue;
+    SkColor color;
     switch (code) {
       case 0:
         [self removeAllObjects];
@@ -37,36 +41,44 @@
         // case 24: underlined off
 
       case 30:
+        content::ParseHexColorString(bold ? "#7f7f7f" : "#000000", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"7f7f7f" : @"000000"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 31:
+        content::ParseHexColorString(bold ? "#cd0000" : "#ff0000", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"cd0000" : @"ff0000"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 32:
+        content::ParseHexColorString(bold ? "#00cd00" : "#00ff00", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"00cd00" : @"00ff00"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 33:
+        content::ParseHexColorString(bold ? "#cdcd00" : "#ffff00", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"cdcd00" : @"ffff00"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 34:
+        content::ParseHexColorString(bold ? "#0000ee" : "#5c5cff", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"0000ee" : @"5c5cff"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 35:
+        content::ParseHexColorString(bold ? "#cd00cd" : "#ff00ff", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"cd00cd" : @"ff00ff"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 36:
+        content::ParseHexColorString(bold ? "#00cdcd" : "#00ffff", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"00cdcd" : @"00ffff"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 37:
+        content::ParseHexColorString(bold ? "#e5e5e5" : "#ffffff", &color);
         self[NSForegroundColorAttributeName] =
-            [NSColor colorWithHexColorString:bold ? @"e5e5e5" : @"ffffff"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
 
       case 39:
@@ -74,36 +86,44 @@
         break;
 
       case 40:
+        content::ParseHexColorString("#7f7f7f", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"7f7f7f"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 41:
+        content::ParseHexColorString("#cd0000", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"cd0000"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 42:
+        content::ParseHexColorString("#00cd00", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"00cd00"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 43:
+        content::ParseHexColorString("#cdcd00", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"cdcd00"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 44:
+        content::ParseHexColorString("#0000ee", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"0000ee"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 45:
+        content::ParseHexColorString("cd00cd", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"cd00cd"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 46:
+        content::ParseHexColorString("#00cdcd", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"00cdcd"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
       case 47:
+        content::ParseHexColorString("#e5e5e5", &color);
         self[NSBackgroundColorAttributeName] =
-            [NSColor colorWithHexColorString:@"e5e5e5"];
+            skia::SkColorToCalibratedNSColor(color);
         break;
 
       case 49: