Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HBColorPickerAdditions To Swift #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 4 additions & 8 deletions Odyssey.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
53E564222506A8F400C4591F /* ColorPickerSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E564092506A8F400C4591F /* ColorPickerSlider.swift */; };
53E564232506A8F400C4591F /* LayoutGuide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640A2506A8F400C4591F /* LayoutGuide.swift */; };
53E564242506A8F400C4591F /* LayoutGuide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640A2506A8F400C4591F /* LayoutGuide.swift */; };
53E564252506A8F400C4591F /* UIColor+HBAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640B2506A8F400C4591F /* UIColor+HBAdditions.m */; };
53E564262506A8F400C4591F /* UIColor+HBAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640B2506A8F400C4591F /* UIColor+HBAdditions.m */; };
53E564272506A8F400C4591F /* ColorPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640C2506A8F400C4591F /* ColorPickerViewController.swift */; };
53E564282506A8F400C4591F /* ColorPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640C2506A8F400C4591F /* ColorPickerViewController.swift */; };
53E564292506A8F400C4591F /* ColorPickerMapSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E5640D2506A8F400C4591F /* ColorPickerMapSlider.swift */; };
Expand All @@ -69,6 +67,7 @@
53E5643B2506F18500C4591F /* ColourViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53E564392506F18500C4591F /* ColourViewer.swift */; };
933AA7C324E9D34F006182FC /* AppVersionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 933AA7C224E9D34F006182FC /* AppVersionManager.swift */; };
933AA7C424E9D34F006182FC /* AppVersionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 933AA7C224E9D34F006182FC /* AppVersionManager.swift */; };
9439AFAC259FD77100FA79EF /* UIColorAdditions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9439AFAB259FD77100FA79EF /* UIColorAdditions.swift */; };
F10593C124B1D98C00343F46 /* TableButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F10593C024B1D98C00343F46 /* TableButton.swift */; };
F10593C324B29DAC00343F46 /* AvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F10593C224B29DAC00343F46 /* AvatarView.swift */; };
F10593C524B2A49500343F46 /* CreditsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F10593C424B2A49500343F46 /* CreditsButton.swift */; };
Expand Down Expand Up @@ -264,11 +263,9 @@
53E564032506A8F400C4591F /* ColorPickerWheelView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerWheelView.swift; sourceTree = "<group>"; };
53E564042506A8F400C4591F /* ColorPickerSlidersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerSlidersViewController.swift; sourceTree = "<group>"; };
53E564052506A8F400C4591F /* ColorPickerSeparatorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerSeparatorView.swift; sourceTree = "<group>"; };
53E564062506A8F400C4591F /* UIColor+HBAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIColor+HBAdditions.h"; sourceTree = "<group>"; };
53E564072506A8F400C4591F /* ColorPickerSwatchViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerSwatchViewController.swift; sourceTree = "<group>"; };
53E564092506A8F400C4591F /* ColorPickerSlider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerSlider.swift; sourceTree = "<group>"; };
53E5640A2506A8F400C4591F /* LayoutGuide.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutGuide.swift; sourceTree = "<group>"; };
53E5640B2506A8F400C4591F /* UIColor+HBAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+HBAdditions.m"; sourceTree = "<group>"; };
53E5640C2506A8F400C4591F /* ColorPickerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerViewController.swift; sourceTree = "<group>"; };
53E5640D2506A8F400C4591F /* ColorPickerMapSlider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerMapSlider.swift; sourceTree = "<group>"; };
53E5640E2506A8F400C4591F /* ColorPickerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -279,6 +276,7 @@
53E564132506A8F400C4591F /* Color.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
53E564392506F18500C4591F /* ColourViewer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColourViewer.swift; sourceTree = "<group>"; };
933AA7C224E9D34F006182FC /* AppVersionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersionManager.swift; sourceTree = "<group>"; };
9439AFAB259FD77100FA79EF /* UIColorAdditions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorAdditions.swift; sourceTree = "<group>"; };
F10593C024B1D98C00343F46 /* TableButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableButton.swift; sourceTree = "<group>"; };
F10593C224B29DAC00343F46 /* AvatarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarView.swift; sourceTree = "<group>"; };
F10593C424B2A49500343F46 /* CreditsButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreditsButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -411,11 +409,10 @@
53E564032506A8F400C4591F /* ColorPickerWheelView.swift */,
53E564042506A8F400C4591F /* ColorPickerSlidersViewController.swift */,
53E564052506A8F400C4591F /* ColorPickerSeparatorView.swift */,
53E564062506A8F400C4591F /* UIColor+HBAdditions.h */,
53E564072506A8F400C4591F /* ColorPickerSwatchViewController.swift */,
53E564092506A8F400C4591F /* ColorPickerSlider.swift */,
53E5640A2506A8F400C4591F /* LayoutGuide.swift */,
53E5640B2506A8F400C4591F /* UIColor+HBAdditions.m */,
9439AFAB259FD77100FA79EF /* UIColorAdditions.swift */,
53E5640C2506A8F400C4591F /* ColorPickerViewController.swift */,
53E5640D2506A8F400C4591F /* ColorPickerMapSlider.swift */,
53E5640E2506A8F400C4591F /* ColorPickerDelegate.swift */,
Expand Down Expand Up @@ -852,7 +849,6 @@
F1C8FE9124F9DE04008A551E /* exploit_utilities.c in Sources */,
360512E024B10E5700DFD3AF /* UICircularRing.swift in Sources */,
F1A2B43624B430760087D978 /* PanelView.swift in Sources */,
53E564252506A8F400C4591F /* UIColor+HBAdditions.m in Sources */,
F191A9BD2432A948005E0F8F /* amfidtakeover.swift in Sources */,
5310399125041BB400CADF5C /* ThemeImagePicker.swift in Sources */,
360512E224B10E5700DFD3AF /* UICircularRingStyle.swift in Sources */,
Expand All @@ -875,6 +871,7 @@
F10593C324B29DAC00343F46 /* AvatarView.swift in Sources */,
36C0994824A456390043250A /* SeparatorView.swift in Sources */,
360512E624B10E5700DFD3AF /* UICircularProgressRing.swift in Sources */,
9439AFAC259FD77100FA79EF /* UIColorAdditions.swift in Sources */,
F14A7A9024A6FB3500215A6F /* kmem.c in Sources */,
531039962504F59800CADF5C /* AlderisButton.swift in Sources */,
F125E0532469E59F0023161C /* patchfinder64.c in Sources */,
Expand Down Expand Up @@ -935,7 +932,6 @@
F170602724CD53CD001596A0 /* PanelStackView.swift in Sources */,
F170602824CD53CD001596A0 /* PanelButton.swift in Sources */,
F170605724CD54D5001596A0 /* electra-dummy.swift in Sources */,
53E564262506A8F400C4591F /* UIColor+HBAdditions.m in Sources */,
F170602924CD53CD001596A0 /* TableButton.swift in Sources */,
F170602A24CD53CD001596A0 /* CreditsButton.swift in Sources */,
53E564182506A8F400C4591F /* ColorPickerWheelView.swift in Sources */,
Expand Down
2 changes: 0 additions & 2 deletions Odyssey/Alderis/Alderis.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

