Swift UIColor→JSON→UIColor
UIColor→JSON
以前の記事
UIColor情報をJSONで保存したい為、RGB情報にする
JSON→UIColor
Exampleソース
class ColorRGBA: Codable { var red: Float = 0.0 var green: Float = 0.0 var blue: Float = 0.0 var alpha: Float = 0.0 init() { } init(color: UIColor) { if let colorcomponents = color.cgColor.components, colorcomponents.count == 4 { self.red = Float(colorcomponents[0]) self.green = Float(colorcomponents[1]) self.blue = Float(colorcomponents[2]) self.alpha = Float(colorcomponents[3]) } else { print(#function + ":no RGB") } } func toUIColor() -> UIColor { UIColor(red: CGFloat(self.red), green: CGFloat(self.green), blue: CGFloat(self.blue), alpha: CGFloat(self.alpha)) } }