From f6877570313fd3882ead7c9b51c04f38d5e91f38 Mon Sep 17 00:00:00 2001 From: Vincent Neo <23420208+vincentneo@users.noreply.github.com> Date: Sun, 26 Feb 2023 23:49:43 +0800 Subject: [PATCH] SPM support + UIColor convenience --- Class/PFColorHash.swift | 43 ++++++++++++++++++++++++++++++-- PFColorHash/ViewController.swift | 5 +--- Package.swift | 29 +++++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 Package.swift diff --git a/Class/PFColorHash.swift b/Class/PFColorHash.swift index 56c415a..0afb178 100644 --- a/Class/PFColorHash.swift +++ b/Class/PFColorHash.swift @@ -6,7 +6,11 @@ // Copyright (c) 2015 Cee. All rights reserved. // -import Foundation +#if os(macOS) +import AppKit +#else +import UIKit +#endif open class PFColorHash { @@ -67,7 +71,18 @@ open class PFColorHash { return (Double(h), Double(s), Double(l)) } - + + #if os(macOS) + public func nsColor(_ str: String) -> NSColor { + let hsl = self.hsl(str) + return NSColor(hue: hsl.h, saturation: hsl.s, lightness: hsl.l, alpha: 1) + } + #else + public func uiColor(_ str: String) -> UIColor { + let hsl = self.hsl(str) + return UIColor(hue: hsl.h, saturation: hsl.s, lightness: hsl.l, alpha: 1) + } + #endif final public func rgb(_ str: String) -> (r: Int, g: Int, b: Int) { let hslValue = hsl(str) @@ -123,3 +138,27 @@ extension Character return Int(scalars[scalars.startIndex].value) } } + +#if os(macOS) +typealias SystemColor = NSColor +#else +typealias SystemColor = UIColor +#endif + +extension SystemColor { + + // https://gist.github.com/adamgraham/3ada1f7f4cdf8131dd3d2d95bd116cfc + convenience init(hue: CGFloat, saturation: CGFloat, lightness: CGFloat, alpha: CGFloat = 1.0) { + + let h = hue / 360.0 + var s = saturation + let l = lightness + + let t = s * ((l < 0.5) ? l : (1.0 - l)) + let b = l + t + s = (l > 0.0) ? (2.0 * t / b) : 0.0 + + self.init(hue: h, saturation: s, brightness: b, alpha: alpha) + } + +} diff --git a/PFColorHash/ViewController.swift b/PFColorHash/ViewController.swift index 1f711ce..18b1fa1 100644 --- a/PFColorHash/ViewController.swift +++ b/PFColorHash/ViewController.swift @@ -60,10 +60,7 @@ extension ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let index = states.count - (indexPath as NSIndexPath).row - 1 let str = states[index] - let color = UIColor(red: (CGFloat)(colorHash.rgb(str).r) / 255.0, - green: (CGFloat)(colorHash.rgb(str).g) / 255.0, - blue: (CGFloat)(colorHash.rgb(str).b) / 255.0, - alpha: 1.0) + let color = colorHash.uiColor(str) let cell: UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell") diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..b855d09 --- /dev/null +++ b/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version:5.7 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "PFColorHash", + platforms: [ + .iOS(.v13), .macOS(.v10_13), .watchOS(.v4), .tvOS(.v13) + ], + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "PFColorHash", + targets: ["PFColorHash"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "PFColorHash", + dependencies: [], + path: "Class"), + ] +)