Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import Foundation
  2. import AppKit
  3.  
  4. class IntensityView: NSView {
  5.  
  6. override func draw(_ rect: CGRect) {
  7. drawMultipleShapesWithAColorGradient()
  8. }
  9.  
  10. func drawMultipleShapesWithAColorGradient() {
  11.  
  12. let height: Double = 500
  13. let startingMargin = 3.5
  14.  
  15. guard let ctx = NSGraphicsContext.current else { return }
  16. let cgx = ctx.cgContext
  17. ctx.saveGraphicsState() /// Save out the state so that we dont clip everything outside of the path ctx.beginPath()
  18. cgx.move(to: CGPoint(x: 0, y: startingMargin))
  19. cgx.addLine(to: CGPoint(x: 25, y: startingMargin))
  20. cgx.addLine(to: CGPoint(x: 25, y: startingMargin + height))
  21. cgx.addLine(to: CGPoint(x: 0, y: startingMargin + height))
  22. cgx.closePath()
  23. cgx.clip() /// This is the clip we are referring to above.
  24.  
  25. let locations: [CGFloat] = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
  26. let colors: [CGFloat] = [
  27. 0.0, 0.0, 205/255, 1.0,
  28. 0.0, 75/255, 248/255, 1.0,
  29. 2/255, 209/255, 137/255, 1.0,
  30. 71/255, 250/255, 55/255, 1.0,
  31. 188/255, 255/255, 18/255, 1.0,
  32. 253/255, 252/255, 16/255, 1.0,
  33. 255/255, 219/255, 16/255, 1.0,
  34. 255/255, 137/255, 0/255, 1.0,
  35. 254/255, 61/255, 0/255, 1.0,
  36. 241/255, 0/255, 0/255, 1.0,
  37. 178/255, 11/255, 11/255, 1.0
  38. ]
  39.  
  40. let colorSpace = CGColorSpaceCreateDeviceRGB()
  41. guard let gradient = CGGradient(colorSpace: colorSpace, colorComponents: colors, locations: locations, count: 11) else {
  42. return
  43. }
  44. cgx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: 0.0), end: CGPoint(x: 0, y: height + startingMargin), options: CGGradientDrawingOptions(rawValue: 0))
  45.  
  46. ctx.restoreGraphicsState() /// Restore the previous state. This will remove any paths, clipping, gradients, etc.
  47.  
  48. let paragraphStyle = NSMutableParagraphStyle()
  49. paragraphStyle.alignment = .center
  50.  
  51. let attributes = [
  52. NSAttributedString.Key.paragraphStyle: paragraphStyle,
  53. NSAttributedString.Key.font: NSFont.systemFont(ofSize: 12.0),
  54. NSAttributedString.Key.foregroundColor: NSColor.white
  55. ]
  56.  
  57. (0...10).forEach { index in
  58. cgx.move(to: CGPoint(x: 0, y: startingMargin + Double(index) * (height) / 10))
  59. cgx.addLine(to: CGPoint(x: 38, y: startingMargin + Double(index) * (height) / 10))
  60. cgx.setLineWidth(1)
  61. cgx.setStrokeColor(#colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1))
  62. cgx.strokePath()
  63.  
  64. let labelText = "\(index - 3)"
  65. let attributedString = NSAttributedString(string: labelText, attributes: attributes)
  66.  
  67. let stringRect = CGRect(x: 38, y: 0.0 + Double(index) * height / 10, width: 20, height: 12)
  68. attributedString.draw(in: stringRect)
  69. }
  70.  
  71. ctx.cgContext.setFillColor(.clear)
  72. ctx.cgContext.setStrokeColor(.white)
  73. ctx.cgContext.setLineWidth(1)
  74.  
  75. let rectangle = CGRect(x: 0, y: startingMargin, width: 26, height: height)
  76. ctx.cgContext.addRect(rectangle)
  77. ctx.cgContext.drawPath(using: .fillStroke)
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement