Skip to content

Commit fd2b417

Browse files
authored
Merge pull request #12 from StephanPartzsch/bugfix/hex-string-with-black
Fixes the conversion from UIColor.black to hexString.
2 parents 69b44cd + 63ae630 commit fd2b417

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Sources/MarkdownKit/AttributedString/Color.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@
2525
extension UIColor {
2626

2727
public var hexString: String {
28-
guard let components = self.cgColor.components, components.count >= 3 else {
28+
guard let components = self.cgColor.components else {
2929
return "#FFFFFF"
3030
}
31-
let red = Int(round(components[0] * 0xff))
32-
let green = Int(round(components[1] * 0xff))
33-
let blue = Int(round(components[2] * 0xff))
31+
var red = 0
32+
var green = 0
33+
var blue = 0
34+
35+
if components.count >= 3 {
36+
red = Int(round(components[0] * 0xff))
37+
green = Int(round(components[1] * 0xff))
38+
blue = Int(round(components[2] * 0xff))
39+
} else if components.count == 2 {
40+
red = Int(round(components[0] * 0xff))
41+
green = Int(round(components[0] * 0xff))
42+
blue = Int(round(components[0] * 0xff))
43+
}
3444
return String(format: "#%02X%02X%02X", red, green, blue)
3545
}
3646
}

0 commit comments

Comments
 (0)