Skip to content

Commit 7eaaada

Browse files
committed
Color palette preview in progress...
1 parent b56a096 commit 7eaaada

File tree

10 files changed

+375
-30
lines changed

10 files changed

+375
-30
lines changed

ColorSet.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
0556FA652342169D00EB91DB /* DrawingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0556FA642342169D00EB91DB /* DrawingView.swift */; };
3535
0556FA67234275CF00EB91DB /* PaletteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0556FA66234275CF00EB91DB /* PaletteViewController.swift */; };
3636
0556FA6A2342761000EB91DB /* ColorSet.PaletteViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0556FA682342761000EB91DB /* ColorSet.PaletteViewController.xib */; };
37+
0556FA6E2342796C00EB91DB /* PaletteColorItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0556FA6B2342796C00EB91DB /* PaletteColorItem.swift */; };
38+
0556FA6F2342796C00EB91DB /* DrawingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0556FA6C2342796C00EB91DB /* DrawingView.swift */; };
39+
0556FA702342796C00EB91DB /* PaletteColorInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0556FA6D2342796C00EB91DB /* PaletteColorInfo.swift */; };
40+
0556FA7323427DE500EB91DB /* PaletteColorItem.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0556FA7123427DE500EB91DB /* PaletteColorItem.xib */; };
3741
0564590220E4DDFD001EA86F /* HexColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0564590120E4DDFD001EA86F /* HexColor.swift */; };
3842
057DC44E22B1492200BA6727 /* Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 057DC44D22B1492200BA6727 /* Test.swift */; };
3943
057DC45022B1492200BA6727 /* ColorSetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 05CD4ABA20ED7CD100DBDE99 /* ColorSetKit.framework */; };
@@ -179,6 +183,10 @@
179183
0556FA642342169D00EB91DB /* DrawingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DrawingView.swift; sourceTree = "<group>"; };
180184
0556FA66234275CF00EB91DB /* PaletteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaletteViewController.swift; sourceTree = "<group>"; };
181185
0556FA692342761000EB91DB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/ColorSet.PaletteViewController.xib; sourceTree = "<group>"; };
186+
0556FA6B2342796C00EB91DB /* PaletteColorItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteColorItem.swift; sourceTree = "<group>"; };
187+
0556FA6C2342796C00EB91DB /* DrawingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawingView.swift; sourceTree = "<group>"; };
188+
0556FA6D2342796C00EB91DB /* PaletteColorInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteColorInfo.swift; sourceTree = "<group>"; };
189+
0556FA7223427DE500EB91DB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/PaletteColorItem.xib; sourceTree = "<group>"; };
182190
0564590120E4DDFD001EA86F /* HexColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HexColor.swift; sourceTree = "<group>"; };
183191
057DC44B22B1492200BA6727 /* ColorSetKit-Test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ColorSetKit-Test.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
184192
057DC44D22B1492200BA6727 /* Test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Test.swift; sourceTree = "<group>"; };
@@ -433,6 +441,9 @@
433441
05D11EC020E1359C00A621B5 /* RGBColorComponent.swift */,
434442
059F8C78233CDB3300C9B83F /* SLColorComponent.swift */,
435443
0556FA66234275CF00EB91DB /* PaletteViewController.swift */,
444+
0556FA6D2342796C00EB91DB /* PaletteColorInfo.swift */,
445+
0556FA6B2342796C00EB91DB /* PaletteColorItem.swift */,
446+
0556FA6C2342796C00EB91DB /* DrawingView.swift */,
436447
);
437448
path = Classes;
438449
sourceTree = "<group>";
@@ -444,6 +455,7 @@
444455
05A6D8C32340EA880079AF3B /* ColorSet.LightnessPairCollectionViewItem.xib */,
445456
05A6D893233F805C0079AF3B /* ColorSet.LightnessPairWindowController.xib */,
446457
0543AE7E20DC35CC00284E99 /* ColorSet.MainWindowController.xib */,
458+
0556FA7123427DE500EB91DB /* PaletteColorItem.xib */,
447459
0556FA682342761000EB91DB /* ColorSet.PaletteViewController.xib */,
448460
05D11ECD20E165FB00A621B5 /* File.icns */,
449461
05D11EC620E13B9400A621B5 /* Icon.icns */,
@@ -644,6 +656,7 @@
644656
05D11EC720E13B9400A621B5 /* Icon.icns in Resources */,
645657
0556FA6A2342761000EB91DB /* ColorSet.PaletteViewController.xib in Resources */,
646658
0543AE8020DC35CC00284E99 /* ColorSet.MainWindowController.xib in Resources */,
659+
0556FA7323427DE500EB91DB /* PaletteColorItem.xib in Resources */,
647660
05D11ECE20E165FB00A621B5 /* File.icns in Resources */,
648661
0543AE7B20DC33DF00284E99 /* MainMenu.xib in Resources */,
649662
05D11ECA20E13DFF00A621B5 /* ColorSet.AboutWindowController.xib in Resources */,
@@ -700,13 +713,16 @@
700713
isa = PBXSourcesBuildPhase;
701714
buildActionMask = 2147483647;
702715
files = (
716+
0556FA702342796C00EB91DB /* PaletteColorInfo.swift in Sources */,
703717
059F8C7A233CDB3300C9B83F /* SLColorComponent.swift in Sources */,
704718
05A6D891233F80430079AF3B /* LightnessPairWindowController.swift in Sources */,
705719
0530C42320E1213500F5B259 /* NSView.swift in Sources */,
706720
05D11ECC20E13E0900A621B5 /* AboutWindowController.swift in Sources */,
707721
0564590220E4DDFD001EA86F /* HexColor.swift in Sources */,
708722
0543AE9E20E0FFD000284E99 /* ColorView.swift in Sources */,
709723
0543AE8220DC382A00284E99 /* ColorItem.swift in Sources */,
724+
0556FA6F2342796C00EB91DB /* DrawingView.swift in Sources */,
725+
0556FA6E2342796C00EB91DB /* PaletteColorItem.swift in Sources */,
710726
05A6D897233F8A300079AF3B /* LightnessString.swift in Sources */,
711727
05A6D8C22340EA740079AF3B /* LightnessPairCollectionViewItem.swift in Sources */,
712728
0530C41F20E10FEA00F5B259 /* ArrayIsEmpty.swift in Sources */,
@@ -823,6 +839,14 @@
823839
name = ColorSet.PaletteViewController.xib;
824840
sourceTree = "<group>";
825841
};
842+
0556FA7123427DE500EB91DB /* PaletteColorItem.xib */ = {
843+
isa = PBXVariantGroup;
844+
children = (
845+
0556FA7223427DE500EB91DB /* Base */,
846+
);
847+
name = PaletteColorItem.xib;
848+
sourceTree = "<group>";
849+
};
826850
05A6D893233F805C0079AF3B /* ColorSet.LightnessPairWindowController.xib */ = {
827851
isa = PBXVariantGroup;
828852
children = (

ColorSet/Classes/ApplicationDelegate.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class ApplicationDelegate: NSResponder, NSApplicationDelegate
4242
}
4343

4444
NotificationCenter.default.addObserver( self, selector: #selector( windowWillClose( _: ) ), name: NSWindow.willCloseNotification, object: nil )
45+
46+
#if DEBUG
47+
UserDefaults.standard.register( defaults: [ "NSApplicationCrashOnExceptions" : true ] )
48+
#endif
4549
}
4650

4751
func applicationWillTerminate( _ notification: Notification )

ColorSet/Classes/DrawingView.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Jean-David Gadina - www.xs-labs.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
******************************************************************************/
24+
25+
import Cocoa
26+
27+
public class DrawingView: NSView
28+
{
29+
public var onDraw: ( ( NSRect ) -> Void )?
30+
31+
public override func draw( _ rect: NSRect )
32+
{
33+
self.onDraw?( self.bounds )
34+
}
35+
36+
public override var isFlipped: Bool
37+
{
38+
return true
39+
}
40+
}

ColorSet/Classes/Exception.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
* THE SOFTWARE.
2323
******************************************************************************/
2424

25-
#import <Foundation/Foundation.h>
25+
@import Foundation;
2626

27-
NS_ASSUME_NONNULL_BEGIN
28-
29-
FOUNDATION_EXPORT void ObjCTryCatch( void ( ^ tryBlock )( void ), void ( ^ catchBlock )( NSException * ) );
30-
31-
NS_ASSUME_NONNULL_END
27+
void ObjCTryCatch( void ( ^ tryBlock )( void ), void ( ^ catchBlock )( NSException * ) );

ColorSet/Classes/MainWindowController.swift

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
112112
self.selectedColor = nil
113113
self.hasVariant = false
114114

115-
self.paletteViewController?.reload()
115+
self.paletteViewController?.colorSet = self.generateColorSet()
116116

117117
return
118118
}
@@ -149,7 +149,8 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
149149

