UIColor+Extension.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // UIColor+Extension.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/02/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. extension UIColor {
  26. var inverted: UIColor {
  27. var r: CGFloat = 0.0, g: CGFloat = 0.0, b: CGFloat = 0.0, a: CGFloat = 0.0
  28. self.getRed(&r, green: &g, blue: &b, alpha: &a)
  29. return UIColor(red: (1 - r), green: (1 - g), blue: (1 - b), alpha: a) // Assuming you want the same alpha value.
  30. }
  31. var hexString: String {
  32. let cgColorInRGB = cgColor.converted(to: CGColorSpace(name: CGColorSpace.sRGB)!, intent: .defaultIntent, options: nil)!
  33. let colorRef = cgColorInRGB.components
  34. let r = colorRef?[0] ?? 0
  35. let g = colorRef?[1] ?? 0
  36. let b = ((colorRef?.count ?? 0) > 2 ? colorRef?[2] : g) ?? 0
  37. let a = cgColor.alpha
  38. var color = String(
  39. format: "#%02lX%02lX%02lX",
  40. lroundf(Float(r * 255)),
  41. lroundf(Float(g * 255)),
  42. lroundf(Float(b * 255))
  43. )
  44. if a < 1 {
  45. color += String(format: "%02lX", lroundf(Float(a * 255)))
  46. }
  47. return color
  48. }
  49. @objc convenience init?(hex: String) {
  50. let r, g, b, a: CGFloat
  51. if hex.hasPrefix("#") {
  52. let start = hex.index(hex.startIndex, offsetBy: 1)
  53. let hexColor = String(hex[start...])
  54. let scanner = Scanner(string: hexColor)
  55. var hexNumber: UInt64 = 0
  56. if hexColor.count == 6 && scanner.scanHexInt64(&hexNumber) {
  57. r = CGFloat((hexNumber & 0xff0000) >> 16) / 255.0
  58. g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255.0
  59. b = CGFloat(hexNumber & 0x0000ff) / 255.0
  60. self.init(red: r, green: g, blue: b, alpha: 1)
  61. return
  62. } else if hexColor.count == 7 && scanner.scanHexInt64(&hexNumber) {
  63. r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
  64. g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
  65. b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
  66. a = CGFloat(hexNumber & 0x000000ff) / 255
  67. self.init(red: r, green: g, blue: b, alpha: a)
  68. return
  69. }
  70. }
  71. return nil
  72. }
  73. @objc func lighter(by percentage: CGFloat = 30.0) -> UIColor? {
  74. return self.adjust(by: abs(percentage) )
  75. }
  76. @objc func darker(by percentage: CGFloat = 30.0) -> UIColor? {
  77. return self.adjust(by: -1 * abs(percentage) )
  78. }
  79. func adjust(by percentage: CGFloat = 30.0) -> UIColor? {
  80. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  81. if self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
  82. return UIColor(red: min(red + percentage / 100, 1.0),
  83. green: min(green + percentage / 100, 1.0),
  84. blue: min(blue + percentage / 100, 1.0),
  85. alpha: alpha)
  86. } else {
  87. return nil
  88. }
  89. }
  90. @objc func isTooLight() -> Bool {
  91. var white: CGFloat = 0.0
  92. self.getWhite(&white, alpha: nil)
  93. if white == 1 { return true }
  94. guard let components = cgColor.components, components.count > 2 else {return false}
  95. let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
  96. return (brightness > 0.90)
  97. }
  98. @objc func isTooDark() -> Bool {
  99. var white: CGFloat = 0.0
  100. self.getWhite(&white, alpha: nil)
  101. if white == 0 { return true }
  102. guard let components = cgColor.components, components.count > 2 else {return false}
  103. let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
  104. return (brightness < 0.10)
  105. }
  106. func isLight(threshold: Float = 0.7) -> Bool {
  107. let originalCGColor = self.cgColor
  108. // Now we need to convert it to the RGB colorspace. UIColor.white / UIColor.black are greyscale and not RGB.
  109. // If you don't do this then you will crash when accessing components index 2 below when evaluating greyscale colors.
  110. let RGBCGColor = originalCGColor.converted(to: CGColorSpaceCreateDeviceRGB(), intent: .defaultIntent, options: nil)
  111. guard let components = RGBCGColor?.components else { return false }
  112. guard components.count >= 3 else { return false }
  113. let brightness = Float(((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000)
  114. return (brightness > threshold)
  115. }
  116. func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
  117. return UIGraphicsImageRenderer(size: size).image { rendererContext in
  118. self.setFill()
  119. rendererContext.fill(CGRect(origin: .zero, size: size))
  120. }
  121. }
  122. }