Utils.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #if os(iOS) || os(tvOS) || os(watchOS)
  27. import UIKit
  28. #elseif os(OSX)
  29. import AppKit
  30. #endif
  31. /**
  32. Clips the values in an interval.
  33. Given an interval, values outside the interval are clipped to the interval
  34. edges. For example, if an interval of [0, 1] is specified, values smaller than
  35. 0 become 0, and values larger than 1 become 1.
  36. - Parameter v: The value to clipped.
  37. - Parameter minimum: The minimum edge value.
  38. - Parameter maximum: The maximum edgevalue.
  39. */
  40. internal func clip<T: Comparable>(_ v: T, _ minimum: T, _ maximum: T) -> T {
  41. return max(min(v, maximum), minimum)
  42. }
  43. /**
  44. Returns the absolute value of the modulo operation.
  45. - Parameter x: The value to compute.
  46. - Parameter m: The modulo.
  47. */
  48. internal func moda(_ x: CGFloat, m: CGFloat) -> CGFloat {
  49. return (x.truncatingRemainder(dividingBy: m) + m).truncatingRemainder(dividingBy: m)
  50. }
  51. /**
  52. Rounds the given float to a given decimal precision.
  53. - Parameter x: The value to round.
  54. - Parameter m: The precision. Default to 10000.
  55. */
  56. internal func roundDecimal(_ x: CGFloat, precision: CGFloat = 10000.0) -> CGFloat {
  57. return CGFloat(Int(round(x * precision))) / precision
  58. }
  59. internal func roundToHex(_ x: CGFloat) -> UInt32 {
  60. guard x > 0 else { return 0 }
  61. let rounded: CGFloat = round(x * 255.0)
  62. return UInt32(rounded)
  63. }
  64. /**
  65. Defines the mode (i.e color space) used for grayscaling.
  66. [More info](https://en.wikipedia.org/wiki/Lightness#Lightness_and_human_perception)
  67. */
  68. public enum GrayscalingMode {
  69. /// XYZ luminance
  70. case luminance
  71. /// HSL lightness
  72. case lightness
  73. /// RGB average
  74. case average
  75. /// HSV value
  76. case value
  77. }