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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/)

# SwiftHSVColorPicker

## Description
Expand Down Expand Up @@ -36,4 +38,4 @@ SwiftHSVColorPicker is available under the MIT license. See the LICENSE file for
## Author
Johan Kasperi<br>
[@johankasperi](http://twitter.com/johankasperi)<br>
[kspri.se](http://kspri.se)
[kspri.se](http://kspri.se)
29 changes: 29 additions & 0 deletions Source/ColorUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,32 @@ func rgb2hsv(_ rgb: RGB) -> HSV {
hsb.alpha = rgb.alpha
return hsb
}

public extension UIColor {

var sRGBColor: UIColor {
if #available(iOS 10.0, *) {
// Only iOS 10.0 and above requires conversion from other color space to sRGB.
// On earlier versions of iOS, the specified values of red, green and blue are always between 0.0 and 1.0.
var sRGBColor: CGColor? = nil
if let colorSpaceName = cgColor.colorSpace?.name {
let compareResult = CFStringCompare(colorSpaceName, CGColorSpace.sRGB, CFStringCompareFlags(rawValue: 0))
if compareResult != .compareEqualTo, let sRGBSpace = CGColorSpace(name: CGColorSpace.sRGB),
let sRGBConvertedColor = cgColor.converted(to: sRGBSpace, intent: .defaultIntent, options: nil) {
sRGBColor = sRGBConvertedColor
} else {
sRGBColor = cgColor
}
}

if let sRGBColor = sRGBColor {
let color = UIColor(cgColor: sRGBColor)
return color
}
return self
} else {
// Fallback on earlier versions.
return self
}
}
}
6 changes: 3 additions & 3 deletions Source/ColorWheel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class ColorWheel: UIView {
if (d == 0) {
hue = 0;
} else {
hue = acos(dx/d) / CGFloat(M_PI) / 2.0
hue = acos(dx/d) / CGFloat.pi / 2.0
if (dy < 0) {
hue = 1.0 - hue
}
Expand All @@ -215,8 +215,8 @@ class ColorWheel: UIView {

let dimension: CGFloat = min(wheelLayer.frame.width, wheelLayer.frame.height)
let radius: CGFloat = saturation * dimension / 2
let x = dimension / 2 + radius * cos(hue * CGFloat(M_PI) * 2) + 20;
let y = dimension / 2 + radius * sin(hue * CGFloat(M_PI) * 2) + 20;
let x = dimension / 2 + radius * cos(hue * CGFloat.pi * 2) + 20;
let y = dimension / 2 + radius * sin(hue * CGFloat.pi * 2) + 20;
return CGPoint(x: x, y: y)
}

Expand Down
85 changes: 53 additions & 32 deletions Source/SwiftHSVColorPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

import UIKit

public protocol SwiftHSVColorPickerDelegate: class {
func colorPicker(_ picker: SwiftHSVColorPicker, didChangeColor color: UIColor)
}

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

open weak var delegate: SwiftHSVColorPickerDelegate?

open var color: UIColor!
var hue: CGFloat = 1.0
var saturation: CGFloat = 1.0
Expand All @@ -36,41 +42,52 @@ open class SwiftHSVColorPicker: UIView, ColorWheelDelegate, BrightnessViewDelega
self.saturation = saturation
self.brightness = brightness
self.color = color
setup()
setupIfNeeded()

delegate?.colorPicker(self, didChangeColor: self.color)
}

func setup() {
// Remove all subviews
let views = self.subviews
for view in views {
view.removeFromSuperview()
func setupIfNeeded() {
if colorWheel != nil && brightnessView != nil && selectedColorView != nil {
colorWheel.setViewBrightness(brightness)
brightnessView.setViewColor(color)
selectedColorView.setViewColor(color)
} else {
// Remove all subviews
let views = self.subviews
for view in views {
view.removeFromSuperview()
}

let selectedColorViewHeight: CGFloat = 44.0
let brightnessViewHeight: CGFloat = 26.0

// let color wheel get the maximum size that is not overflow from the frame for both width and height
let colorWheelSize = min(self.bounds.width, self.bounds.height - selectedColorViewHeight - brightnessViewHeight)

// 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)
selectedColorView.layer.cornerRadius = 5.0
selectedColorView.layer.borderWidth = 1.0
selectedColorView.layer.borderColor = UIColor.darkGray.cgColor
// Add selectedColorView as a subview of this view
self.addSubview(selectedColorView)

// Init new ColorWheel subview
colorWheel = ColorWheel(frame: CGRect(x: centeredX, y: selectedColorView.frame.maxY, 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)
}

let selectedColorViewHeight: CGFloat = 44.0
let brightnessViewHeight: CGFloat = 26.0

// let color wheel get the maximum size that is not overflow from the frame for both width and height
let colorWheelSize = min(self.bounds.width, self.bounds.height - selectedColorViewHeight - brightnessViewHeight)

// 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)

// Init new ColorWheel subview
colorWheel = ColorWheel(frame: CGRect(x: centeredX, y: selectedColorView.frame.maxY, 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)
}

func hueAndSaturationSelected(_ hue: CGFloat, saturation: CGFloat) {
Expand All @@ -79,12 +96,16 @@ open class SwiftHSVColorPicker: UIView, ColorWheelDelegate, BrightnessViewDelega
self.color = UIColor(hue: self.hue, saturation: self.saturation, brightness: self.brightness, alpha: 1.0)
brightnessView.setViewColor(self.color)
selectedColorView.setViewColor(self.color)

delegate?.colorPicker(self, didChangeColor: 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)

delegate?.colorPicker(self, didChangeColor: self.color)
}
}
2 changes: 1 addition & 1 deletion SwiftHSVColorPicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SwiftHSVColorPicker'
s.version = '1.1.0'
s.version = '1.1.3'
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'
Expand Down
7 changes: 7 additions & 0 deletions SwiftHSVColorPickerExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ViewController: UIViewController {
// Do any additional setup after loading the view, typically from a nib.

// Setup Color Picker
colorPicker.delegate = self
colorPicker.setViewColor(selectedColor)
}

Expand All @@ -40,3 +41,9 @@ class ViewController: UIViewController {
print(selectedColor!)
}
}

extension ViewController: SwiftHSVColorPickerDelegate {
func colorPicker(_ picker: SwiftHSVColorPicker, didChangeColor color: UIColor) {
print("did change color to: \(color), sRGB equivalent: \(color.sRGBColor)")
}
}