Skip to content

Commit 2ae13d6

Browse files
committed
Support for hex colors
1 parent 27cc54a commit 2ae13d6

File tree

4 files changed

+138
-16
lines changed

4 files changed

+138
-16
lines changed

ColorSet.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
0543AE8020DC35CC00284E99 /* ColorSet.MainWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0543AE7E20DC35CC00284E99 /* ColorSet.MainWindowController.xib */; };
1818
0543AE8220DC382A00284E99 /* ColorItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0543AE8120DC382A00284E99 /* ColorItem.swift */; };
1919
0543AE9E20E0FFD000284E99 /* ColorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0543AE9D20E0FFD000284E99 /* ColorView.swift */; };
20+
0564590220E4DDFD001EA86F /* HexColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0564590120E4DDFD001EA86F /* HexColor.swift */; };
2021
05D11EC120E1359C00A621B5 /* ColorComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05D11EC020E1359C00A621B5 /* ColorComponent.swift */; };
2122
05D11EC720E13B9400A621B5 /* Icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 05D11EC620E13B9400A621B5 /* Icon.icns */; };
2223
05D11ECA20E13DFF00A621B5 /* ColorSet.AboutWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 05D11EC820E13DFF00A621B5 /* ColorSet.AboutWindowController.xib */; };
@@ -40,6 +41,7 @@
4041
0543AE7F20DC35CC00284E99 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/ColorSet.MainWindowController.xib; sourceTree = "<group>"; };
4142
0543AE8120DC382A00284E99 /* ColorItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorItem.swift; sourceTree = "<group>"; };
4243
0543AE9D20E0FFD000284E99 /* ColorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorView.swift; sourceTree = "<group>"; };
44+
0564590120E4DDFD001EA86F /* HexColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HexColor.swift; sourceTree = "<group>"; };
4345
05D11EC020E1359C00A621B5 /* ColorComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorComponent.swift; sourceTree = "<group>"; };
4446
05D11EC620E13B9400A621B5 /* Icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Icon.icns; sourceTree = "<group>"; };
4547
05D11EC920E13DFF00A621B5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/ColorSet.AboutWindowController.xib; sourceTree = "<group>"; };
@@ -99,6 +101,7 @@
99101
0530C42520E1305B00F5B259 /* Exception.h */,
100102
0530C42620E1305B00F5B259 /* Exception.m */,
101103
05D11EC020E1359C00A621B5 /* ColorComponent.swift */,
104+
0564590120E4DDFD001EA86F /* HexColor.swift */,
102105
0530C42420E1305B00F5B259 /* ColorSet-Bridging-Header.h */,
103106
);
104107
path = Classes;
@@ -197,6 +200,7 @@
197200
files = (
198201
0530C42320E1213500F5B259 /* NSView.swift in Sources */,
199202
05D11ECC20E13E0900A621B5 /* AboutWindowController.swift in Sources */,
203+
0564590220E4DDFD001EA86F /* HexColor.swift in Sources */,
200204
0543AE9E20E0FFD000284E99 /* ColorView.swift in Sources */,
201205
0543AE8220DC382A00284E99 /* ColorItem.swift in Sources */,
202206
0530C41F20E10FEA00F5B259 /* ArrayIsEmpty.swift in Sources */,

ColorSet/Classes/HexColor.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*******************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 Jean-David Gadina - www.imazing.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+
@objc class HexColor: ValueTransformer
28+
{
29+
@objc public override static func allowsReverseTransformation() -> Bool
30+
{
31+
return true
32+
}
33+
34+
@objc public override static func transformedValueClass() -> Swift.AnyClass
35+
{
36+
return NSString.self
37+
}
38+
39+
@objc public override func transformedValue( _ value: Any? ) -> Any?
40+
{
41+
var r: CGFloat = 0
42+
var g: CGFloat = 0
43+
var b: CGFloat = 0
44+
45+
guard let c = value as? NSColor else
46+
{
47+
return "000000"
48+
}
49+
50+
c.usingColorSpace( NSColorSpace.sRGB )?.getRed( &r, green: &g, blue: &b, alpha: nil )
51+
52+
return NSString( format: "%02X%02X%02X", UInt32( r * 255 ), UInt32( g * 255 ), UInt32( b * 255 ) )
53+
}
54+
55+
override func reverseTransformedValue( _ value: Any? ) -> Any?
56+
{
57+
guard let s = value as? NSString else
58+
{
59+
return NSColor.black
60+
}
61+
62+
if( s.length != 6 )
63+
{
64+
return NSColor.black
65+
}
66+
67+
var n: UInt32 = 0
68+
let scanner = Scanner( string: s as String )
69+
70+
scanner.scanHexInt32( &n )
71+
72+
let r = ( n >> 16 ) & 0xFF
73+
let g = ( n >> 8 ) & 0xFF
74+
let b = ( n ) & 0xFF
75+
76+
return NSColor( red: CGFloat( r ) / 255, green: CGFloat( g ) / 255, blue: CGFloat( b ) / 255, alpha: 1 )
77+
}
78+
}

ColorSet/Classes/MainWindowController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class MainWindowController: NSWindowController, NSTableViewDelegate, NSTableView
6767
controller.addObject( color )
6868
}
6969

70+
self.arrayController?.selectsInsertedObjects = true
71+
7072
colorView.bind( NSBindingName( "color" ), to: self, withKeyPath: "selectedColor.color", options: nil )
7173
variantView.bind( NSBindingName( "color" ), to: self, withKeyPath: "selectedColor.variant", options: nil )
7274

0 commit comments

Comments
 (0)