in Cyborg/XMLSchema.swift [96:134]
init?(_ string: XMLString) {
if string.count == 0 {
return nil
} else if string[safeIndex: 0] == .questionMark {
if string.count > 1 {
self = .theme(name: String(copying: string[1..<string.count]))
} else {
return nil
}
} else if string[safeIndex: 0] == .at {
if string.count > 1 {
self = .resource(named: String(copying: string[1..<string.count]))
} else {
return nil
}
} else {
let hasAlpha = (string.count == 9)
// munge the string into a form that Init.init(_:, radix:) can understand
var withoutLeadingHashTag = String(withoutCopying: string)
_ = withoutLeadingHashTag.remove(at: withoutLeadingHashTag.startIndex)
if withoutLeadingHashTag.count == 3 {
// convert from shorthand hexadecimal form, which doesn't work with the init
withoutLeadingHashTag.append(withoutLeadingHashTag)
}
if let value = Int64(withoutLeadingHashTag, radix: 16) {
func component(_ mask: Int64, _ shift: Int64) -> CGFloat {
return CGFloat((value & mask) >> shift) / 255
}
let alpha = hasAlpha ? component(0xFF000000, 24) : 1.0
let color = UIColor(red: component(0xFF0000, 16),
green: component(0xFF00, 8),
blue: component(0xFF, 0),
alpha: alpha)
self = .hex(value: color)
} else {
return nil
}
}
}