aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2019-01-07 14:47:27 +0200
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:34 +0530
commit59bfa7f1df470af711187335f604422dfafca634 (patch)
treebcc80efb0af5a800fc830837616c63d06301a878 /WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift
parentmacOS: Tunnel edit view (diff)
downloadwireguard-apple-59bfa7f1df470af711187335f604422dfafca634.tar.xz
wireguard-apple-59bfa7f1df470af711187335f604422dfafca634.zip
Added syntax highlighting conf textview
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift')
-rw-r--r--WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift25
1 files changed, 25 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift b/WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift
new file mode 100644
index 0000000..abd5723
--- /dev/null
+++ b/WireGuard/WireGuard/UI/macOS/NSColor+Hex.swift
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import AppKit
+
+extension NSColor {
+
+ convenience init(hex: String) {
+ var hexString = hex.uppercased()
+
+ if hexString.hasPrefix("#") {
+ hexString.remove(at: hexString.startIndex)
+ }
+
+ if hexString.count != 6 {
+ fatalError("Invalid hex string \(hex)")
+ }
+
+ var rgb: UInt32 = 0
+ Scanner(string: hexString).scanHexInt32(&rgb)
+
+ self.init(red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0, blue: CGFloat(rgb & 0x0000FF) / 255.0, alpha: 1)
+ }
+
+}