UIColor+Extensions.swift 5.4 KB

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