Merge 319647d537
into 3eefa464ee
This commit is contained in:
commit
cac988ad43
1 changed files with 39 additions and 42 deletions
|
@ -2,18 +2,11 @@
|
|||
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
*/
|
||||
|
||||
import AK
|
||||
@_exported import GfxCxx
|
||||
|
||||
// FIXME: Do this without extending String with an index operation that was explicitly deleted :^)
|
||||
extension Swift.String {
|
||||
subscript(_ index: Int) -> Character {
|
||||
return self[self.index(self.startIndex, offsetBy: index)]
|
||||
}
|
||||
}
|
||||
|
||||
private func hexNibbleToUInt8(_ nibble: Character) -> UInt8? {
|
||||
guard nibble.isHexDigit else {
|
||||
return nil
|
||||
|
@ -41,49 +34,53 @@ public func parseHexString(_ rawString: AK.StringView) -> [Gfx.Color] {
|
|||
|
||||
switch string.count {
|
||||
case 4:
|
||||
let r = hexNibbleToUInt8(string[1])
|
||||
let g = hexNibbleToUInt8(string[2])
|
||||
let b = hexNibbleToUInt8(string[3])
|
||||
|
||||
guard r != nil && g != nil && b != nil else {
|
||||
guard let r = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 1)]),
|
||||
let g = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 2)]),
|
||||
let b = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 3)]) else {
|
||||
return []
|
||||
}
|
||||
|
||||
return [Gfx.Color(r! * 17, g! * 17, b! * 17)]
|
||||
return [Gfx.Color(r * 17, g * 17, b * 17)]
|
||||
|
||||
case 5:
|
||||
let r = hexNibbleToUInt8(string[1])
|
||||
let g = hexNibbleToUInt8(string[2])
|
||||
let b = hexNibbleToUInt8(string[3])
|
||||
let a = hexNibbleToUInt8(string[4])
|
||||
|
||||
guard r != nil && g != nil && b != nil && a != nil else {
|
||||
guard let r = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 1)]),
|
||||
let g = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 2)]),
|
||||
let b = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 3)]),
|
||||
let a = hexNibbleToUInt8(string[string.index(string.startIndex, offsetBy: 4)]) else {
|
||||
return []
|
||||
}
|
||||
|
||||
return [Gfx.Color(r! * 17, g! * 17, b! * 17, a! * 17)]
|
||||
case 6: return []
|
||||
return [Gfx.Color(r * 17, g * 17, b * 17, a * 17)]
|
||||
|
||||
case 6:
|
||||
return []
|
||||
|
||||
case 7:
|
||||
let r = hexNibblesToUInt8(string[1], string[2])
|
||||
let g = hexNibblesToUInt8(string[3], string[4])
|
||||
let b = hexNibblesToUInt8(string[5], string[6])
|
||||
|
||||
guard r != nil && g != nil && b != nil else {
|
||||
guard let r = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 1)],
|
||||
string[string.index(string.startIndex, offsetBy: 2)]),
|
||||
let g = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 3)],
|
||||
string[string.index(string.startIndex, offsetBy: 4)]),
|
||||
let b = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 5)],
|
||||
string[string.index(string.startIndex, offsetBy: 6)]) else {
|
||||
return []
|
||||
}
|
||||
|
||||
return [Gfx.Color(r!, g!, b!, UInt8(255))]
|
||||
case 8: return []
|
||||
return [Gfx.Color(r, g, b, 255)]
|
||||
|
||||
case 8:
|
||||
return []
|
||||
|
||||
case 9:
|
||||
let r = hexNibblesToUInt8(string[1], string[2])
|
||||
let g = hexNibblesToUInt8(string[3], string[4])
|
||||
let b = hexNibblesToUInt8(string[5], string[6])
|
||||
let a = hexNibblesToUInt8(string[7], string[8])
|
||||
|
||||
guard r != nil && g != nil && b != nil && a != nil else {
|
||||
guard let r = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 1)],
|
||||
string[string.index(string.startIndex, offsetBy: 2)]),
|
||||
let g = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 3)],
|
||||
string[string.index(string.startIndex, offsetBy: 4)]),
|
||||
let b = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 5)],
|
||||
string[string.index(string.startIndex, offsetBy: 6)]),
|
||||
let a = hexNibblesToUInt8(string[string.index(string.startIndex, offsetBy: 7)],
|
||||
string[string.index(string.startIndex, offsetBy: 8)]) else {
|
||||
return []
|
||||
}
|
||||
|
||||
return [Gfx.Color(r!, g!, b!, a!)]
|
||||
default: return []
|
||||
return [Gfx.Color(r, g, b, a)]
|
||||
|
||||
default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue