Help us understand the problem. What is going on with this article?

UIColorとIntの相互変換

n番煎じですが、Intとの相互変換についての日本語の記事が見当たらなかったため投稿します

使い方

let color = UIColor(hex: 0xff0000) // red
color.hex // 0xff0000
extension UIColor {
    convenience init(hex: Int) {
        let red = (hex & 0xff0000) >> 16
        let green = (hex & 0x00ff00) >> 8
        let blue = hex & 0x0000ff

        self.init(
            red: CGFloat(red) / 255,
            green: CGFloat(green) / 255,
            blue: CGFloat(blue) / 255,
            alpha: 1
        )
    }

    var hex: Int {
        var red = CGFloat(0)
        var green = CGFloat(0)
        var blue = CGFloat(0)
        getRed(&red, green: &green, blue: &blue, alpha: nil)

        return Int(red * 255) << 16
            + Int(green * 255) << 8
            + Int(blue * 255)
    }
}

s2mr
iOS engineer.
iwate-pu
岩手県滝沢市にある公立大学です。Qiitaではソフトウェア情報学部生や出身の人が多いです。
https://www.iwate-pu.ac.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away