Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions Source/BrightnessView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import UIKit

protocol BrightnessViewDelegate: class {
func brightnessSelected(_ brightness: CGFloat)
func brightnessSelected(_ brightness: CGFloat)
}

class BrightnessView: UIView {

weak var delegate: BrightnessViewDelegate?

var colorLayer: CAGradientLayer!

var point: CGPoint!
Expand All @@ -40,8 +40,8 @@ class BrightnessView: UIView {
}
colorLayer = CAGradientLayer()
colorLayer.colors = [
UIColor.black.cgColor,
UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1).cgColor
UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1).cgColor,
UIColor.black.cgColor
]
colorLayer.locations = [0.0, 1.0]
colorLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
Expand Down Expand Up @@ -105,7 +105,7 @@ class BrightnessView: UIView {

func getBrightnessFromPoint() -> CGFloat {
// Get the brightness value for a given point
return point.x/self.frame.width
return 1-point.x/self.frame.width
}

func getPointFromColor(_ color: UIColor) -> CGPoint {
Expand All @@ -115,8 +115,7 @@ class BrightnessView: UIView {
if (!ok) {
print("SwiftHSVColorPicker: exception <The color provided to SwiftHSVColorPicker is not convertible to HSV>")
}

return CGPoint(x: brightness * frame.width, y: frame.height / 2)
return CGPoint(x: (1 - brightness) * frame.width, y: frame.height / 2)
}

func setViewColor(_ color: UIColor!) {
Expand All @@ -127,8 +126,8 @@ class BrightnessView: UIView {
print("SwiftHSVColorPicker: exception <The color provided to SwiftHSVColorPicker is not convertible to HSV>")
}
colorLayer.colors = [
UIColor.black.cgColor,
UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1).cgColor
UIColor(hue: hue, saturation: saturation, brightness: 1, alpha: 1).cgColor,
UIColor.black.cgColor
]
}
}
52 changes: 35 additions & 17 deletions Source/SwiftHSVColorPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@

import UIKit

public protocol SwiftHSVColorPickerDelegate: class {
func swiftHSVColorPicker(_ color: UIColor)
}

open class SwiftHSVColorPicker: UIView, ColorWheelDelegate, BrightnessViewDelegate {
var colorWheel: ColorWheel!
var brightnessView: BrightnessView!
var selectedColorView: SelectedColorView!

var brightnessView: BrightnessView?
var selectedColorView: SelectedColorView?

open weak var delegate: SwiftHSVColorPickerDelegate?
open var color: UIColor!
open var isSelectedColorView = true
open var isBrightnessView = true

var hue: CGFloat = 1.0
var saturation: CGFloat = 1.0
var brightness: CGFloat = 1.0
Expand Down Expand Up @@ -55,36 +63,46 @@ open class SwiftHSVColorPicker: UIView, ColorWheelDelegate, BrightnessViewDelega
// let the all the subviews stay in the middle of universe horizontally
let centeredX = (self.bounds.width - colorWheelSize) / 2.0

// Init SelectedColorView subview
selectedColorView = SelectedColorView(frame: CGRect(x: centeredX, y:0, width: colorWheelSize, height: selectedColorViewHeight), color: self.color)
// Add selectedColorView as a subview of this view
self.addSubview(selectedColorView)
if self.isSelectedColorView{
// Init SelectedColorView subview
let selectedColorView = SelectedColorView(frame: CGRect(x: centeredX, y:0, width: colorWheelSize, height: selectedColorViewHeight), color: self.color)
// Add selectedColorView as a subview of this view
self.addSubview(selectedColorView)
self.selectedColorView = selectedColorView
}

// Init new ColorWheel subview
colorWheel = ColorWheel(frame: CGRect(x: centeredX, y: selectedColorView.frame.maxY, width: colorWheelSize, height: colorWheelSize), color: self.color)
colorWheel = ColorWheel(frame: CGRect(x: centeredX, y: selectedColorView?.frame.maxY ?? 0, width: colorWheelSize, height: colorWheelSize), color: self.color)
colorWheel.delegate = self
// Add colorWheel as a subview of this view
self.addSubview(colorWheel)

// Init new BrightnessView subview
brightnessView = BrightnessView(frame: CGRect(x: centeredX, y: colorWheel.frame.maxY, width: colorWheelSize, height: brightnessViewHeight), color: self.color)
brightnessView.delegate = self
// Add brightnessView as a subview of this view
self.addSubview(brightnessView)
if self.isBrightnessView{
// Init new BrightnessView subview
let brightnessView = BrightnessView(frame: CGRect(x: centeredX, y: colorWheel.frame.maxY, width: colorWheelSize, height: brightnessViewHeight), color: self.color)
brightnessView.delegate = self
// Add brightnessView as a subview of this view
self.addSubview(brightnessView)
self.brightnessView = brightnessView
}

self.delegate?.swiftHSVColorPicker(self.color)
}

func hueAndSaturationSelected(_ hue: CGFloat, saturation: CGFloat) {
self.hue = hue
self.saturation = saturation
self.color = UIColor(hue: self.hue, saturation: self.saturation, brightness: self.brightness, alpha: 1.0)
brightnessView.setViewColor(self.color)
selectedColorView.setViewColor(self.color)
brightnessView?.setViewColor(self.color)
selectedColorView?.setViewColor(self.color)
self.delegate?.swiftHSVColorPicker(self.color)
}

func brightnessSelected(_ brightness: CGFloat) {
self.brightness = brightness
self.color = UIColor(hue: self.hue, saturation: self.saturation, brightness: self.brightness, alpha: 1.0)
colorWheel.setViewBrightness(brightness)
selectedColorView.setViewColor(self.color)
colorWheel?.setViewBrightness(brightness)
selectedColorView?.setViewColor(self.color)
self.delegate?.swiftHSVColorPicker(self.color)
}
}
4 changes: 2 additions & 2 deletions SwiftHSVColorPicker.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = 'SwiftHSVColorPicker'
s.version = '1.1.0'
s.version = '1.1.2'
s.summary = 'Swift HSV Color Picker'
s.description = 'A HSV Color Picker for iOS including a color wheel, brightness slider and a view for the selected color.'
s.homepage = 'https://github.com/johankasperi/SwiftHSVColorPicker'
s.license = 'MIT'
s.author = { 'Johan Kasperi' => 'johan@kasperi.se' }
s.social_media_url = 'https://twitter.com/johankasperi'
s.platform = :ios, '8.0'
s.source = { :git => 'https://github.com/johankasperi/SwiftHSVColorPicker.git', :tag => s.version.to_s }
s.source = { :git => 'https://github.com/pikachu987/SwiftHSVColorPicker.git', :tag => s.version.to_s }
s.source_files = 'Classes', 'Source/**/*.{swift}'
s.requires_arc = true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>