Skip to content

Instantly share code, notes, and snippets.

@Enchan1207
Created May 3, 2021 07:16
Show Gist options
  • Save Enchan1207/81f70545cdb399501960116267c1cadc to your computer and use it in GitHub Desktop.
Save Enchan1207/81f70545cdb399501960116267c1cadc to your computer and use it in GitHub Desktop.
CGColorからカラーコードを生成
extension CGColor {
// convert to color-code string
public var hexCodeRepresentation: String? {
get{
guard let components = self.components else {return nil}
let colorCode = components.map({$0 * 255.0})
.compactMap({UInt8($0)})
.map({String(format: "%02X", $0)})
.joined()
return "#\(colorCode)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment