Skip to content

Commit 4da9444

Browse files
committed
Support for # or 0x prefix on hex colors.
1 parent ff5a706 commit 4da9444

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

ColorSet/Classes/HexColor.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,30 @@ import Cocoa
4444

4545
guard let c = value as? NSColor else
4646
{
47-
return "000000"
47+
return "#000000"
4848
}
4949

5050
c.usingColorSpace( NSColorSpace.sRGB )?.getRed( &r, green: &g, blue: &b, alpha: nil )
5151

52-
return NSString( format: "%02X%02X%02X", UInt32( r * 255 ), UInt32( g * 255 ), UInt32( b * 255 ) )
52+
return NSString( format: "#%02X%02X%02X", UInt32( r * 255 ), UInt32( g * 255 ), UInt32( b * 255 ) )
5353
}
5454

5555
override func reverseTransformedValue( _ value: Any? ) -> Any?
5656
{
57-
guard let s = value as? NSString else
57+
guard var s = value as? NSString else
5858
{
5959
return NSColor.black
6060
}
6161

62+
if( s.hasPrefix( "#" ) )
63+
{
64+
s = s.substring( from: 1 ) as NSString
65+
}
66+
else if( s.hasPrefix( "0x" ) )
67+
{
68+
s = s.substring( from: 2 ) as NSString
69+
}
70+
6271
if( s.length != 6 )
6372
{
6473
return NSColor.black

0 commit comments

Comments
 (0)