SwiftUIColor.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * DynamicColor
  3. *
  4. * Copyright 2015-present Yannick Loriot.
  5. * http://yannickloriot.com
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. */
  26. import SwiftUI
  27. @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
  28. public extension Color {
  29. // MARK: - Manipulating Hexa-decimal Values and Strings
  30. /**
  31. Creates a color from an hex string (e.g. "#3498db"). The RGBA string are also supported (e.g. "#3498dbff").
  32. If the given hex string is invalid the initialiser will create a black color.
  33. - parameter hexString: A hexa-decimal color string representation.
  34. */
  35. init(hexString: String) {
  36. let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
  37. let scanner = Scanner(string: hexString)
  38. scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
  39. var color: UInt64 = 0
  40. if scanner.scanHexInt64(&color) {
  41. self.init(hex: color, useOpacity: hexString.count > 7)
  42. }
  43. else {
  44. self.init(hex: 0x000000)
  45. }
  46. }
  47. /**
  48. Creates a color from an hex integer (e.g. 0x3498db).
  49. - parameter hex: A hexa-decimal UInt64 that represents a color.
  50. - parameter opacityChannel: If true the given hex-decimal UInt64 includes the opacity channel (e.g. 0xFF0000FF).
  51. */
  52. init(hex: UInt64, useOpacity opacityChannel: Bool = false) {
  53. let mask = UInt64(0xFF)
  54. let cappedHex = !opacityChannel && hex > 0xffffff ? 0xffffff : hex
  55. let r = cappedHex >> (opacityChannel ? 24 : 16) & mask
  56. let g = cappedHex >> (opacityChannel ? 16 : 8) & mask
  57. let b = cappedHex >> (opacityChannel ? 8 : 0) & mask
  58. let o = opacityChannel ? cappedHex & mask : 255
  59. let red = Double(r) / 255.0
  60. let green = Double(g) / 255.0
  61. let blue = Double(b) / 255.0
  62. let opacity = Double(o) / 255.0
  63. self.init(red: red, green: green, blue: blue, opacity: opacity)
  64. }
  65. }