0001    //  SwiftHEXColors.swift
0002    //
0003    // Copyright (c) 2014 Doan Truong Thi
0004    //
0005    // Permission is hereby granted, free of charge, to any person obtaining a copy
0006    // of this software and associated documentation files (the "Software"), to deal
0007    // in the Software without restriction, including without limitation the rights
0008    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009    // copies of the Software, and to permit persons to whom the Software is
0010    // furnished to do so, subject to the following conditions:
0011    //
0012    // The above copyright notice and this permission notice shall be included in
0013    // all copies or substantial portions of the Software.
0014    //
0015    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0021    // THE SOFTWARE.
0022    
0023    #if os(iOS) || os(tvOS)
0024    	import UIKit
0025    	typealias SWColor = UIColor
0026    #else
0027    	import Cocoa
0028    	typealias SWColor
SwiftHEXColors.swift:32
public extension SWColor {
= NSColor 0029 #endif 0030 0031 /// An extension of UIColor (on iOS) or NSColor (on OSX) providing HEX color handling. 0032 public extension SWColor { 0033 /** 0034 Create non-autoreleased color with in the given hex string. Alpha will be set as 1 by default. 0035 0036 - parameter hexString: The hex string, with or without the hash character. 0037 - returns: A color with the given hex string. 0038 */ 0039 public convenience init?(hexString: String) { 0040 self.init(hexString: hexString, alpha: 1.0) 0041 } 0042 0043 /** 0044 Create non-autoreleased color with in the given hex string and alpha. 0045 0046 - parameter hexString: The hex string, with or without the hash character. 0047 - parameter alpha: The alpha value, a floating value between 0 and 1. 0048 - returns: A color with the given hex string and alpha. 0049 */ 0050 public convenience init
SwiftHEXColors.swift:40
		self.init(hexString: hexString, alpha: 1.0)
SwiftHEXColors.swift:114
		self.init(hexString: hexString as String , alpha: alpha)
?(hexString: String, alpha: Float) { 0051 var hex = hexString 0052 0053 // Check for hash and remove the hash 0054 if hex.hasPrefix("#") { 0055 hex = hex.substringFromIndex(hex.startIndex.advancedBy(1)) 0056 } 0057 0058 if (hex.rangeOfString("(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)", options: .RegularExpressionSearch) != nil) { 0059 0060 // Deal with 3 character Hex strings 0061 if hex.characters.count == 3 { 0062 let redHex = hex.substringToIndex(hex.startIndex.advancedBy(1)) 0063 let greenHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(1), end: hex.startIndex.advancedBy(2))) 0064 let blueHex = hex.substringFromIndex(hex.startIndex.advancedBy(2)) 0065 0066 hex = redHex + redHex + greenHex + greenHex + blueHex + blueHex 0067 } 0068 0069 let redHex = hex.substringToIndex(hex.startIndex.advancedBy(2)) 0070 let greenHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(2), end: hex.startIndex.advancedBy(4))) 0071 let blueHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(4), end: hex.startIndex.advancedBy(6))) 0072 0073 var redInt: CUnsignedInt = 0 0074 var greenInt: CUnsignedInt = 0 0075 var blueInt: CUnsignedInt = 0 0076 0077 NSScanner(string: redHex).scanHexInt(&redInt) 0078 NSScanner(string: greenHex).scanHexInt(&greenInt) 0079 NSScanner(string: blueHex).scanHexInt(&blueInt) 0080 0081 self.init(red: CGFloat(redInt) / 255.0, green: CGFloat(greenInt) / 255.0, blue: CGFloat(blueInt) / 255.0, alpha: CGFloat(alpha)) 0082 } 0083 else { 0084 // Note: 0085 // The swift 1.1 compiler is currently unable to destroy partially initialized classes in all cases, 0086 // so it disallows formation of a situation where it would have to. We consider this a bug to be fixed 0087 // in future releases, not a feature. -- Apple Forum 0088 self.init() 0089 return nil 0090 } 0091 } 0092 0093 /** 0094 Create non-autoreleased color with in the given hex value. Alpha will be set as 1 by default. 0095 0096 - parameter hex: The hex value. For example: 0xff8942 (no quotation). 0097 - returns: A color with the given hex value 0098 */ 0099 public convenience init?(hex: Int) { 0100 self.init(hex: hex, alpha: 1.0) 0101 } 0102 0103 /** 0104 Create non-autoreleased color with in the given hex value and alpha 0105 0106 - parameter hex: The hex value. For example: 0xff8942 (no quotation). 0107 - parameter alpha: The alpha value, a floating value between 0 and 1. 0108 - returns: color with the given hex value and alpha 0109 */ 0110 public convenience init
SwiftHEXColors.swift:100
		self.init(hex: hex, alpha: 1.0)
?(hex: Int, alpha: Float) { 0111 var hexString = String(format: "%2X", hex) 0112 let leadingZerosString = String(count: 6 - hexString.characters.count, repeatedValue: Character("0")) 0113 hexString = leadingZerosString + hexString 0114 self.init(hexString: hexString as String , alpha: alpha) 0115 } 0116 } 0117