Bibi's DevLog ๐Ÿค“๐ŸŽ

UIColor extension ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS/Code Templates

UIColor extension

๋น„๋น„ bibi 2022. 5. 11. 15:02

init : rgb ์•ŒํŒŒ ๊ธฐ๋ณธ๊ฐ’์„ 1๋กœ ์ง€์ •ํ•˜๋Š” convenience init
customColor : ์ง€์ •๋œ ์ƒ‰์ƒ์„ colorLiteral๋กœ ํ‘œํ˜„

//
//  UIColorExtension.swift
//  starbuckst
//
//  Created by Bibi on 2022/05/11.
//

import UIKit

enum CustomColor {
    case white
}

extension UIColor {

    convenience init(red: Int, green: Int, blue: Int, a: Int = 1) {
        self.init(
            red: CGFloat(red) / 255.0,
            green: CGFloat(green) / 255.0,
            blue: CGFloat(blue) / 255.0,
            alpha: CGFloat(a) / 255.0
        )
    }

    static func customColor(_ name: CustomColor) -> UIColor {
        switch name {
        case .white: // return #colorLiteral()
            return #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        }
    }
}