Skip to content

Commit

Permalink
Added preferences
Browse files Browse the repository at this point in the history
Fixed exception due to grayscale color space
  • Loading branch information
Gofake1 committed May 22, 2017
1 parent ddb39c3 commit dc3d1c5
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Color Picker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
4A66F1971EBC274100D81A4A /* ColorWheelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A66F1961EBC274100D81A4A /* ColorWheelView.swift */; };
4A66F19B1EBC6F0900D81A4A /* GradientSliderCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A66F19A1EBC6F0900D81A4A /* GradientSliderCell.swift */; };
4A720F7C1EBD7E0000205678 /* NSColor+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A720F7B1EBD7E0000205678 /* NSColor+.swift */; };
4A720F811EBE8B8000205678 /* Preferences.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4A720F831EBE8B8000205678 /* Preferences.storyboard */; };
4A720F841EBE8B8800205678 /* Palettes.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4A720F861EBE8B8800205678 /* Palettes.storyboard */; };
4AC3DDBE1EC185E600E08B8D /* PaletteCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC3DDBD1EC185E600E08B8D /* PaletteCollection.swift */; };
4AC3DDC11EC2B87D00E08B8D /* MainMenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC3DDC01EC2B87D00E08B8D /* MainMenuController.swift */; };
Expand All @@ -44,6 +45,7 @@
4A66F1961EBC274100D81A4A /* ColorWheelView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorWheelView.swift; sourceTree = "<group>"; };
4A66F19A1EBC6F0900D81A4A /* GradientSliderCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GradientSliderCell.swift; sourceTree = "<group>"; };
4A720F7B1EBD7E0000205678 /* NSColor+.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSColor+.swift"; sourceTree = "<group>"; };
4A720F821EBE8B8000205678 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Preferences.storyboard; sourceTree = "<group>"; };
4A720F851EBE8B8800205678 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Palettes.storyboard; sourceTree = "<group>"; };
4AC3DDBD1EC185E600E08B8D /* PaletteCollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteCollection.swift; sourceTree = "<group>"; };
4AC3DDC01EC2B87D00E08B8D /* MainMenuController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainMenuController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -86,6 +88,7 @@
children = (
4A62A80B1EBEB4A4003A02D2 /* ColorPicker.storyboard */,
4A720F861EBE8B8800205678 /* Palettes.storyboard */,
4A720F831EBE8B8000205678 /* Preferences.storyboard */,
4A62A8101EBEBA07003A02D2 /* MainMenu.xib */,
4A62A81B1EBED0FB003A02D2 /* PaletteCollectionViewItem.xib */,
4A66F1861EBC24E200D81A4A /* AppDelegate.swift */,
Expand Down Expand Up @@ -173,6 +176,7 @@
buildActionMask = 2147483647;
files = (
4A720F841EBE8B8800205678 /* Palettes.storyboard in Resources */,
4A720F811EBE8B8000205678 /* Preferences.storyboard in Resources */,
4A66F18B1EBC24E200D81A4A /* Assets.xcassets in Resources */,
4AC3DDC91EC7EBF100E08B8D /* LICENSE in Resources */,
4A62A8091EBEB4A4003A02D2 /* ColorPicker.storyboard in Resources */,
Expand Down Expand Up @@ -230,6 +234,14 @@
name = PaletteCollectionViewItem.xib;
sourceTree = "<group>";
};
4A720F831EBE8B8000205678 /* Preferences.storyboard */ = {
isa = PBXVariantGroup;
children = (
4A720F821EBE8B8000205678 /* Base */,
);
name = Preferences.storyboard;
sourceTree = "<group>";
};
4A720F861EBE8B8800205678 /* Palettes.storyboard */ = {
isa = PBXVariantGroup;
children = (
Expand Down
12 changes: 12 additions & 0 deletions Color Picker/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class AppDelegate: NSObject {
palettes?.window?.isExcludedFromWindowsMenu = true
return palettes
}()

fileprivate lazy var preferences: NSWindowController? = {
let preferences = NSStoryboard(name: "Preferences", bundle: nil).instantiateInitialController()
as? NSWindowController
preferences?.window?.isExcludedFromWindowsMenu = true
return preferences
}()

@IBAction func resetPalettes(_ sender: NSButton) {
paletteCollection.restoreDefaults()
}
}

extension AppDelegate: NSApplicationDelegate {
Expand All @@ -41,6 +52,7 @@ extension AppDelegate: NSApplicationDelegate {
mainMenuController.colorPicker = colorPicker
mainMenuController.palettes = palettes
mainMenuController.palettesCollection = paletteCollection
mainMenuController.preferences = preferences
(palettes?.contentViewController as! PaletteViewController).paletteCollection = paletteCollection

colorPicker?.showWindow(nil)
Expand Down
80 changes: 80 additions & 0 deletions Color Picker/Base.lproj/Preferences.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16G8c" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="Hh4-8Z-bYp">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12120"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Window Controller-->
<scene sceneID="Kyy-kW-0Vy">
<objects>
<windowController id="Hh4-8Z-bYp" sceneMemberID="viewController">
<window key="window" title="Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="DpL-oe-ukv">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<rect key="contentRect" x="294" y="372" width="400" height="300"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1200"/>
</window>
<connections>
<segue destination="san-kV-6IZ" kind="relationship" relationship="window.shadowedContentViewController" id="Hx4-kH-d9R"/>
</connections>
</windowController>
<customObject id="hub-KQ-PQv" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="177" y="-91"/>
</scene>
<!--Tab View Controller-->
<scene sceneID="d4B-xM-2yf">
<objects>
<tabViewController tabStyle="toolbar" id="san-kV-6IZ" sceneMemberID="viewController">
<tabViewItems>
<tabViewItem image="NSPreferencesGeneral" id="Jpq-W7-vAJ"/>
</tabViewItems>
<tabView key="tabView" misplaced="YES" type="noTabsNoBorder" id="8x7-B8-RO5">
<rect key="frame" x="0.0" y="0.0" width="400" height="300"/>
<autoresizingMask key="autoresizingMask"/>
<font key="font" metaFont="message"/>
<tabViewItems/>
</tabView>
<connections>
<segue destination="jDH-Sn-CNl" kind="relationship" relationship="tabItems" id="7Dp-U8-TKL"/>
</connections>
</tabViewController>
<customObject id="yVy-Kc-ZIy" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="177" y="342"/>
</scene>
<!--General-->
<scene sceneID="vqh-hT-nbe">
<objects>
<viewController title="General" id="jDH-Sn-CNl" sceneMemberID="viewController">
<view key="view" id="zSe-au-hgV">
<rect key="frame" x="0.0" y="0.0" width="346" height="300"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="EmP-pE-FCf">
<rect key="frame" x="110" y="252" width="127" height="32"/>
<buttonCell key="cell" type="push" title="Reset Palettes" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Yyb-MS-Qcv">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="resetPalettes:" target="jUF-ED-me1" id="6pU-z4-Hqq"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="EmP-pE-FCf" firstAttribute="top" secondItem="zSe-au-hgV" secondAttribute="top" constant="20" id="8Xm-1d-Wqn"/>
<constraint firstItem="EmP-pE-FCf" firstAttribute="centerX" secondItem="zSe-au-hgV" secondAttribute="centerX" id="CPk-6v-ZLm"/>
</constraints>
</view>
</viewController>
<customObject id="jUF-ED-me1" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="177" y="754"/>
</scene>
</scenes>
<resources>
<image name="NSPreferencesGeneral" width="32" height="32"/>
</resources>
</document>
3 changes: 2 additions & 1 deletion Color Picker/MainMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MainMenuController: NSObject {
weak var colorPicker: NSWindowController?
weak var palettes: NSWindowController?
weak var palettesCollection: PaletteCollection!
weak var preferences: NSWindowController?

override func awakeFromNib() {
NSPasteboard.general().declareTypes([NSPasteboardTypeString], owner: self)
Expand Down Expand Up @@ -44,7 +45,7 @@ class MainMenuController: NSObject {
}

@IBAction func showPreferences(_ sender: NSMenuItem) {

preferences?.showWindow(nil)
}

func addColorToPalette(_ sender: NSMenuItem) {
Expand Down
5 changes: 4 additions & 1 deletion Color Picker/NSColor+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ extension NSColor {
if hexString.hasPrefix("#") {
hexString.remove(at: hexString.startIndex)
}
guard let hexInt = Int(hexString, radix: 16) else { self.init(white: 1, alpha: 1); return }
guard let hexInt = Int(hexString, radix: 16) else {
self.init(red: 1, green: 1, blue: 1, alpha: 1)
return
}
self.init(red: CGFloat((hexInt >> 16) & 0xFF) / 255.0,
green: CGFloat((hexInt >> 8) & 0xFF) / 255.0,
blue: CGFloat((hexInt >> 0) & 0xFF) / 255.0,
Expand Down
2 changes: 1 addition & 1 deletion Color Picker/Palette.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Palette: NSObject, NSCoding {

dynamic var name: String
dynamic var colors: Set<NSColor>
private let id: Int
let id: Int

init(id: Int? = nil, name: String = "New Palette", colors: Set<NSColor> = Set()) {
self.name = name
Expand Down
25 changes: 17 additions & 8 deletions Color Picker/PaletteCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@

import Cocoa

fileprivate let defaultPalettes = [
Palette(name: "Rainbow",
colors: [NSColor.red, NSColor.orange, NSColor.yellow, NSColor.green, NSColor.blue,
NSColor.purple]),
Palette(name: "Grayscale",
colors: [NSColor(red: 1, green: 1, blue: 1, alpha: 1),
NSColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 1),
NSColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1),
NSColor(red: 0.25, green: 0.25, blue: 0.25, alpha: 1),
NSColor(red: 0, green: 0, blue: 0, alpha: 1)])
]

class PaletteCollection: NSObject, NSCoding {

dynamic var palettes: [Palette]

override init() {
palettes = [
Palette(name: "Rainbow",
colors: [NSColor.red, NSColor.orange, NSColor.yellow, NSColor.green, NSColor.blue,
NSColor.purple]),
Palette(name: "Grayscale",
colors: [NSColor.white, NSColor(white: 0.75, alpha: 1.0), NSColor.gray,
NSColor(white: 0.25, alpha: 1.0), NSColor.black])
]
palettes = defaultPalettes
}

func addPalette(_ palette: Palette) {
palettes.append(palette)
}

func restoreDefaults() {
palettes = defaultPalettes
}

// MARK: - NSCoding

required init?(coder aDecoder: NSCoder) {
Expand Down

0 comments on commit dc3d1c5

Please sign in to comment.