@@ -328,25 +328,25 @@ extension StringProtocol {
328328 public subscript( _ offset: Int ) -> Element {
329329 self [ index ( startIndex, offsetBy: offset) ]
330330 }
331-
331+
332332 public subscript( _ range: Range < Int > ) -> SubSequence {
333- prefix ( range. lowerBound+ range. count) . suffix ( range. count)
333+ prefix ( range. lowerBound + range. count) . suffix ( range. count)
334334 }
335-
335+
336336 public subscript( _ range: ClosedRange < Int > ) -> SubSequence {
337- prefix ( range. lowerBound+ range. count) . suffix ( range. count)
337+ prefix ( range. lowerBound + range. count) . suffix ( range. count)
338338 }
339-
339+
340340 public subscript( _ range: PartialRangeThrough < Int > ) -> SubSequence {
341341 prefix ( range. upperBound. advanced ( by: 1 ) )
342342 }
343-
343+
344344 public subscript( _ range: PartialRangeUpTo < Int > ) -> SubSequence {
345345 prefix ( range. upperBound)
346346 }
347-
347+
348348 public subscript( _ range: PartialRangeFrom < Int > ) -> SubSequence {
349- suffix ( Swift . max ( 0 , count- range. lowerBound) )
349+ suffix ( Swift . max ( 0 , count - range. lowerBound) )
350350 }
351351}
352352
@@ -361,7 +361,6 @@ extension BidirectionalCollection {
361361 }
362362}
363363
364-
365364extension URL {
366365 @discardableResult
367366 public func trimmingQuery( ) -> String ? {
@@ -751,16 +750,72 @@ extension UIImage {
751750}
752751
753752extension UIColor {
753+ @discardableResult
754+
754755 public class func randomColor( ) -> UIColor {
755756 let red = arc4random_uniform ( 256 )
756757 let green = arc4random_uniform ( 256 )
757758 let blue = arc4random_uniform ( 256 )
758759 return CRRGBA ( r: Float ( red) , g: Float ( green) , b: Float ( blue) )
759760 }
760761
761- // MARK: - color from {R, G, B}
762+ @discardableResult
763+ public func hexString( _ alpha: Bool = false ) -> String {
764+ let comps = cgColor. components!
765+ let r = Int ( comps [ 0 ] * 255 )
766+ let g = Int ( comps [ 1 ] * 255 )
767+ let b = Int ( comps [ 2 ] * 255 )
768+ let a = Int ( comps [ 3 ] * 255 )
769+ var hexString : String = " # "
770+
771+ hexString += String ( format: " %02X%02X%02X " , r, g, b)
772+ if alpha {
773+ hexString += String ( format: " %02X " , a)
774+ }
775+ return hexString
776+ }
762777
763- public class func colorWithString( string: String ) -> UIColor {
764- return UIColor ( )
778+ @discardableResult
779+ public class func colorWithHex( hex: String , _ alpha: CGFloat = 1 ) -> UIColor ? {
780+ var hexString = hex
781+ if hex. hasPrefix ( " # " ) {
782+ let index = hex. index ( hex. startIndex, offsetBy: 1 )
783+ hexString = String ( hex [ index... ] )
784+ }
785+ var red : CGFloat = 0.0
786+ var green : CGFloat = 0.0
787+ var blue : CGFloat = 0.0
788+ var alpha = alpha
789+ let scanner = Scanner ( string: hexString)
790+ var hexValue : CUnsignedLongLong = 0
791+ if scanner. scanHexInt64 ( & hexValue) {
792+ switch hexString. count {
793+ case 3 :
794+ red = CGFloat ( ( hexValue & 0xF00 ) >> 8 ) / 15.0
795+ green = CGFloat ( ( hexValue & 0x0F0 ) >> 4 ) / 15.0
796+ blue = CGFloat ( hexValue & 0x00F ) / 15.0
797+ case 4 :
798+ red = CGFloat ( ( hexValue & 0xF000 ) >> 12 ) / 15.0
799+ green = CGFloat ( ( hexValue & 0x0F00 ) >> 8 ) / 15.0
800+ blue = CGFloat ( ( hexValue & 0x00F0 ) >> 4 ) / 15.0
801+ alpha = CGFloat ( hexValue & 0x000F ) / 15.0
802+ case 6 :
803+ red = CGFloat ( ( hexValue & 0xFF0000 ) >> 16 ) / 255.0
804+ green = CGFloat ( ( hexValue & 0x00FF00 ) >> 8 ) / 255.0
805+ blue = CGFloat ( hexValue & 0x0000FF ) / 255.0
806+ case 8 :
807+ red = CGFloat ( ( hexValue & 0xFF000000 ) >> 24 ) / 255.0
808+ green = CGFloat ( ( hexValue & 0x00FF0000 ) >> 16 ) / 255.0
809+ blue = CGFloat ( ( hexValue & 0x0000FF00 ) >> 8 ) / 255.0
810+ alpha = CGFloat ( hexValue & 0x000000FF ) / 255.0
811+ default :
812+ // Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8
813+ return nil
814+ }
815+ } else {
816+ // "Scan hex error
817+ return nil
818+ }
819+ return UIColor ( red: red, green: green, blue: blue, alpha: alpha)
765820 }
766821}
0 commit comments