HexColor.swift 688 B

123456789101112131415161718192021222324252627
  1. //
  2. // HexColor.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 19/08/21.
  6. //
  7. import Foundation
  8. import UIKit
  9. extension UIColor {
  10. convenience init(red: Int, green: Int, blue: Int) {
  11. assert(red >= 0 && red <= 255, "Invalid red component")
  12. assert(green >= 0 && green <= 255, "Invalid green component")
  13. assert(blue >= 0 && blue <= 255, "Invalid blue component")
  14. self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
  15. }
  16. convenience init(rgb: Int) {
  17. self.init(
  18. red: (rgb >> 16) & 0xFF,
  19. green: (rgb >> 8) & 0xFF,
  20. blue: rgb & 0xFF
  21. )
  22. }
  23. }