From 14e3ad600edfb7ce6470907e1ece232183315b68 Mon Sep 17 00:00:00 2001 From: jimmy8854 Date: Thu, 30 Nov 2023 17:47:42 +0800 Subject: [PATCH 1/2] feat: support Prefix "0x" --- Sources/Core/DynamicColor.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/Core/DynamicColor.swift b/Sources/Core/DynamicColor.swift index a797f2e..ddbcbb6 100644 --- a/Sources/Core/DynamicColor.swift +++ b/Sources/Core/DynamicColor.swift @@ -55,7 +55,10 @@ public extension DynamicColor { - parameter hexString: A hexa-decimal color string representation. */ convenience init(hexString: String) { - let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines) + var hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines) + if hexString.hasPrefix("0x"){ + hexString = hexString.removingPrefixes(["0x"]) + } let scanner = Scanner(string: hexString) scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#") @@ -224,3 +227,12 @@ public extension DynamicColor { return self.contrastRatio(with: otherColor) > context.minimumContrastRatio } } + +extension String{ + + public func removingPrefixes(_ prefixes: [String]) -> String { + let pattern = "^(\(prefixes.map{"\\Q"+$0+"\\E"}.joined(separator: "|")))\\s?" + guard let range = self.range(of: pattern, options: [.regularExpression, .caseInsensitive]) else { return self } + return String(self[range.upperBound...]) + } +} From 01693329373dbca4b28482b2b9cef977ad5869cc Mon Sep 17 00:00:00 2001 From: jimmy8854 Date: Thu, 30 Nov 2023 20:57:51 +0800 Subject: [PATCH 2/2] feat: support 0x prefix and alpha in fron --- Sources/Core/DynamicColor.swift | 34 +++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Sources/Core/DynamicColor.swift b/Sources/Core/DynamicColor.swift index ddbcbb6..c7a0d37 100644 --- a/Sources/Core/DynamicColor.swift +++ b/Sources/Core/DynamicColor.swift @@ -46,6 +46,35 @@ public extension DynamicColor { // MARK: - Manipulating Hexa-decimal Values and Strings + + + /** + Creates a color from an hex string (e.g. "#3498db"). The RGBA string are also supported (e.g. "#ff3498db"). ff is alpha + + support (0x3498db) + + If the given hex string is invalid the initialiser will create a black color. + + - parameter hexString: A hexa-decimal color string representation. + */ + convenience init(_ hexString: String, alphaInFront front: Bool = true) { + var hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines) + if hexString.hasPrefix("0x"){ + hexString = hexString.removingPrefixes(["0x"]) + } + + if hexString.hasPrefix("#"){ + hexString = hexString.removingPrefixes(["#"]) + } + + if hexString.count > 7 && front { + let prefix = String(hexString.prefix(2)) // 获取最前面的两个字符 + let middle = String(hexString.dropFirst(2)) // 获取中间的部分 + hexString = middle + prefix // 重新组合字符串 + } + self.init(hexString: hexString) + } + /** Creates a color from an hex string (e.g. "#3498db"). The RGBA string are also supported (e.g. "#3498dbff"). @@ -55,10 +84,7 @@ public extension DynamicColor { - parameter hexString: A hexa-decimal color string representation. */ convenience init(hexString: String) { - var hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines) - if hexString.hasPrefix("0x"){ - hexString = hexString.removingPrefixes(["0x"]) - } + let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines) let scanner = Scanner(string: hexString) scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")