-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathColor.swift
60 lines (49 loc) · 1.51 KB
/
Color.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Color.swift
// JSONHelper
//
// Created by Baris Sencan on 6/29/15.
// Copyright (c) 2015 Baris Sencan. All rights reserved.
//
private struct ColorConversionHelper {
private static func hexStringToRGB(hexString: String) -> (red: CGFloat, green: CGFloat, blue: CGFloat) {
let trimmedString = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
let scanner = NSScanner(string: trimmedString)
if trimmedString.hasPrefix("#") {
scanner.scanLocation = 1
}
var color:UInt32 = 0
scanner.scanHexInt(&color)
let mask = 0x000000FF
let r = Int(color >> 16) & mask
let g = Int(color >> 8) & mask
let b = Int(color) & mask
let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
return (red: red, green: green, blue: blue)
}
}
#if os(iOS)
import UIKit
extension UIColor: Convertible {
public static func convertFromValue(value: Any?) -> Self? {
if let stringValue = value as? String {
let rgb = ColorConversionHelper.hexStringToRGB(stringValue)
return self(red: rgb.red, green: rgb.green, blue: rgb.blue, alpha: 1)
}
return nil
}
}
#else
import AppKit
extension NSColor: Convertible {
public static func convertFromValue(value: Any?) -> Self? {
if let stringValue = value as? String {
let rgb = ColorConversionHelper.hexStringToRGB(stringValue)
return self(red: rgb.red, green: rgb.green, blue: rgb.blue, alpha: 1)
}
return nil
}
}
#endif