Skip to content

Commit dc6195f

Browse files
committed
Color palette preview
1 parent 990c14b commit dc6195f

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

ColorSet/Classes/PaletteColorItem.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,65 @@ import ColorSetKit
7979
p.y += 15
8080

8181
( hslString as NSString ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 12, weight: .thin ) ] )
82+
83+
var lightnesses = [ ( CGFloat, NSColor ) ]()
84+
85+
for lp in info.colorPair.lightnesses
86+
{
87+
let l1 = lp.lightness1.lightness
88+
let l2 = lp.lightness2.lightness
89+
var c1 = base.byChangingLightness( l1 )
90+
var c2 = base.byChangingLightness( l2 )
91+
92+
if #available( macOS 10.14, * )
93+
{
94+
if NSApp.effectiveAppearance.name == .darkAqua
95+
{
96+
swap( &c1, &c2 )
97+
}
98+
}
99+
100+
lightnesses.append( ( l1, c1 ) )
101+
lightnesses.append( ( l2, c2 ) )
102+
}
103+
104+
if let color = info.colorPair.color
105+
{
106+
lightnesses.append( ( color.hsl().lightness, color ) )
107+
}
108+
109+
if let variant = info.colorPair.variant
110+
{
111+
lightnesses.append( ( variant.hsl().lightness, variant ) )
112+
}
113+
114+
lightnesses.sort
115+
{
116+
l1, l2 in l1.0 > l2.0
117+
}
118+
119+
if lightnesses.count > 0
120+
{
121+
let width = rect.size.width / CGFloat( lightnesses.count )
122+
var r = rect
123+
r.size.width = width
124+
r.size.height = r.size.height / 4
125+
r.origin.y = rect.size.height - r.size.height
126+
127+
for lightness in lightnesses
128+
{
129+
let color = lightness.1
130+
131+
color.setFill()
132+
r.fill()
133+
134+
let textColor = NSColor.bestTextColorForBackgroundColor( color )
135+
let p = NSMakePoint( r.origin.x - 8 + ( r.size.width / 2 ), r.origin.y - 5 + ( r.size.height / 2 ) )
136+
137+
String( format: "%.0f", lightness.0 * 100 ).draw( at: p, withAttributes: [ .foregroundColor : textColor, .font : NSFont.systemFont( ofSize: 10, weight: .thin ) ] )
138+
139+
r.origin.x += width
140+
}
141+
}
82142
}
83143
}

ColorSet/Interface/Base.lproj/ColorSet.PaletteViewController.xib

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@
4444
</connections>
4545
</popUpButton>
4646
<scrollView wantsLayer="YES" borderType="none" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="4Gb-Td-Ua6">
47-
<rect key="frame" x="20" y="20" width="614" height="382"/>
47+
<rect key="frame" x="20" y="20" width="614" height="370"/>
4848
<clipView key="contentView" drawsBackground="NO" id="uSH-1x-Kb0">
49-
<rect key="frame" x="0.0" y="0.0" width="614" height="382"/>
49+
<rect key="frame" x="0.0" y="0.0" width="614" height="370"/>
5050
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
5151
<subviews>
5252
<collectionView id="Wvb-SF-Ze4">
5353
<rect key="frame" x="0.0" y="0.0" width="614" height="158"/>
5454
<autoresizingMask key="autoresizingMask" widthSizable="YES"/>
55-
<collectionViewFlowLayout key="collectionViewLayout" id="35z-f8-tya">
56-
<size key="itemSize" width="235" height="150"/>
55+
<collectionViewFlowLayout key="collectionViewLayout" minimumInteritemSpacing="10" minimumLineSpacing="10" id="35z-f8-tya">
56+
<size key="itemSize" width="225" height="150"/>
5757
</collectionViewFlowLayout>
5858
<color key="primaryBackgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
5959
<connections>
@@ -81,7 +81,7 @@
8181
<constraint firstItem="eUV-TI-EqP" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="20" id="bb2-Vp-8RP"/>
8282
<constraint firstItem="4Gb-Td-Ua6" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="20" id="h7J-d1-epS"/>
8383
<constraint firstAttribute="trailing" secondItem="YZr-ib-2Fa" secondAttribute="trailing" constant="20" id="loG-Hr-8KB"/>
84-
<constraint firstItem="4Gb-Td-Ua6" firstAttribute="top" secondItem="eUV-TI-EqP" secondAttribute="bottom" constant="8" id="z8Z-P8-lhg"/>
84+
<constraint firstItem="4Gb-Td-Ua6" firstAttribute="top" secondItem="eUV-TI-EqP" secondAttribute="bottom" constant="20" id="z8Z-P8-lhg"/>
8585
</constraints>
8686
<point key="canvasLocation" x="223" y="57.5"/>
8787
</customView>

ColorSetKit/NSColor+ColorSetKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ import Cocoa
159159

160160
let contrast = ( ( rgb.red * 299 ) + ( rgb.green * 587 ) + ( rgb.blue * 114 ) ) / 1000
161161

162-
return ( contrast > 123 ) ? darkTextColor : lightTextColor
162+
return ( contrast > 150 ) ? darkTextColor : lightTextColor
163163
}
164164

165165
@objc var hexString: String

0 commit comments

Comments
 (0)