Skip to content

Commit

Permalink
Use HSVA based color
Browse files Browse the repository at this point in the history
  • Loading branch information
noppefoxwolf committed Jul 17, 2022
1 parent 53f1d26 commit 16b47a3
Show file tree
Hide file tree
Showing 29 changed files with 287 additions and 300 deletions.
12 changes: 6 additions & 6 deletions Example.swiftpm/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ContentViewController: UIViewController {

let colorPickerButton = UIButton(primaryAction: UIAction(title: "noppefoxwolf/ColorPicker", handler: { _ in
self.presentColorPicker(
CGColor(red: 1, green: 0, blue: 0, alpha: 0.5)
UIColor(red: 1, green: 0, blue: 0, alpha: 0.5)
)
}))
let uiColorPickerButton = UIButton(primaryAction: UIAction(title: "apple/UIColorPicker", handler: { _ in
Expand Down Expand Up @@ -88,14 +88,14 @@ class ContentViewController: UIViewController {
}
}

func presentColorPicker(_ color: CGColor) {
func presentColorPicker(_ color: UIColor) {
let vc = ColorPickerViewController()
vc.supportsAlpha = true
let configuration = ColorPickerConfiguration.default
configuration.initialColorItems = [
.init(id: UUID(), color: CGColor(red: 1, green: 0, blue: 0, alpha: 1)),
.init(id: UUID(), color: CGColor(red: 0, green: 1, blue: 0, alpha: 1)),
.init(id: UUID(), color: CGColor(red: 0, green: 0, blue: 1, alpha: 1)),
.init(id: UUID(), color: HSVA(UIColor(red: 1, green: 0, blue: 0, alpha: 1))),
.init(id: UUID(), color: HSVA(UIColor(red: 0, green: 1, blue: 0, alpha: 1))),
.init(id: UUID(), color: HSVA(UIColor(red: 0, green: 0, blue: 1, alpha: 1))),
]
// configuration.colorPickers = [HSBHexSliderColorPicker(frame: .null)]
// configuration.usesSwatchTool = false
Expand All @@ -121,7 +121,7 @@ extension ContentViewController: ColorPickerViewControllerDelegate {
print(#function, viewController.selectedColor)
}

func colorPickerViewController(_ viewController: ColorPickerViewController, didSelect color: CGColor, continuously: Bool) {
func colorPickerViewController(_ viewController: ColorPickerViewController, didSelect color: UIColor, continuously: Bool) {
print(#function, color, continuously)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/ColorPicker/Components/ColorCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ColorCell: UICollectionViewCell {
class ColorView: UIView {

@Invalidating(.display)
var color: CGColor = .white
var color: HSVA = .white

enum Style {
case normal
Expand All @@ -55,7 +55,7 @@ class ColorView: UIView {
switch style {
case .normal:
let size = CGSize(width: 30, height: 30)
context.setFillColor(color)
context.setFillColor(color.makeColor().cgColor)
let origin = CGPoint(
x: (rect.width - size.width) / 2,
y: (rect.height - size.height) / 2
Expand All @@ -64,7 +64,7 @@ class ColorView: UIView {
case .outlined:
do { // inner ellipse
let size = CGSize(width: 17, height: 17)
context.setFillColor(color)
context.setFillColor(color.makeColor().cgColor)
let origin = CGPoint(
x: (rect.width - size.width) / 2,
y: (rect.height - size.height) / 2
Expand All @@ -77,7 +77,7 @@ class ColorView: UIView {
x: (rect.width - size.width) / 2,
y: (rect.height - size.height) / 2
)
context.setStrokeColor(color)
context.setStrokeColor(color.makeColor().cgColor)
context.setLineWidth(3)
context.strokeEllipse(in: CGRect(origin: origin, size: size))
}
Expand Down
26 changes: 12 additions & 14 deletions Sources/ColorPicker/Extensions/ColorFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import UIKit

class ColorFormatter: Formatter {

func color(from string: String) -> CGColor? {
func color(from string: String) -> HSVA? {
let hexString = string.trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString)

Expand All @@ -14,21 +14,19 @@ class ColorFormatter: Formatter {
var rgbValue:UInt64 = 0
scanner.scanHexInt64(&rgbValue)

return CGColor(
red: Double((rgbValue & 0xFF0000) >> 16) / 255.0,
green: Double((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: Double(rgbValue & 0x0000FF) / 255.0,
alpha: 1.0
)
let r: Double = Double((rgbValue & 0xFF0000) >> 16) / 255.0
let g: Double = Double((rgbValue & 0x00FF00) >> 8) / 255.0
let b: Double = Double(rgbValue & 0x0000FF) / 255.0
let a: Double = 1

return HSVA(hsv: RGB(r: r, g: g, b: b).hsv, a: a)
}

func string(from color: CGColor) -> String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0

color.getRed(&r, green: &g, blue: &b, alpha: &a)
func string(from color: HSVA) -> String {
var r: CGFloat = color.hsv.rgb.r
var g: CGFloat = color.hsv.rgb.g
var b: CGFloat = color.hsv.rgb.b
var a: CGFloat = color.a

let rgb: Int = (Int)(r * 255) << 16 | (Int)(g * 255) << 8 | (Int)(b * 255) << 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class AlphaColorPicker: UIControl {

let alphaSlider = ColorSliderWithInputView()

private var _color: CGColor = .white
private var _color: HSVA = .white

public var color: CGColor {
var color: HSVA {
get { _color }
set {
guard _color != newValue else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public class ClassicColorPicker: UIControl, ColorPicker {
let thumbView: ThumbView = .init(frame: .null)

@Invalidating(.constraints)
private var _color: CGColor = .white
private var _color: HSVA = .white

public var color: CGColor {
public var color: HSVA {
get { _color }
set {
_color = newValue
thumbView.color = newValue
hueSlider.color = newValue
colorView.hue = newValue.hsb.h
colorView.hue = newValue.hsv.h
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ class ClassicColorView: UIView {
fatalError()
}

func color(at location: CGPoint) -> CGColor {
func color(at location: CGPoint) -> HSVA {
let saturation = location.x / bounds.width
let brightness = 1.0 - (location.y / bounds.height)
let clampedSaturation = max(min(saturation, 1), 0)
let clampedBrightness = max(min(brightness, 1), 0)
return CGColor.make(
hsv: HSV(h: hue, s: clampedSaturation, v: clampedBrightness),
alpha: 1
)
return HSVA(hsv: HSV(h: hue, s: clampedSaturation, v: clampedBrightness), a: 1)
}

func location(by color: CGColor) -> CGPoint? {
let hsb = color.hsb
func location(by color: HSVA) -> CGPoint? {
let hsb = color.hsv
return CGPoint(x: hsb.s * bounds.width, y: (1.0 - hsb.v) * bounds.height)
}

func locationMultiply(by color: CGColor) -> CGSize {
let hsb = color.hsb
func locationMultiply(by color: HSVA) -> CGSize {
let hsb = color.hsv
return CGSize(width: hsb.s, height: (1.0 - hsb.v))
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ColorPicker/Feature/ColorPicker/ColorPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import UIKit
public protocol ColorPicker: AnyObject {
var id: String { get }
var title: String { get }
var color: CGColor { get set }
var color: HSVA { get set }
var continuously: Bool { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit
import SwiftUI

protocol ColorPickerContentViewControllerDelegate: AnyObject {
func colorPickerViewController(_ viewController: ColorPickerContentViewController, didSelect color: CGColor, continuously: Bool)
func colorPickerViewController(_ viewController: ColorPickerContentViewController, didSelect color: HSVA, continuously: Bool)
func colorPickerViewControllerDidFinish(_ viewController: ColorPickerContentViewController)
func colorPickerSwatchDidChanged(_ viewController: ColorPickerContentViewController)

Expand All @@ -21,13 +21,13 @@ class ColorPickerContentViewController: UIViewController {
/// colorPicker - Swatch
let colorPickersStackView = UIStackView()

private var _color: CGColor = .white {
private var _color: HSVA = .white {
didSet {
onUpdate(_color)
}
}

var color: CGColor {
var color: HSVA {
get { _color }
set {
guard _color != newValue else { return }
Expand Down Expand Up @@ -125,7 +125,9 @@ class ColorPickerContentViewController: UIViewController {
)

let colorPickerAction = UIAction { [unowned self, unowned colorPicker, unowned alphaColorPicker] _ in
self.color = colorPicker.color.withAlphaComponent(alphaColorPicker.color.alpha)
var newColor = colorPicker.color
newColor.a = alphaColorPicker.color.a
self.color = newColor
self.delegate?.colorPickerViewController(
self,
didSelect: self.color,
Expand Down Expand Up @@ -202,7 +204,7 @@ class ColorPickerContentViewController: UIViewController {
UserDefaults.standard.latestColorPickerID = colorPicker.id
}

func onUpdate(_ color: CGColor) {
func onUpdate(_ color: HSVA) {
swatchAndPreviewView.color = color
alphaColorPicker.color = color
configuration.colorPickers.forEach({ $0.color = color })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

public protocol ColorPickerViewControllerDelegate: AnyObject {
func colorPickerViewController(_ viewController: ColorPickerViewController, didSelect color: CGColor, continuously: Bool)
func colorPickerViewController(_ viewController: ColorPickerViewController, didSelect color: UIColor, continuously: Bool)
func colorPickerViewControllerDidFinish(_ viewController: ColorPickerViewController)
}

Expand Down Expand Up @@ -33,9 +33,9 @@ public class ColorPickerViewController: UINavigationController {
set { contentViewController.supportsAlpha = newValue }
}

public var selectedColor: CGColor {
get { contentViewController.color }
set { contentViewController.color = newValue }
public var selectedColor: UIColor {
get { contentViewController.color.makeColor() }
set { contentViewController.color = HSVA(newValue) }
}

public var colorItems: [ColorItem] {
Expand All @@ -62,8 +62,8 @@ extension ColorPickerViewController: ColorPickerContentViewControllerDelegate {
_delegate?.colorPickerViewControllerDidFinish(self)
}

func colorPickerViewController(_ viewController: ColorPickerContentViewController, didSelect color: CGColor, continuously: Bool) {
_delegate?.colorPickerViewController(self, didSelect: color, continuously: continuously)
func colorPickerViewController(_ viewController: ColorPickerContentViewController, didSelect color: HSVA, continuously: Bool) {
_delegate?.colorPickerViewController(self, didSelect: color.makeColor(), continuously: continuously)
}

func colorPickerSwatchDidChanged(_ viewController: ColorPickerContentViewController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,6 @@ class InnerDiskColorView: UIView {
UIColor(hue: 1, saturation: sat, brightness: 1, alpha: 1).cgColor
})
layer.addSublayer(gradientLayer)

// maskLayer2.type = .axial
// maskLayer2.startPoint = CGPoint(x: 0.5, y: 0)
// maskLayer2.endPoint = CGPoint(x: 0.5, y: 1)
// maskLayer2.colors = [UIColor.white.cgColor, UIColor.clear.cgColor]
// maskLayer2.locations = [0, 1]
// maskLayer2.allowsGroupOpacity = false
//
// gradientLayer.mask = maskLayer2

// gradientLayer.mask = maskLayer
}

required init?(coder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class GridColorPicker: UIControl, ColorPicker {
let markerView: GridColorMarkerView = .init(frame: .null)

@Invalidating(.constraints)
private var _color: CGColor = .white
private var _color: HSVA = .white

public var color: CGColor {
public var color: HSVA {
get { _color }
set {
guard _color != newValue else { return }
Expand Down
Loading

0 comments on commit 16b47a3

Please sign in to comment.