|
| 1 | +/******************************************************************************* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Jean-David Gadina - www.imazing.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +import Cocoa |
| 26 | + |
| 27 | +@objc class HexColor: ValueTransformer |
| 28 | +{ |
| 29 | + @objc public override static func allowsReverseTransformation() -> Bool |
| 30 | + { |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + @objc public override static func transformedValueClass() -> Swift.AnyClass |
| 35 | + { |
| 36 | + return NSString.self |
| 37 | + } |
| 38 | + |
| 39 | + @objc public override func transformedValue( _ value: Any? ) -> Any? |
| 40 | + { |
| 41 | + var r: CGFloat = 0 |
| 42 | + var g: CGFloat = 0 |
| 43 | + var b: CGFloat = 0 |
| 44 | + |
| 45 | + guard let c = value as? NSColor else |
| 46 | + { |
| 47 | + return "000000" |
| 48 | + } |
| 49 | + |
| 50 | + c.usingColorSpace( NSColorSpace.sRGB )?.getRed( &r, green: &g, blue: &b, alpha: nil ) |
| 51 | + |
| 52 | + return NSString( format: "%02X%02X%02X", UInt32( r * 255 ), UInt32( g * 255 ), UInt32( b * 255 ) ) |
| 53 | + } |
| 54 | + |
| 55 | + override func reverseTransformedValue( _ value: Any? ) -> Any? |
| 56 | + { |
| 57 | + guard let s = value as? NSString else |
| 58 | + { |
| 59 | + return NSColor.black |
| 60 | + } |
| 61 | + |
| 62 | + if( s.length != 6 ) |
| 63 | + { |
| 64 | + return NSColor.black |
| 65 | + } |
| 66 | + |
| 67 | + var n: UInt32 = 0 |
| 68 | + let scanner = Scanner( string: s as String ) |
| 69 | + |
| 70 | + scanner.scanHexInt32( &n ) |
| 71 | + |
| 72 | + let r = ( n >> 16 ) & 0xFF |
| 73 | + let g = ( n >> 8 ) & 0xFF |
| 74 | + let b = ( n ) & 0xFF |
| 75 | + |
| 76 | + return NSColor( red: CGFloat( r ) / 255, green: CGFloat( g ) / 255, blue: CGFloat( b ) / 255, alpha: 1 ) |
| 77 | + } |
| 78 | +} |
0 commit comments