diff --git a/Example/ViewController.swift b/Example/ViewController.swift index b93948d..4087243 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -23,6 +23,8 @@ class ViewController: UIViewController { temp.frame = CGRect(x: 30, y: 70, width: 320, height: 32) view.addSubview(temp) + temp.trackColor = UIColor.red + colorPicker.translatesAutoresizingMaskIntoConstraints = false view.addSubview(colorPicker) diff --git a/Source/BrightnessSlider.swift b/Source/BrightnessSlider.swift index 4057d60..5a7a0c4 100644 --- a/Source/BrightnessSlider.swift +++ b/Source/BrightnessSlider.swift @@ -15,6 +15,11 @@ public class BrightnessSlider: UIControl, ChromaControlStylable { didSet { updateControl(to: currentValue) } } + /// The base color the slider on the track. + public var trackColor: UIColor = .white { + didSet { updateTrackColor(to: trackColor) } + } + /// The value of the color the handle is currently displaying. public var currentColor: UIColor { return sliderHandleView.handleColor @@ -54,12 +59,6 @@ public class BrightnessSlider: UIControl, ChromaControlStylable { updateShadowIfNeeded() } - /// Sets the tracking color of the slider and updates the gradient view. - public func updateTrackingColor(to color: UIColor, value: CGFloat) { - updateTrackViewGradient(for: color) - currentValue = value - } - // MARK: - Control public override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { @@ -99,7 +98,7 @@ public class BrightnessSlider: UIControl, ChromaControlStylable { backgroundColor = .clear setupSliderTrackView() setupSliderHandleView() - updateControl(to: currentValue) + updateTrackColor(to: trackColor) } internal func setupSliderTrackView() { @@ -130,15 +129,11 @@ public class BrightnessSlider: UIControl, ChromaControlStylable { } } -// internal func updateControl(to color: UIColor) { -// updateTrackViewGradient(for: color) -// } - internal func updateControl(to value: CGFloat) { let brightness = 1 - max(0, min(1, value)) var hue: CGFloat = 0 var saturation: CGFloat = 0 - currentColor.getHue(&hue, saturation: &saturation, brightness: nil, alpha: nil) + trackColor.getHue(&hue, saturation: &saturation, brightness: nil, alpha: nil) let newColor = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0) @@ -150,6 +145,18 @@ public class BrightnessSlider: UIControl, ChromaControlStylable { moveHandle(to: value) } + internal func updateTrackColor(to color: UIColor) { + var hue: CGFloat = 0 + var saturation: CGFloat = 0 + var brightness: CGFloat = 0 + color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil) + + let colorWithMaxBrightness = UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1) + + updateTrackViewGradient(for: colorWithMaxBrightness) + currentValue = 1 - brightness + } + internal func updateTrackViewGradient(for color: UIColor) { sliderTrackView.gradientValues = (color, .black) } @@ -161,6 +168,4 @@ public class BrightnessSlider: UIControl, ChromaControlStylable { sliderHandleView.frame = CGRect(origin: CGPoint(x: xPos - (size.width / 2), y: 0), size: size) } - - }