Skip to content

Commit 8555c96

Browse files
committed
Support for macOS accent color.
1 parent 73d08ac commit 8555c96

File tree

4 files changed

+85
-34
lines changed

4 files changed

+85
-34
lines changed

ColorSet-Example/Classes/ColorItem.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import ColorSetKit
6767
base.getHue( &h, saturation: &s, lightness: &l, alpha: nil )
6868
base.getRed( &r, green: &g, blue: &b, alpha: nil )
6969

70-
let textColor = ( l > 0.55 ) ? NSColor.black : NSColor.white
70+
let textColor = ( l > 0.65 ) ? NSColor.black : NSColor.white
7171
var p = NSMakePoint( rect.origin.x + 10, rect.origin.y + 10 )
7272

7373
( info.name as NSString ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 25, weight: .thin ) ] )
@@ -82,15 +82,33 @@ import ColorSetKit
8282

8383
( hsl as NSString ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 12, weight: .thin ) ] )
8484

85-
if info.variants.count > 0
85+
var variants = info.variants
86+
87+
if variants.count > 0
8688
{
87-
let width = rect.size.width / CGFloat( info.variants.count )
89+
if let pair = ColorSet.shared[ info.name ]
90+
{
91+
if let color = pair.color
92+
{
93+
variants.append( color.hsl().lightness )
94+
}
95+
96+
if let variant = pair.variant
97+
{
98+
variants.append( variant.hsl().lightness )
99+
}
100+
101+
variants.sort()
102+
variants.reverse()
103+
}
104+
105+
let width = rect.size.width / CGFloat( variants.count )
88106
var r = rect
89107
r.size.width = width
90108
r.size.height = r.size.height / 4
91109
r.origin.y = rect.size.height - r.size.height
92110

93-
for lightness in info.variants
111+
for lightness in variants
94112
{
95113
guard let color = NSColor.from( colorSet: info.name + "." + String( format: "%.0f", lightness * 100 ) ) else
96114
{

ColorSet-Example/Classes/MainWindowController.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,16 @@ class MainWindowController: NSWindowController
6565

6666
private func load()
6767
{
68+
if let accent = ColorSet.shared[ "Accent" ], let color = accent.color
69+
{
70+
ColorSet.shared.add( color: color, variant: accent.variant, lightnesses: accent.lightnesses, forName: "Accent (macOS)" )
71+
ColorSet.shared.useAccentColorForColor( name: "Accent (macOS)" )
72+
}
73+
6874
for p in ColorSet.shared.colors
6975
{
70-
guard let color = p.value.color else
71-
{
72-
continue
73-
}
74-
7576
var variants = [ CGFloat ]()
7677

77-
variants.append( color.hsl().lightness )
78-
79-
if let variant = p.value.variant
80-
{
81-
variants.append( variant.hsl().lightness )
82-
}
83-
8478
for l in p.value.lightnesses
8579
{
8680
variants.append( l.lightness1.lightness )

ColorSet-Example/Interface/Base.lproj/MainWindowController.xib

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
<window title="ColorSet Example" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g">
1919
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
2020
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
21-
<rect key="contentRect" x="196" y="240" width="800" height="555"/>
21+
<rect key="contentRect" x="196" y="240" width="1000" height="555"/>
2222
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1177"/>
23-
<value key="minSize" type="size" width="800" height="555"/>
23+
<value key="minSize" type="size" width="1000" height="555"/>
2424
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
25-
<rect key="frame" x="0.0" y="0.0" width="800" height="555"/>
25+
<rect key="frame" x="0.0" y="0.0" width="1000" height="555"/>
2626
<autoresizingMask key="autoresizingMask"/>
2727
<subviews>
2828
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="FMF-Kc-Uoi">
@@ -34,7 +34,7 @@
3434
</textFieldCell>
3535
</textField>
3636
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="KlT-ll-CnK">
37-
<rect key="frame" x="676" y="504" width="107" height="25"/>
37+
<rect key="frame" x="876" y="504" width="107" height="25"/>
3838
<popUpButtonCell key="cell" type="push" title="Light Mode" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="yiN-GK-d9X" id="U5F-KX-nDC">
3939
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
4040
<font key="font" metaFont="system"/>
@@ -55,16 +55,16 @@
5555
</connections>
5656
</popUpButton>
5757
<scrollView wantsLayer="YES" borderType="none" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qnS-wt-fNA">
58-
<rect key="frame" x="20" y="20" width="760" height="472"/>
58+
<rect key="frame" x="20" y="20" width="960" height="472"/>
5959
<clipView key="contentView" drawsBackground="NO" id="3KV-Ly-R5o">
60-
<rect key="frame" x="0.0" y="0.0" width="760" height="472"/>
60+
<rect key="frame" x="0.0" y="0.0" width="960" height="472"/>
6161
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
6262
<subviews>
6363
<collectionView id="3VM-np-L0Y">
64-
<rect key="frame" x="0.0" y="0.0" width="760" height="158"/>
64+
<rect key="frame" x="0.0" y="0.0" width="960" height="158"/>
6565
<autoresizingMask key="autoresizingMask" widthSizable="YES"/>
66-
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" id="0OQ-0U-cVm">
67-
<size key="itemSize" width="250" height="150"/>
66+
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="5" id="0OQ-0U-cVm">
67+
<size key="itemSize" width="235" height="150"/>
6868
</collectionViewFlowLayout>
6969
<color key="primaryBackgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
7070
<connections>

ColorSetKit/ColorSet.swift

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,33 @@ import Cocoa
5555
return set ?? ColorSet()
5656
}()
5757

58-
public private( set ) var colors = [ String : ColorPair ]()
59-
private var children = [ ColorSet ]()
58+
public private( set ) var colorPairs = [ String : ColorPair ]()
59+
private var children = [ ColorSet ]()
60+
private var accentNames = [ String ]()
61+
62+
@objc public var colors: [ String : ColorPair ]
63+
{
64+
return self.synchronized
65+
{
66+
var colors = [ String : ColorPair ]()
67+
68+
for p in self.colorPairs
69+
{
70+
if let color = self.colorWith( name: p.key )
71+
{
72+
colors[ p.key ] = color
73+
}
74+
}
75+
76+
return colors
77+
}
78+
}
6079

6180
@objc public var count: Int
6281
{
6382
return self.synchronized
6483
{
65-
return self.colors.count
84+
return self.colorPairs.count
6685
}
6786
}
6887

@@ -157,12 +176,32 @@ import Cocoa
157176
return self.colorWith( name: key )
158177
}
159178

179+
@objc public func useAccentColorForColor( name: String )
180+
{
181+
self.synchronized
182+
{
183+
self.accentNames.append( name )
184+
}
185+
}
186+
160187
@objc public func colorWith( name: String ) -> ColorPair?
161188
{
162189
return self.synchronized
163190
{
164-
if let color = self.colors[ name ]
191+
if let color = self.colorPairs[ name ]
165192
{
193+
if #available( macOS 10.14, * )
194+
{
195+
if self.accentNames.contains( name )
196+
{
197+
let accent = NSColor.controlAccentColor.usingColorSpace( .sRGB )
198+
let p = ColorPair( color: accent, variant: ( color.variant != nil ) ? accent : nil )
199+
p.lightnesses = color.lightnesses
200+
201+
return p
202+
}
203+
}
204+
166205
return color
167206
}
168207

@@ -221,7 +260,7 @@ import Cocoa
221260
{
222261
self.synchronized
223262
{
224-
if self.colors.keys.contains( name ) == false
263+
if self.colorPairs.keys.contains( name ) == false
225264
{
226265
self.set( color: color, variant: variant, lightnesses: lightnesses, forName: name )
227266
}
@@ -233,9 +272,9 @@ import Cocoa
233272
{
234273
self.synchronized
235274
{
236-
let p = ColorPair( color: color, variant: variant )
237-
p.lightnesses = lightnesses
238-
self.colors[ name ] = p
275+
let p = ColorPair( color: color, variant: variant )
276+
p.lightnesses = lightnesses
277+
self.colorPairs[ name ] = p
239278
}
240279
}
241280

@@ -247,7 +286,7 @@ import Cocoa
247286

248287
self.synchronized
249288
{
250-
colors = self.colors
289+
colors = self.colorPairs
251290
}
252291

253292
let stream = ColorSetStream()

0 commit comments

Comments
 (0)