UIColor+Extensions.swift 4.4 KB

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