150150
self.observations.append( contentsOf: [ o1, o2, o3 ] )
151151

152-
self.paletteViewController = PaletteViewController()
152+
self.paletteViewController = PaletteViewController()
153+
self.paletteViewController?.colorSet = self.generateColorSet()
153154

154155
if let view = self.paletteViewController?.view
155156
{
@@ -225,6 +226,29 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
225226
}
226227

227228
public func save( to url: URL )
229+
{
230+
let set = self.generateColorSet()
231+
232+
do
233+
{
234+
try set.writeTo( url: url )
235+
}
236+
catch let error as NSError
237+
{
238+
let alert = NSAlert( error: error )
239+
240+
guard let window = self.window else
241+
{
242+
alert.runModal()
243+
244+
return
245+
}
246+
247+
alert.beginSheetModal( for: window, completionHandler: nil )
248+
}
249+
}
250+
251+
public func generateColorSet() -> ColorSet
228252
{
229253
let set = ColorSet()
230254

@@ -247,23 +271,7 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
247271
set.add( color: color.color, variant: color.variant, lightnesses: lightnesses, forName: color.name )
248272
}
249273

250-
do
251-
{
252-
try set.writeTo( url: url )
253-
}
254-
catch let error as NSError
255-
{
256-
let alert = NSAlert( error: error )
257-
258-
guard let window = self.window else
259-
{
260-
alert.runModal()
261-
262-
return
263-
}
264-
265-
alert.beginSheetModal( for: window, completionHandler: nil )
266-
}
274+
return set
267275
}
268276