@import UIKit;

#import "UIColor+HBAdditions.h"

@interface UIView ()

@property (setter=_setContinuousCornerRadius:, nonatomic) double _continuousCornerRadius;
Expand Down
2 changes: 1 addition & 1 deletion Odyssey/Alderis/ColorPickerSlidersViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ extension ColorPickerSlidersViewController: UITextFieldDelegate {
return true
}

guard let uiColor = UIColor(hbcp_propertyListValue: "#\(canonicalizedString)") else {
guard let uiColor = UIColor(propertyListValue: "#\(canonicalizedString)") else {
return true
}

Expand Down
28 changes: 0 additions & 28 deletions Odyssey/Alderis/UIColor+HBAdditions.h

This file was deleted.

70 changes: 0 additions & 70 deletions Odyssey/Alderis/UIColor+HBAdditions.m

This file was deleted.

135 changes: 135 additions & 0 deletions Odyssey/Alderis/UIColorAdditions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// UIColorAdditions.swift
// Alderis
//
// Created by Ryan Nair on 10/5/20.
// Copyright © 2020 HASHBANG Productions. All rights reserved.
//

import UIKit

/// ColorPropertyListValue is a protocol representing types that can be passed to the\
/// `UIColor.init(propertyListValue:)` initialiser. `String` and `Array` both conform to this type.
///
/// - see: `UIColor.init(propertyListValue:)`
public protocol ColorPropertyListValue {}

/// A string can represent a `ColorPropertyListValue`.
///
/// - see: `UIColor.init(propertyListValue:)`
extension String: ColorPropertyListValue {}

/// An array of integers can represent a `ColorPropertyListValue`.
///
/// - see: `UIColor.init(propertyListValue:)`
extension Array: ColorPropertyListValue where Element: FixedWidthInteger {}

/// Alderis provides extensions to `UIColor` for the purpose of serializing and deserializing colors
/// into representations that can be stored in property lists, JSON, and similar formats.
public extension UIColor {

/// Initializes and returns a color object using data from the specified object.
///
/// The value is expected to be one of:
///
/// * An array of 3 or 4 integer RGB or RGBA color components, with values between 0 and 255 (e.g.
/// `[ 218, 192, 222 ]`)
/// * A CSS-style hex string, with an optional alpha component (e.g. `#DAC0DE` or `#DACODE55`)
/// * A short CSS-style hex string, with an optional alpha component (e.g. `#DC0` or `#DC05`)
///
/// Use `-[UIColor initWithHbcp_propertyListValue:]` to access this method from Objective-C.
///
/// - parameter value: The object to retrieve data from. See the discussion for the supported object
/// types.
/// - returns: An initialized color object, or nil if the value does not conform to the expected
/// type. The color information represented by this object is in the device RGB colorspace.
/// - see: `propertyListValue`
@nonobjc convenience init?(propertyListValue: ColorPropertyListValue?) {
if let array = propertyListValue as? [Int], array.count == 3 || array.count == 4 {
let floats = array.map { CGFloat($0) }
self.init(red: floats[0] / 255,
green: floats[1] / 255,
blue: floats[2] / 255,
alpha: array.count == 4 ? floats[3] : 1)
return
} else if var string = propertyListValue as? String {
if let range = string.range(of: ":") {
let location = string.distance(from: string.startIndex, to: range.lowerBound)
string = String(string[..<string.index(string.startIndex, offsetBy: location)])
}

if string.count == 4 || string.count == 5 {
let r = String(repeating: string[string.index(string.startIndex, offsetBy: 1)], count: 2)
let g = String(repeating: string[string.index(string.startIndex, offsetBy: 2)], count: 2)
let b = String(repeating: string[string.index(string.startIndex, offsetBy: 3)], count: 2)
let a = string.count == 5 ? String(repeating: string[string.index(string.startIndex, offsetBy: 4)], count: 2) : "FF"
string = String(format: "%@%@%@%@", r, g, b, a)
}

var hex: UInt64 = 0
let scanner = Scanner(string: string)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
scanner.scanHexInt64(&hex)

if string.count == 9 {
self.init(red: CGFloat((hex & 0xFF000000) >> 24) / 255,
green: CGFloat((hex & 0x00FF0000) >> 16) / 255,
blue: CGFloat((hex & 0x0000FF00) >> 8) / 255,
alpha: CGFloat((hex & 0x000000FF) >> 0) / 255)
return
} else {
self.init(red: CGFloat((hex & 0xFF0000) >> 16) / 255,
green: CGFloat((hex & 0x00FF00) >> 8) / 255,
blue: CGFloat((hex & 0x0000FF) >> 0) / 255,
alpha: 1)
return
}
}

return nil
}

/// Maps `init(propertyListValue:)` to Objective-C due to Swift limitations. This is an
/// implementation detail. Ignore this and use `UIColor(propertyListValue:)` or
/// `-[UIColor initWithHbcp_propertyListValue:]` as per usual.
///
/// - parameter value: The object to retrieve data from. See the discussion for the supported
/// object types.
/// - returns: An initialized color object, or nil if the value does not conform to the expected
/// type. The color information represented by this object is in the device RGB colorspace.
/// device RGB colorspace.
/// - see: `init(propertyListValue:)`
@objc(initWithHbcp_propertyListValue:)
convenience init?(_propertyListValueObjC propertyListValue: Any?) {
if let value = propertyListValue as? String {
self.init(propertyListValue: value)
} else if let value = propertyListValue as? [Int] {
self.init(propertyListValue: value)
} else {
return nil
}
}

/// Returns a string that represents the color.
///
/// The output is a string in the format `#AABBCC:0.5`, where the initial `#AABBCC` portion is a
/// 6-character CSS-style hex string, and the final `:0.5` portion represents the alpha value. If
/// the color’s alpha value is `1`, the alpha portion is excluded.
///
/// If the color is dynamic, for instance if it is a UIKit system color, or initialised via
/// `UIColor(dynamicProvider:)`, the color that matches the current trait collection is used.
///
/// - returns: A string representing the color, in the format discussed above.
/// - see: `init(propertyListValue:)`
@objc(hbcp_propertyListValue)
var propertyListValue: String {
var alpha: CGFloat = 0
getRed(nil, green: nil, blue: nil, alpha: &alpha)

let color = Color(uiColor: self.withAlphaComponent(1))
let hexString = color.hexString()
let alphaString = alpha == 1 ? "" : String(format: ":%.5G", alpha)
return "\(hexString)\(alphaString)"
}

}