Issue: hexString does not honour the color's alpha component.
Although the color's alpha is retrieved as part of the getting of the components, it is ignored, and the output string is formatted to #RRGGBB instead of #RRGGBBAA
Desired outcome: if alpha is 1, then the current form should be returned. Otherwise, the string should be formatted to #(8 chars)
Cheers.
@SuperWomble Thank you for reporting this. Do you want to submit a pull request with this change?
TBH, and I am shamed about this, but I'm a total github nub. I barely know how to read the site, let alone contribute anything more than issues. ::deep, deep shame::
@SuperWomble There's nothing to be ashamed about! I'm _still_ a git noob 馃槓
okay so i saw this and decided to close, but need review on following snippet:
edit: String(format:)
edit2: fix error
extension UIColor {
@available(*, deprecated, message: "Variable format no longer supported, use 'hexString(withAlpha:)' instead")
public var hexString: String {
return hexString(withAlpha: false)
}
public func hexString(withAlpha: Bool = false) -> String {
let alphaShift = withAlpha ? 8 : 0
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
let rgb: Int = (withAlpha ? (Int)(alpha*255) : 0)
| (Int)(red*255) << (16+alphaShift)
| (Int)(green*255) << (8+alphaShift)
| (Int)(blue*255) << (0+alphaShift)
return String(format: (withAlpha ? "#%08x" : "#%06x"), rgb).uppercased()
}
}
let color = UIColor.init(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)
print(color.hexString)
print(color.hexString(withAlpha: true))
@pvtmert Just one thing, the swift String class also has a constructor String(format: ), so you don't need to use NSString then cast it to string :)
@SuperWomble shouldn't the format with transparency be #AARRGGBB ?
afaik in css it is called RGBA
here is ref: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgba
@pvtmert Would you please submit a pull request with the changes so we can discuss it there.
@pvtmert sure in CSS but Android use ARGB (https://developer.android.com/guide/topics/resources/more-resources.html#Color).
In an app I would more often share values with Android than with web.
Anyway this is just my two cents and with the new swift 4 String refactor it will be easy to move those chars.
okay it has been 2 weeks since last comment, so lets decide on something what to do
ARGB = 馃憤
RGBA = 馃憥
okay lets start voting, i'll make pr later on
Fixed in #228, thanks to @pvtmert 馃帀
Most helpful comment
okay it has been 2 weeks since last comment, so lets decide on something what to do
ARGB = 馃憤
RGBA = 馃憥
okay lets start voting, i'll make pr later on