UIColor+Extensions.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // UIColor+adjust.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 UIKit
  24. extension UIColor {
  25. var hexString: String {
  26. let cgColorInRGB = cgColor.converted(to: CGColorSpace(name: CGColorSpace.sRGB)!, intent: .defaultIntent, options: nil)!
  27. let colorRef = cgColorInRGB.components
  28. let r = colorRef?[0] ?? 0
  29. let g = colorRef?[1] ?? 0
  30. let b = ((colorRef?.count ?? 0) > 2 ? colorRef?[2] : g) ?? 0
  31. let a = cgColor.alpha
  32. var color = String(
  33. format: "#%02lX%02lX%02lX",
  34. lroundf(Float(r * 255)),
  35. lroundf(Float(g * 255)),
  36. lroundf(Float(b * 255))
  37. )
  38. if a < 1 {
  39. color += String(format: "%02lX", lroundf(Float(a * 255)))
  40. }
  41. return color
  42. }
  43. @objc convenience init?(hex: String) {
  44. let r, g, b, a: CGFloat
  45. if hex.hasPrefix("#") {
  46. let start = hex.index(hex.startIndex, offsetBy: 1)
  47. let hexColor = String(hex[start...])
  48. let scanner = Scanner(string: hexColor)
  49. var hexNumber: UInt64 = 0
  50. if hexColor.count == 6 && scanner.scanHexInt64(&hexNumber) {
  51. r = CGFloat((hexNumber & 0xff0000) >> 16) / 255.0
  52. g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255.0
  53. b = CGFloat(hexNumber & 0x0000ff) / 255.0
  54. self.init(red: r, green: g, blue: b, alpha: 1)
  55. return
  56. } else if hexColor.count == 7 && scanner.scanHexInt64(&hexNumber) {
  57. r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
  58. g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
  59. b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
  60. a = CGFloat(hexNumber & 0x000000ff) / 255
  61. self.init(red: r, green: g, blue: b, alpha: a)
  62. return
  63. }
  64. }
  65. return nil
  66. }
  67. @objc func lighter(by percentage: CGFloat = 30.0) -> UIColor? {
  68. return self.adjust(by: abs(percentage) )
  69. }
  70. @objc func darker(by percentage: CGFloat = 30.0) -> UIColor? {
  71. return self.adjust(by: -1 * abs(percentage) )
  72. }
  73. func adjust(by percentage: CGFloat = 30.0) -> UIColor? {
  74. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  75. if self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
  76. return UIColor(red: min(red + percentage/100, 1.0),
  77. green: min(green + percentage/100, 1.0),
  78. blue: min(blue + percentage/100, 1.0),
  79. alpha: alpha)
  80. } else {
  81. return nil
  82. }
  83. }
  84. @objc func isTooLight() -> Bool {
  85. var white: CGFloat = 0.0
  86. self.getWhite(&white, alpha: nil)
  87. if white == 1 { return true }
  88. guard let components = cgColor.components, components.count > 2 else {return false}
  89. let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
  90. return (brightness > 0.95)
  91. }
  92. @objc func isTooDark() -> Bool {
  93. var white: CGFloat = 0.0
  94. self.getWhite(&white, alpha: nil)
  95. if white == 0 { return true }
  96. guard let components = cgColor.components, components.count > 2 else {return false}
  97. let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
  98. return (brightness < 0.05)
  99. }
  100. func isLight(threshold: Float = 0.7) -> Bool {
  101. let originalCGColor = self.cgColor
  102. // Now we need to convert it to the RGB colorspace. UIColor.white / UIColor.black are greyscale and not RGB.
  103. // If you don't do this then you will crash when accessing components index 2 below when evaluating greyscale colors.
  104. let RGBCGColor = originalCGColor.converted(to: CGColorSpaceCreateDeviceRGB(), intent: .defaultIntent, options: nil)
  105. guard let components = RGBCGColor?.components else { return false }
  106. guard components.count >= 3 else { return false }
  107. let brightness = Float(((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000)
  108. return (brightness > threshold)
  109. }
  110. func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
  111. return UIGraphicsImageRenderer(size: size).image { rendererContext in
  112. self.setFill()
  113. rendererContext.fill(CGRect(origin: .zero, size: size))
  114. }
  115. }
  116. }