Skip to content

Instantly share code, notes, and snippets.

@ha1f
Created Sep 25, 2017
Embed
What would you like to do?
//
// Rgba.swift
// ha1f-chat
//
// Created by ST20591 on 2017/07/19.
// Copyright © 2017年 ha1f. All rights reserved.
//
import UIKit
struct Rgba {
// MARK: Properties
let red: CGFloat
let green: CGFloat
let blue: CGFloat
let alpha: CGFloat
// MARK: Getter
var colorHex: UInt32 {
let iRed = UInt32(red * 255.0)
let iGreen = UInt32(green * 255.0)
let iBlue = UInt32(blue * 255.0)
return (iRed << 16) + (iGreen << 8) + iBlue
}
var uiColor: UIColor {
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
// MARK: Initializer
init(hex: UInt32, alpha: CGFloat = 1.0) {
self.red = CGFloat((hex & 0xFF0000) >> 16) / 255.0
self.green = CGFloat((hex & 0x00FF00) >> 8) / 255.0
self.blue = CGFloat(hex & 0x0000FF) / 255.0
self.alpha = alpha
}
init?(color: UIColor) {
var fRed: CGFloat = 0
var fGreen: CGFloat = 0
var fBlue: CGFloat = 0
var fAlpha: CGFloat = 0
guard color.getRed(&fRed, green: &fGreen, blue: &fBlue, alpha: &fAlpha) else {
return nil
}
self.red = fRed
self.green = fGreen
self.blue = fBlue
self.alpha = fAlpha
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment