-
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
Conversation
@freed0m266 your pull request is missing a changelog! |
} | ||
convenience init(hexString: String) { | ||
var hexSanitized = hexString.trimmingCharacters(in: .whitespacesAndNewlines) | ||
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") |
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.
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() | ||
} | ||
|
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.
👋 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 🤔
This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Checklist