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
546 changes: 546 additions & 0 deletions Poll/Poll.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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>SchemeUserState</key>
<dict>
<key>Poll.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
154 changes: 154 additions & 0 deletions Poll/Poll/ColorStruct.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
//
// ColorStruct.swift
// colorCounter
//
// Created by Michael Redig on 4/14/19.
// Copyright © 2019 Michael Redig. All rights reserved.
//

import Foundation
import UIKit


struct Color: Hashable, Codable {
var red: UInt8
var green: UInt8
var blue: UInt8
var alpha: UInt8

var redFloat: CGFloat {
return CGFloat(red) / CGFloat(255)
}
var greenFloat: CGFloat {
return CGFloat(green) / CGFloat(255)
}
var blueFloat: CGFloat {
return CGFloat(blue) / CGFloat(255)
}
var alphaFloat: CGFloat {
return CGFloat(alpha) / CGFloat(255)
}

var uiColor: UIColor {
return UIColor(red: redFloat, green: greenFloat, blue: blueFloat, alpha: alphaFloat)
}

var isDarkColor: Bool {
return ((redFloat * 0.3) + (greenFloat * 0.59) + (blueFloat * 0.11)) < 0.5
}

var hexString: String {
let redHex = String(format: "%02X", red)
let greenHex = String(format: "%02X", green)
let blueHex = String(format: "%02X", blue)
return redHex + greenHex + blueHex
}

init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8 = 255) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}

static func distanceBetween(colorA: Color, colorB: Color) -> CGFloat {
return sqrt(pow((colorA.redFloat-colorB.redFloat), 2) + pow((colorA.greenFloat-colorB.greenFloat), 2) + pow((colorA.blueFloat-colorB.blueFloat), 2))
}

func distanceTo(color: Color) -> CGFloat {
return Color.distanceBetween(colorA: self, colorB: color)
}

mutating func stochasticize(with granularity: UInt8, includeAlpha: Bool = false) {
red = (red / granularity) * granularity
green = (green / granularity) * granularity
blue = (blue / granularity) * granularity
if includeAlpha {
alpha = (alpha / granularity) * granularity
}
}

mutating func average(with newColor: Color, currentCount: Int, includeAlpha: Bool = false) {
red = calculateAverage(existingValue: red, newValue: newColor.red, currentCount: currentCount)
green = calculateAverage(existingValue: green, newValue: newColor.green, currentCount: currentCount)
blue = calculateAverage(existingValue: blue, newValue: newColor.blue, currentCount: currentCount)
if includeAlpha {
alpha = calculateAverage(existingValue: alpha, newValue: newColor.alpha, currentCount: currentCount)
}
}

func calculateAverage(existingValue: UInt8, newValue: UInt8, currentCount: Int) -> UInt8 {
guard currentCount >= 1 else { return existingValue }
return UInt8((CGFloat(existingValue) * CGFloat(currentCount) + CGFloat(newValue)) / CGFloat(currentCount + 1))
}
}

struct ColorStructDict: Codable {
let colorsByName: [String: Color]

static func getColor(named proposedColorName: String) -> Color? {
guard let namedColors = namedColors else { fatalError("Couldn't load color names") }

let proposedNameLC = proposedColorName.lowercased().replacingOccurrences(of: ##"\s+"##, with: "", options: .regularExpression, range: nil)

var closestColor: (name: String, color: Color)?
for (name, color) in namedColors {
if name.lowercased() == proposedNameLC {
return color
} else if name.lowercased().contains(proposedNameLC) {
closestColor = (name, color)
}
}

return closestColor?.color
}

static func getNameForColor(color: Color, unCamelcase: Bool = true) -> (name: String, isExact: Bool) {
guard let namedColors = namedColors else { fatalError("Couldn't load color names") }

var nearestName: String = ""
var nearestDistance: CGFloat = 15
for (name, namedColor) in namedColors {
let distance = color.distanceTo(color: namedColor)
if distance < nearestDistance {
nearestDistance = distance
nearestName = name
}
if nearestDistance == 0 {
if unCamelcase {
nearestName = ColorStructDict.unCamelcase(string: nearestName)
}
return (nearestName, true)
}
}

if unCamelcase {
nearestName = ColorStructDict.unCamelcase(string: nearestName)
}
return (nearestName, false)
}

private static func unCamelcase(string: String) -> String {
let array = Array(string)
var outStr = ""
for (index, letter) in array.enumerated() {
if index == 0 {
outStr += String(letter).uppercased()
} else if (letter.isUppercase) {
outStr += " \(letter)"
} else {
outStr += String(letter)
}
}
return outStr
}

static var namedColors: [String: Color]? = {
guard let jsonURL = Bundle.main.url(forResource: "colorNames", withExtension: "json") else { return nil }
guard let jsonData = try? Data(contentsOf: jsonURL) else { return nil }

let decoder = JSONDecoder()
let theDict = try? decoder.decode(ColorStructDict.self, from: jsonData).colorsByName
return theDict
}()
}
46 changes: 46 additions & 0 deletions Poll/Poll/Resources/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AppDelegate.swift
// Poll
//
// Created by Michael Redig on 4/22/19.
// Copyright © 2019 Michael Redig. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions Poll/Poll/Resources/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
22 changes: 22 additions & 0 deletions Poll/Poll/Resources/Assets.xcassets/Globe.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Globe@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Globe@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions Poll/Poll/Resources/Assets.xcassets/Plus.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Plus@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Plus@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading