Commit 5caacf8acf22e16f698f7d1311dfb6e35e7f1682
1 parent
8e77cf68
提交Extensions文件夹
Showing
2 changed files
with
165 additions
and
32 deletions
metaCode.xcodeproj/project.pbxproj
| @@ -9,14 +9,14 @@ | @@ -9,14 +9,14 @@ | ||
| 9 | /* Begin PBXBuildFile section */ | 9 | /* Begin PBXBuildFile section */ |
| 10 | 0D39E93D7229931C86EB1161 /* Pods_metaCode_metaCodeUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63D879B49B1E42F1584DD75E /* Pods_metaCode_metaCodeUITests.framework */; }; | 10 | 0D39E93D7229931C86EB1161 /* Pods_metaCode_metaCodeUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63D879B49B1E42F1584DD75E /* Pods_metaCode_metaCodeUITests.framework */; }; |
| 11 | 1DD2808DF02C120276F99F4D /* Pods_metaCode.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F1C55A719CFEE073C06D37CE /* Pods_metaCode.framework */; }; | 11 | 1DD2808DF02C120276F99F4D /* Pods_metaCode.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F1C55A719CFEE073C06D37CE /* Pods_metaCode.framework */; }; |
| 12 | + 790D01822A2D88FC00C5978D /* CNLiveConfigMacro.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790D01812A2D88FC00C5978D /* CNLiveConfigMacro.swift */; }; | ||
| 13 | + 790D01852A2D89E100C5978D /* CNHexColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790D01842A2D89E100C5978D /* CNHexColor.swift */; }; | ||
| 12 | 796398932A260C0100D045EB /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7963988C2A260C0100D045EB /* ViewController.swift */; }; | 14 | 796398932A260C0100D045EB /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7963988C2A260C0100D045EB /* ViewController.swift */; }; |
| 13 | 796398942A260C0100D045EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7963988D2A260C0100D045EB /* AppDelegate.swift */; }; | 15 | 796398942A260C0100D045EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7963988D2A260C0100D045EB /* AppDelegate.swift */; }; |
| 14 | 7963989D2A260E6200D045EB /* CNLiveBaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7963989C2A260E6200D045EB /* CNLiveBaseViewController.swift */; }; | 16 | 7963989D2A260E6200D045EB /* CNLiveBaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7963989C2A260E6200D045EB /* CNLiveBaseViewController.swift */; }; |
| 15 | - 79A68C4A2A26E6CE00DC3B43 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79A68C492A26E6CE00DC3B43 /* Network.swift */; }; | ||
| 16 | - 79A68C4C2A26EA6000DC3B43 /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79A68C4B2A26EA6000DC3B43 /* API.swift */; }; | ||
| 17 | - 7CD675202A26F805008354C1 /* CNLiveConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD6751F2A26F805008354C1 /* CNLiveConfig.swift */; }; | 17 | + 79A68C4A2A26E6CE00DC3B43 /* (null) in Sources */ = {isa = PBXBuildFile; }; |
| 18 | + 79A68C4C2A26EA6000DC3B43 /* (null) in Sources */ = {isa = PBXBuildFile; }; | ||
| 18 | 7CD675292A270F6A008354C1 /* WJColorChange.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675282A270F6A008354C1 /* WJColorChange.m */; }; | 19 | 7CD675292A270F6A008354C1 /* WJColorChange.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675282A270F6A008354C1 /* WJColorChange.m */; }; |
| 19 | - 7CD6752E2A271FE6008354C1 /* CNHexColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD6752D2A271FE6008354C1 /* CNHexColor.swift */; }; | ||
| 20 | 7CD6753C2A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675372A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift */; }; | 20 | 7CD6753C2A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675372A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift */; }; |
| 21 | 7CD6753D2A2740B0008354C1 /* CNMainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675382A2740B0008354C1 /* CNMainView.swift */; }; | 21 | 7CD6753D2A2740B0008354C1 /* CNMainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675382A2740B0008354C1 /* CNMainView.swift */; }; |
| 22 | 7CD6753E2A2740B0008354C1 /* CNPayMoneyCodeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675392A2740B0008354C1 /* CNPayMoneyCodeView.swift */; }; | 22 | 7CD6753E2A2740B0008354C1 /* CNPayMoneyCodeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD675392A2740B0008354C1 /* CNPayMoneyCodeView.swift */; }; |
| @@ -60,15 +60,15 @@ | @@ -60,15 +60,15 @@ | ||
| 60 | 63D879B49B1E42F1584DD75E /* Pods_metaCode_metaCodeUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_metaCode_metaCodeUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; | 60 | 63D879B49B1E42F1584DD75E /* Pods_metaCode_metaCodeUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_metaCode_metaCodeUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; |
| 61 | 67F8190701D1AB7F7724E8BA /* Pods-metaCodeTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-metaCodeTests.release.xcconfig"; path = "Target Support Files/Pods-metaCodeTests/Pods-metaCodeTests.release.xcconfig"; sourceTree = "<group>"; }; | 61 | 67F8190701D1AB7F7724E8BA /* Pods-metaCodeTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-metaCodeTests.release.xcconfig"; path = "Target Support Files/Pods-metaCodeTests/Pods-metaCodeTests.release.xcconfig"; sourceTree = "<group>"; }; |
| 62 | 721865F548C997EFAE525B6F /* Pods-metaCode-metaCodeUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-metaCode-metaCodeUITests.release.xcconfig"; path = "Target Support Files/Pods-metaCode-metaCodeUITests/Pods-metaCode-metaCodeUITests.release.xcconfig"; sourceTree = "<group>"; }; | 62 | 721865F548C997EFAE525B6F /* Pods-metaCode-metaCodeUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-metaCode-metaCodeUITests.release.xcconfig"; path = "Target Support Files/Pods-metaCode-metaCodeUITests/Pods-metaCode-metaCodeUITests.release.xcconfig"; sourceTree = "<group>"; }; |
| 63 | + 790D01812A2D88FC00C5978D /* CNLiveConfigMacro.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNLiveConfigMacro.swift; sourceTree = "<group>"; }; | ||
| 64 | + 790D01842A2D89E100C5978D /* CNHexColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNHexColor.swift; sourceTree = "<group>"; }; | ||
| 63 | 7963988C2A260C0100D045EB /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; }; | 65 | 7963988C2A260C0100D045EB /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; }; |
| 64 | 7963988D2A260C0100D045EB /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; }; | 66 | 7963988D2A260C0100D045EB /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; }; |
| 65 | 7963989C2A260E6200D045EB /* CNLiveBaseViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNLiveBaseViewController.swift; sourceTree = "<group>"; }; | 67 | 7963989C2A260E6200D045EB /* CNLiveBaseViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNLiveBaseViewController.swift; sourceTree = "<group>"; }; |
| 66 | 79A68C512A26F59C00DC3B43 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = metaCode/Info.plist; sourceTree = "<group>"; }; | 68 | 79A68C512A26F59C00DC3B43 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = metaCode/Info.plist; sourceTree = "<group>"; }; |
| 67 | - 7CD6751F2A26F805008354C1 /* CNLiveConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CNLiveConfig.swift; path = ../../../../Documents/CNLiveConfig.swift; sourceTree = "<group>"; }; | ||
| 68 | 7CD675262A270F69008354C1 /* metaCode-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "metaCode-Bridging-Header.h"; sourceTree = "<group>"; }; | 69 | 7CD675262A270F69008354C1 /* metaCode-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "metaCode-Bridging-Header.h"; sourceTree = "<group>"; }; |
| 69 | 7CD675272A270F6A008354C1 /* WJColorChange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WJColorChange.h; sourceTree = "<group>"; }; | 70 | 7CD675272A270F6A008354C1 /* WJColorChange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WJColorChange.h; sourceTree = "<group>"; }; |
| 70 | 7CD675282A270F6A008354C1 /* WJColorChange.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WJColorChange.m; sourceTree = "<group>"; }; | 71 | 7CD675282A270F6A008354C1 /* WJColorChange.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WJColorChange.m; sourceTree = "<group>"; }; |
| 71 | - 7CD6752D2A271FE6008354C1 /* CNHexColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CNHexColor.swift; path = ../../../../CNLiveDigitalRMBHomeDeom/CNLiveDigitalRMBHomeDeom/Classes/NewPagedFlowView/CNHexColor.swift; sourceTree = "<group>"; }; | ||
| 72 | 7CD675372A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNBottomPayMoneyRemindView.swift; sourceTree = "<group>"; }; | 72 | 7CD675372A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNBottomPayMoneyRemindView.swift; sourceTree = "<group>"; }; |
| 73 | 7CD675382A2740B0008354C1 /* CNMainView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNMainView.swift; sourceTree = "<group>"; }; | 73 | 7CD675382A2740B0008354C1 /* CNMainView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNMainView.swift; sourceTree = "<group>"; }; |
| 74 | 7CD675392A2740B0008354C1 /* CNPayMoneyCodeView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNPayMoneyCodeView.swift; sourceTree = "<group>"; }; | 74 | 7CD675392A2740B0008354C1 /* CNPayMoneyCodeView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CNPayMoneyCodeView.swift; sourceTree = "<group>"; }; |
| @@ -136,11 +136,19 @@ | @@ -136,11 +136,19 @@ | ||
| 136 | path = Pods; | 136 | path = Pods; |
| 137 | sourceTree = "<group>"; | 137 | sourceTree = "<group>"; |
| 138 | }; | 138 | }; |
| 139 | + 790D01832A2D89E100C5978D /* Extensions */ = { | ||
| 140 | + isa = PBXGroup; | ||
| 141 | + children = ( | ||
| 142 | + 790D01842A2D89E100C5978D /* CNHexColor.swift */, | ||
| 143 | + ); | ||
| 144 | + path = Extensions; | ||
| 145 | + sourceTree = "<group>"; | ||
| 146 | + }; | ||
| 139 | 7963988A2A260B3300D045EB /* Classes */ = { | 147 | 7963988A2A260B3300D045EB /* Classes */ = { |
| 140 | isa = PBXGroup; | 148 | isa = PBXGroup; |
| 141 | children = ( | 149 | children = ( |
| 150 | + 790D01832A2D89E100C5978D /* Extensions */, | ||
| 142 | 7CD675502A29C036008354C1 /* Constant */, | 151 | 7CD675502A29C036008354C1 /* Constant */, |
| 143 | - 7CD6752C2A271FB1008354C1 /* Extensions */, | ||
| 144 | 796398912A260C0100D045EB /* Common */, | 152 | 796398912A260C0100D045EB /* Common */, |
| 145 | 796398922A260C0100D045EB /* Config */, | 153 | 796398922A260C0100D045EB /* Config */, |
| 146 | 796398902A260C0100D045EB /* Main */, | 154 | 796398902A260C0100D045EB /* Main */, |
| @@ -200,8 +208,7 @@ | @@ -200,8 +208,7 @@ | ||
| 200 | 796398922A260C0100D045EB /* Config */ = { | 208 | 796398922A260C0100D045EB /* Config */ = { |
| 201 | isa = PBXGroup; | 209 | isa = PBXGroup; |
| 202 | children = ( | 210 | children = ( |
| 203 | - | ||
| 204 | - 7CD6751F2A26F805008354C1 /* CNLiveConfig.swift */, | 211 | + 790D01812A2D88FC00C5978D /* CNLiveConfigMacro.swift */, |
| 205 | ); | 212 | ); |
| 206 | path = Config; | 213 | path = Config; |
| 207 | sourceTree = "<group>"; | 214 | sourceTree = "<group>"; |
| @@ -224,16 +231,6 @@ | @@ -224,16 +231,6 @@ | ||
| 224 | path = Base; | 231 | path = Base; |
| 225 | sourceTree = "<group>"; | 232 | sourceTree = "<group>"; |
| 226 | }; | 233 | }; |
| 227 | - | ||
| 228 | - 7963989E2A260E7600D045EB /* Network */ = { | ||
| 229 | - isa = PBXGroup; | ||
| 230 | - children = ( | ||
| 231 | - 79A68C492A26E6CE00DC3B43 /* Network.swift */, | ||
| 232 | - 79A68C4B2A26EA6000DC3B43 /* API.swift */, | ||
| 233 | - ); | ||
| 234 | - path = Network; | ||
| 235 | - sourceTree = "<group>"; | ||
| 236 | - }; | ||
| 237 | 7CD675212A26F844008354C1 /* HomePage */ = { | 234 | 7CD675212A26F844008354C1 /* HomePage */ = { |
| 238 | isa = PBXGroup; | 235 | isa = PBXGroup; |
| 239 | children = ( | 236 | children = ( |
| @@ -257,15 +254,6 @@ | @@ -257,15 +254,6 @@ | ||
| 257 | path = HomePage/View; | 254 | path = HomePage/View; |
| 258 | sourceTree = "<group>"; | 255 | sourceTree = "<group>"; |
| 259 | }; | 256 | }; |
| 260 | - 7CD6752C2A271FB1008354C1 /* Extensions */ = { | ||
| 261 | - isa = PBXGroup; | ||
| 262 | - children = ( | ||
| 263 | - 7CD6752D2A271FE6008354C1 /* CNHexColor.swift */, | ||
| 264 | - ); | ||
| 265 | - name = Extensions; | ||
| 266 | - path = Common; | ||
| 267 | - sourceTree = "<group>"; | ||
| 268 | - }; | ||
| 269 | 7CD6754D2A2840B9008354C1 /* Controller */ = { | 257 | 7CD6754D2A2840B9008354C1 /* Controller */ = { |
| 270 | isa = PBXGroup; | 258 | isa = PBXGroup; |
| 271 | children = ( | 259 | children = ( |
| @@ -585,7 +573,7 @@ | @@ -585,7 +573,7 @@ | ||
| 585 | 7CD675292A270F6A008354C1 /* WJColorChange.m in Sources */, | 573 | 7CD675292A270F6A008354C1 /* WJColorChange.m in Sources */, |
| 586 | 796398942A260C0100D045EB /* AppDelegate.swift in Sources */, | 574 | 796398942A260C0100D045EB /* AppDelegate.swift in Sources */, |
| 587 | 7963989D2A260E6200D045EB /* CNLiveBaseViewController.swift in Sources */, | 575 | 7963989D2A260E6200D045EB /* CNLiveBaseViewController.swift in Sources */, |
| 588 | - 7CD6752E2A271FE6008354C1 /* CNHexColor.swift in Sources */, | 576 | + 790D01822A2D88FC00C5978D /* CNLiveConfigMacro.swift in Sources */, |
| 589 | 7CD675482A274167008354C1 /* PGIndexBannerSubiew.m in Sources */, | 577 | 7CD675482A274167008354C1 /* PGIndexBannerSubiew.m in Sources */, |
| 590 | 7CD6753D2A2740B0008354C1 /* CNMainView.swift in Sources */, | 578 | 7CD6753D2A2740B0008354C1 /* CNMainView.swift in Sources */, |
| 591 | 7CD675522A29C04B008354C1 /* CNLiveConstant.swift in Sources */, | 579 | 7CD675522A29C04B008354C1 /* CNLiveConstant.swift in Sources */, |
| @@ -593,13 +581,13 @@ | @@ -593,13 +581,13 @@ | ||
| 593 | 7CD675402A2740B0008354C1 /* CNCollectMoneyCodeView.swift in Sources */, | 581 | 7CD675402A2740B0008354C1 /* CNCollectMoneyCodeView.swift in Sources */, |
| 594 | 7CD6754F2A2840B9008354C1 /* CNMetaCodeHomePageController.swift in Sources */, | 582 | 7CD6754F2A2840B9008354C1 /* CNMetaCodeHomePageController.swift in Sources */, |
| 595 | 7CD675472A274167008354C1 /* NewPagedFlowView.m in Sources */, | 583 | 7CD675472A274167008354C1 /* NewPagedFlowView.m in Sources */, |
| 596 | - 79A68C4A2A26E6CE00DC3B43 /* Network.swift in Sources */, | 584 | + 79A68C4A2A26E6CE00DC3B43 /* (null) in Sources */, |
| 585 | + 790D01852A2D89E100C5978D /* CNHexColor.swift in Sources */, | ||
| 597 | 796398932A260C0100D045EB /* ViewController.swift in Sources */, | 586 | 796398932A260C0100D045EB /* ViewController.swift in Sources */, |
| 598 | 7CD675422A2740F8008354C1 /* CNCustomIndexView.swift in Sources */, | 587 | 7CD675422A2740F8008354C1 /* CNCustomIndexView.swift in Sources */, |
| 599 | 7CD6753F2A2740B0008354C1 /* CNTopCollectMoneyRemindView.swift in Sources */, | 588 | 7CD6753F2A2740B0008354C1 /* CNTopCollectMoneyRemindView.swift in Sources */, |
| 600 | 7CD6753C2A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift in Sources */, | 589 | 7CD6753C2A2740B0008354C1 /* CNBottomPayMoneyRemindView.swift in Sources */, |
| 601 | - 7CD675202A26F805008354C1 /* CNLiveConfig.swift in Sources */, | ||
| 602 | - 79A68C4C2A26EA6000DC3B43 /* API.swift in Sources */, | 590 | + 79A68C4C2A26EA6000DC3B43 /* (null) in Sources */, |
| 603 | ); | 591 | ); |
| 604 | runOnlyForDeploymentPostprocessing = 0; | 592 | runOnlyForDeploymentPostprocessing = 0; |
| 605 | }; | 593 | }; |
metaCode/Classes/Extensions/CNHexColor.swift
0 → 100644
| 1 | +// | ||
| 2 | +// CNHexColor.swift | ||
| 3 | +// metaCode | ||
| 4 | +// | ||
| 5 | +// Created by daojian on 2023/5/31. | ||
| 6 | +// | ||
| 7 | + | ||
| 8 | +import Foundation | ||
| 9 | + | ||
| 10 | +#if os(iOS) || os(watchOS) || os(tvOS) | ||
| 11 | + import UIKit | ||
| 12 | + public typealias HexColor = UIColor | ||
| 13 | +#else | ||
| 14 | + import Cocoa | ||
| 15 | + public typealias HexColor = NSColor | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +public extension HexColor { | ||
| 20 | + typealias Hex = String | ||
| 21 | + | ||
| 22 | + convenience init?(_ hex: Hex, alpha: CGFloat? = nil) { | ||
| 23 | + | ||
| 24 | + guard let hexType = Type(from: hex), let components = hexType.components() else { | ||
| 25 | + return nil | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + #if os(iOS) || os(watchOS) || os(tvOS) | ||
| 29 | + self.init(red: components.red, green: components.green, blue: components.blue, alpha: alpha ?? components.alpha) | ||
| 30 | + #else | ||
| 31 | + self.init(calibratedRed: components.red, green: components.green, blue: components.blue, alpha: alpha ?? components.alpha) | ||
| 32 | + #endif | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /// The string hex value representation of the current color | ||
| 36 | + var hex: Hex { | ||
| 37 | + var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0, rgb: Int | ||
| 38 | + getRed(&r, green: &g, blue: &b, alpha: &a) | ||
| 39 | + | ||
| 40 | + if a == 1 { // no alpha value set, we are returning the short version | ||
| 41 | + rgb = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0 | ||
| 42 | + return String(format: "#%06x", rgb) | ||
| 43 | + } else { | ||
| 44 | + rgb = (Int)(r*255)<<24 | (Int)(g*255)<<16 | (Int)(b*255)<<8 | (Int)(a*255)<<0 | ||
| 45 | + return String(format: "#%08x", rgb) | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + private enum `Type` { | ||
| 50 | + | ||
| 51 | + case RGBshort(rgb: Hex) | ||
| 52 | + case RGBshortAlpha(rgba: Hex) | ||
| 53 | + case RGB(rgb: Hex) | ||
| 54 | + case RGBA(rgba: Hex) | ||
| 55 | + | ||
| 56 | + init?(from hex: Hex) { | ||
| 57 | + | ||
| 58 | + var hexString = hex | ||
| 59 | + hexString.removeHashIfNecessary() | ||
| 60 | + | ||
| 61 | + guard let t = Type.transform(hex: hexString) else { | ||
| 62 | + return nil | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + self = t | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + static func transform(hex string: Hex) -> Type? { | ||
| 69 | + switch string.count { | ||
| 70 | + case 3: | ||
| 71 | + return .RGBshort(rgb: string) | ||
| 72 | + case 4: | ||
| 73 | + return .RGBshortAlpha(rgba: string) | ||
| 74 | + case 6: | ||
| 75 | + return .RGB(rgb: string) | ||
| 76 | + case 8: | ||
| 77 | + return .RGBA(rgba: string) | ||
| 78 | + default: | ||
| 79 | + return nil | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + var value: Hex { | ||
| 84 | + switch self { | ||
| 85 | + case .RGBshort(let rgb): | ||
| 86 | + return rgb | ||
| 87 | + case .RGBshortAlpha(let rgba): | ||
| 88 | + return rgba | ||
| 89 | + case .RGB(let rgb): | ||
| 90 | + return rgb | ||
| 91 | + case .RGBA(let rgba): | ||
| 92 | + return rgba | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + typealias rgbComponents = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) | ||
| 97 | + func components() -> rgbComponents? { | ||
| 98 | + | ||
| 99 | + var hexValue: UInt32 = 0 | ||
| 100 | + guard Scanner(string: value).scanHexInt32(&hexValue) else { | ||
| 101 | + return nil | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + let r, g, b, a, divisor: CGFloat | ||
| 105 | + | ||
| 106 | + switch self { | ||
| 107 | + case .RGBshort(_): | ||
| 108 | + divisor = 15 | ||
| 109 | + r = CGFloat((hexValue & 0xF00) >> 8) / divisor | ||
| 110 | + g = CGFloat((hexValue & 0x0F0) >> 4) / divisor | ||
| 111 | + b = CGFloat( hexValue & 0x00F) / divisor | ||
| 112 | + a = 1 | ||
| 113 | + case .RGBshortAlpha(_): | ||
| 114 | + divisor = 15 | ||
| 115 | + r = CGFloat((hexValue & 0xF000) >> 12) / divisor | ||
| 116 | + g = CGFloat((hexValue & 0x0F00) >> 8) / divisor | ||
| 117 | + b = CGFloat((hexValue & 0x00F0) >> 4) / divisor | ||
| 118 | + a = CGFloat( hexValue & 0x000F) / divisor | ||
| 119 | + case .RGB(_): | ||
| 120 | + divisor = 255 | ||
| 121 | + r = CGFloat((hexValue & 0xFF0000) >> 16) / divisor | ||
| 122 | + g = CGFloat((hexValue & 0x00FF00) >> 8) / divisor | ||
| 123 | + b = CGFloat( hexValue & 0x0000FF) / divisor | ||
| 124 | + a = 1 | ||
| 125 | + case .RGBA(_): | ||
| 126 | + divisor = 255 | ||
| 127 | + r = CGFloat((hexValue & 0xFF000000) >> 24) / divisor | ||
| 128 | + g = CGFloat((hexValue & 0x00FF0000) >> 16) / divisor | ||
| 129 | + b = CGFloat((hexValue & 0x0000FF00) >> 8) / divisor | ||
| 130 | + a = CGFloat( hexValue & 0x000000FF) / divisor | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + return (red: r, green: g, blue: b, alpha: a) | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | +} | ||
| 137 | + | ||
| 138 | +private extension String { | ||
| 139 | + | ||
| 140 | + mutating func removeHashIfNecessary() { | ||
| 141 | + if hasPrefix("#") { | ||
| 142 | + self = replacingOccurrences(of: "#", with: "") | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | +} |