Skip to content

Commit

Permalink
Add functionality to set the picker's color
Browse files Browse the repository at this point in the history
  • Loading branch information
joncardasis committed Sep 9, 2016
1 parent 1960f15 commit fc5efa3
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 16 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,38 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ChromaColorPicker/ChromaShadeSlider.swift"
timestampString = "495133389.679175"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "58"
endingLineNumber = "58"
landmarkName = "ChromaShadeSlider"
landmarkType = "3">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ChromaColorPicker/ChromaShadeSlider.swift"
timestampString = "495139028.639954"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "133"
endingLineNumber = "133"
landmarkName = "updateGradientTrack(for:)"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
6 changes: 3 additions & 3 deletions ChromaColorPicker-Demo/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -15,7 +16,6 @@
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
Expand Down
3 changes: 2 additions & 1 deletion ChromaColorPicker-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ViewController: UIViewController {
colorPicker.padding = 10
colorPicker.stroke = 3 //stroke of the rainbow circle
colorPicker.currentAngle = Float(M_PI)

colorPicker.hexLabel.textColor = UIColor.whiteColor()

//Don't want an element like the shade slider? Just hide it:
Expand All @@ -39,7 +40,7 @@ class ViewController: UIViewController {

extension ViewController: ChromaColorPickerDelegate{
func colorPickerDidChooseColor(colorPicker: ChromaColorPicker, color: UIColor) {

//Set color for the display view
colorDisplayView.backgroundColor = color

Expand Down
4 changes: 2 additions & 2 deletions ChromaColorPicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ChromaColorPicker"
s.version = "1.0.2"
s.version = "1.1"
s.summary = "An intuitive iOS color picker built in Swift."

s.description = <<-DESC
Expand All @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = "Jonathan Cardasis"
s.platform = :ios, "8.0"
s.source = { :git => "https://github.com/joncardasis/ChromaColorPicker.git", :tag => "1.0.2" }
s.source = { :git => "https://github.com/joncardasis/ChromaColorPicker.git", :tag => "1.0.3" }
s.source_files = "ChromaColorPicker", "ChromaColorPicker/*.swift"

end
42 changes: 39 additions & 3 deletions ChromaColorPicker/ChromaColorPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class ChromaColorPicker: UIControl {
public var handleLine: CAShapeLayer!
public var addButton: ChromaAddButton!

public var currentColor = UIColor()
public private(set) var currentColor = UIColor.redColor()
public var delegate: ChromaColorPickerDelegate?
public var currentAngle: Float = 0
private (set) var radius: CGFloat = 0
public private(set) var radius: CGFloat = 0
public var stroke: CGFloat = 1
public var padding: CGFloat = 15
public var handleSize: CGSize{
Expand Down Expand Up @@ -115,6 +115,36 @@ public class ChromaColorPicker: UIControl {
self.updateHexLabel() //update for hex value
}

public func adjustToColor(color: UIColor){
/* Apply saturation and brightness from previous color to current one */
var saturation: CGFloat = 0.0
var brightness: CGFloat = 0.0
//currentColor.getHue(nil, saturation: &saturation, brightness: &brightness, alpha: nil)

var hue: CGFloat = 0.0
var alpha: CGFloat = 0.0
color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
let newColor = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)

/* Update the angle and currentColor */
currentAngle = angleForColor(newColor)
currentColor = newColor

/* Set the slider value for the new color and update addButton */
shadeSlider.primaryColor = UIColor(hue: hue, saturation: 1, brightness: 1, alpha: 1) //Set a color recognzied on the color wheel
if brightness < 1.0 { //currentValue is on the left side of the slider
shadeSlider.currentValue = brightness-1
}else{
shadeSlider.currentValue = -(saturation-1)
}
shadeSlider.updateHandleLocation() //update the handle location now that the value is set
addButton.color = shadeSlider.currentColor

/* Will layout based on new angle */
self.layoutHandle()
self.layoutHandleLine()
self.updateHexLabel()
}

//MARK: - Handle Touches
override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?){
Expand Down Expand Up @@ -291,7 +321,7 @@ public class ChromaColorPicker: UIControl {

/*
Updates the line view's position for the current angle
Pre: dependant on addButtons position
Pre: dependant on addButtons position & current angle
*/
func layoutHandleLine(){
let linePath = UIBezierPath()
Expand Down Expand Up @@ -359,6 +389,12 @@ public class ChromaColorPicker: UIControl {
return UIColor(hue: CGFloat(Double(angle)/(2*M_PI)), saturation: 1, brightness: 1, alpha: 1)
}

private func angleForColor(color: UIColor) -> Float {
var hue: CGFloat = 0
color.getHue(&hue, saturation: nil, brightness: nil, alpha: nil)
return Float(hue * CGFloat(2*M_PI))
}

/* Returns a position centered on the wheel for a given angle */
private func positionOnWheelFromAngle(angle: Float) -> CGPoint{
let buffer = padding + stroke/2
Expand Down
19 changes: 15 additions & 4 deletions ChromaColorPicker/ChromaShadeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class ChromaShadeSlider: UIControl {
handleView.userInteractionEnabled = false //disable interaction for touch events
self.layer.addSublayer(handleView.layer)

let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(resetHandleToCenter))
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleTapRecognized))
doubleTapGesture.numberOfTapsRequired = 2
self.addGestureRecognizer(doubleTapGesture)

Expand All @@ -113,7 +113,7 @@ public class ChromaShadeSlider: UIControl {
trackLayer.cornerRadius = trackLayer.bounds.height/2

self.updateGradientTrack(for: primaryColor)
handleCenterX = (currentValue+1)/2 * (bounds.width - handleView.bounds.width) + handleView.bounds.width/2 //Update where the handles center should be
self.updateHandleLocation()
self.layoutHandleFrame()
}

Expand All @@ -138,6 +138,12 @@ public class ChromaShadeSlider: UIControl {
trackLayer.gradient.colors = [color.darkerColor(0.65).CGColor, color.CGColor, color.lighterColor(0.65).CGColor]
}

//Updates handeles location based on currentValue
public func updateHandleLocation(){
handleCenterX = (currentValue+1)/2 * (bounds.width - handleView.bounds.width) + handleView.bounds.width/2
handleView.color = currentColor
self.layoutHandleFrame()
}

override public func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
let location = touch.locationInView(self)
Expand Down Expand Up @@ -172,13 +178,18 @@ public class ChromaShadeSlider: UIControl {
return true
}

public func resetHandleToCenter(recognizer: UITapGestureRecognizer){
func doubleTapRecognized(recognizer: UITapGestureRecognizer){
let location = recognizer.locationInView(self)
guard handleView.frame.contains(location) else {
return
}
//Tap is on handle

resetHandleToCenter()
}

public func resetHandleToCenter(){

//tap is on handle
handleCenterX = self.bounds.width/2
self.layoutHandleFrame()
handleView.color = primaryColor
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![Supported Version](https://img.shields.io/badge/Swift-2.2-yellow.svg)
![Platform](https://img.shields.io/badge/platform-iOS-lightgray.svg)
![License](https://img.shields.io/badge/license-MIT-blue.svg)
![CocoaPods](https://img.shields.io/badge/CocoaPods-1.0-green.svg)
![CocoaPods](https://img.shields.io/badge/CocoaPods-1.1-green.svg)

An intuitive iOS color picker built in Swift.

Expand All @@ -20,6 +20,7 @@ Add all files from the ChromaColorPicker folder to your project.
## Examples
```Swift
let neatColorPicker = ChromaColorPicker(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
neatColorPicker.delegate = self //ChromaColorPickerDelegate
neatColorPicker.padding = 5
neatColorPicker.stroke = 3
neatColorPicker.hexLabel.textColor = UIColor.whiteColor()
Expand All @@ -28,7 +29,7 @@ self.view.addSubview(neatColorPicker)
```
<img src="../assets/demo.gif?raw=true" width="225">

If the ChromaColorPicker or any of its properties are later updated, the `layout()` function should be called to update the view.
If the ChromaColorPicker or any of its properties are later updated after being added to a view, the `layout()` function should be called to update the view.

```Swift
let neatColorPicker = ChromaColorPicker(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
Expand All @@ -40,6 +41,15 @@ neatColorPicker.hexLabel.hidden = true
neatColorPicker.layout()
```

You can also set the color of the picker anytime by using the `adjustToColor(color:)` function.

```Swift
let neatColorPicker = ChromaColorPicker(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
...
neatColorPicker.adjustToColor(UIColor.greenColor())
...
```

## Customization
<img src="../assets/Design_Breakdown.png?raw=true" width="350">

Expand All @@ -50,10 +60,15 @@ neatColorPicker.layout()
| delegate | ChromaColorPickerDelegate |
| padding | The padding on each side of the view (default=10) |
| stroke | The stroke of the rainbow track (deafult=1) |
| currentColor | The currently set color by the control. It is displayed in the add button. |
| currentColor | The currently set color by the control. It is displayed in the add button. Use `adjustToColor(color:)` to update the color.|
| currentAngle | The angle which the handle is currently sitting at. Can be changed and the view can be re-drawn using `layout()` to show the change.
| handleSize | Returns the size of the handle. |

### Functions
| Function | Description |
| :-------------: |:-------------|
|layout() | Layout the entire picker and all its subviews.|
|adjustToColor(color: UIColor) | Updates the picker to a specific color.|

### Sub-Components
Sub-Components can be hidden and customized to the preferred liking.
Expand Down

0 comments on commit fc5efa3

Please sign in to comment.