Skip to content

Commit

Permalink
refactor: simplify hexToRGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter committed Jul 21, 2024
1 parent 1c20ab1 commit 5ce3f4a
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions scripts/builders/palettes/json-to-clr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,13 @@ struct ColorProperties: Decodable {
typealias ColorList = [String: ColorProperties]

func hexToRGBA(_ color: String) -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
var hexColor = color
if hexColor.hasPrefix("#") {
hexColor.remove(at: hexColor.startIndex)
}
var hexColor = String(color.dropFirst()) + "ff"

switch hexColor.count {
case 3, 4:
hexColor = hexColor.reduce("") { $0 + String(repeating: String($1), count: 2) }
fallthrough
case 6, 8:
if hexColor.count == 6 {
hexColor += "ff"
}
let r = CGFloat(Int(hexColor.prefix(2), radix: 16) ?? 0) / 255
let g = CGFloat(Int(hexColor.dropFirst(2).prefix(2), radix: 16) ?? 0) / 255
let b = CGFloat(Int(hexColor.dropFirst(4).prefix(2), radix: 16) ?? 0) / 255
let a = CGFloat(Int(hexColor.dropFirst(6).prefix(2), radix: 16) ?? 0) / 255
return (r, g, b, a)
default:
return (0, 0, 0, 0)
}
let r = CGFloat(Int(hexColor.prefix(2), radix: 16) ?? 0) / 255
let g = CGFloat(Int(hexColor.dropFirst(2).prefix(2), radix: 16) ?? 0) / 255
let b = CGFloat(Int(hexColor.dropFirst(4).prefix(2), radix: 16) ?? 0) / 255
let a = CGFloat(Int(hexColor.dropFirst(6).prefix(2), radix: 16) ?? 0) / 255
return (r, g, b, a)
}

func convertJSONToCLR(inputFilePath: String, outputFilePath: String) {
Expand Down

0 comments on commit 5ce3f4a

Please sign in to comment.