269277
@IBAction public func newColor( _ sender: Any? )
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*******************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Jean-David Gadina - www.xs-labs.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
******************************************************************************/
24+
25+
import Cocoa
26+
import ColorSetKit
27+
28+
@objc public class PaletteColorInfo: NSObject
29+
{
30+
@objc public dynamic var colorSet: ColorSet
31+
@objc public dynamic var name: String
32+
@objc public dynamic var colorPair: ColorPair
33+
34+
@objc public init?( colorSet: ColorSet, name: String, variants: [ CGFloat ] )
35+
{
36+
guard let pair = colorSet[ name ] else
37+
{
38+
return nil
39+
}
40+
41+
self.colorSet = colorSet
42+
self.name = name
43+
self.colorPair = pair
44+
}
45+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*******************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Jean-David Gadina - www.xs-labs.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
******************************************************************************/
24+
25+
import Cocoa
26+
import ColorSetKit
27+
28+
@objc public class PaletteColorItem: NSCollectionViewItem
29+
{
30+
public override var nibName: NSNib.Name?
31+
{
32+
return NSNib.Name( NSStringFromClass( type( of: self ) ) )
33+
}
34+
35+
public override func viewDidLoad()
36+
{
37+
guard let view = self.view as? DrawingView else
38+
{
39+
return
40+
}
41+
42+
view.onDraw = { [ weak self ] rect in self?.drawColors( rect: rect ) }
43+
}
44+
45+
private func drawColors( rect: NSRect )
46+
{
47+
guard let info = self.representedObject as? PaletteColorInfo else
48+
{
49+
return
50+
}
51+
52+
guard var base = info.colorPair.color?.usingColorSpace( .sRGB ) else
53+
{
54+
return
55+
}
56+
57+
if #available( macOS 10.14, * )
58+
{
59+
if NSApp.effectiveAppearance.name == .darkAqua, let variant = info.colorPair.variant?.usingColorSpace( .sRGB )
60+
{
61+
base = variant
62+
}
63+
}
64+
65+
base.setFill()
66+
rect.fill()
67+
68+
let hsl = base.hsl()
69+
let textColor = ( hsl.lightness >= 0.5 ) ? NSColor.black : NSColor.white
70+
var p = NSMakePoint( rect.origin.x + 10, rect.origin.y + 10 )
71+
72+
( info.name as NSString ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 25, weight: .thin ) ] )
73+
74+
p.y += 30
75+
76+
( ( "HEX: " + base.hexString ) as NSString ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 12, weight: .thin ) ] )
77+
78+
let hslString = String( format: "HSL: %.0f, %.0f, %.0f", hsl.hue * 360, hsl.saturation * 100, hsl.lightness * 100 )
79+
p.y += 15
80+
81+
( hslString as NSString ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 12, weight: .thin ) ] )
82+
}
83+
}

0 commit comments

Comments
 (0)