Skip to content

Commit d8ec12f

Browse files
0Hoonihsw1920
andcommitted
feat/#176 :: hex code 기반 UIColor initializer 생성
Co-Authored-By: seuhong <[email protected]>
1 parent 0a6303b commit d8ec12f

File tree

1 file changed

+26
-0
lines changed
  • PhotoGether/PresentationLayer/DesignSystem/DesignSystem/Source

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import UIKit
2+
3+
public extension UIColor {
4+
convenience init(hex: String) {
5+
let hexString = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
6+
var int = UInt64()
7+
Scanner(string: hexString).scanHexInt64(&int)
8+
9+
let red, green, blue, alpha: UInt64
10+
switch hexString.count {
11+
case 6: // 6자리 (RGB)
12+
(red, green, blue, alpha) = ((int >> 16) & 0xFF, (int >> 8) & 0xFF, int & 0xFF, 0xFF)
13+
case 8: // 8자리 (RGBA)
14+
(red, green, blue, alpha) = ((int >> 24) & 0xFF, (int >> 16) & 0xFF, (int >> 8) & 0xFF, int & 0xFF)
15+
default:
16+
(red, green, blue, alpha) = (0, 0, 0, 0xFF) // 유효하지 않은 경우 기본값
17+
}
18+
19+
self.init(
20+
red: CGFloat(red) / 255.0,
21+
green: CGFloat(green) / 255.0,
22+
blue: CGFloat(blue) / 255.0,
23+
alpha: CGFloat(alpha) / 255.0
24+
)
25+
}
26+
}

0 commit comments

Comments
 (0)