-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uicolor hexcolor init #136
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
359ff2b
🎨 Increase deployment target
7b3f4f4
🎨 Update hexString init
7788378
Revert "🎨 Update hexString init"
0033349
Revert "🎨 Increase deployment target"
d04ca32
🎨 Update hexString init
95b64b9
Merge remote-tracking branch 'origin/main' into uicolor-hexcolor-init
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,38 @@ public extension UIColor { | |
} | ||
|
||
/** | ||
Initialize color from hex string (eg. "#ff00ff"). Leading '#' is mandatory. | ||
Initialize color from hex string (eg. "#ff00ff"). Leading '#' is not mandatory. | ||
|
||
- parameter hexString: Hex string to create color from | ||
*/ | ||
convenience init(hexString: String) { | ||
var rgbValue: UInt32 = 0 | ||
let scanner = Scanner(string: hexString) | ||
scanner.scanLocation = 1 // bypass '#' character | ||
scanner.scanHexInt32(&rgbValue) | ||
self.init(hex: rgbValue) | ||
} | ||
convenience init(hexString: String) { | ||
var hexSanitized = hexString.trimmingCharacters(in: .whitespacesAndNewlines) | ||
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") | ||
|
||
var rgb: UInt64 = 0 | ||
|
||
var r: CGFloat = 0.0 | ||
var g: CGFloat = 0.0 | ||
var b: CGFloat = 0.0 | ||
let a: CGFloat = 1.0 | ||
|
||
let length = hexSanitized.count | ||
|
||
guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { | ||
self.init() | ||
return | ||
} | ||
|
||
if length == 6 { | ||
r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0 | ||
g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0 | ||
b = CGFloat(rgb & 0x0000FF) / 255.0 | ||
} else { | ||
self.init() | ||
} | ||
|
||
Comment on lines
+27
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👋 Hey, I agree with @olejnjak. By the way, comments are missing...and what about correct indentation? Is it just copy&paste from Stack Overflow? In the case of using ChatGPT, I would ask to add comments 🤔 |
||
self.init(red: r, green: g, blue: b, alpha: a) | ||
} | ||
|
||
/** | ||
Returns color as hex string (eg. '#ff00ff') or nil if RGBA components couldn't be loaded. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To achieve this kind of change you can simply use the original 5 line solution and only adjust
scanLocation
based on whether the string has leading hash or not.