1+ #if canImport(SwiftUI) && canImport(ObjectiveC)
2+
13import XCTest
24
35@testable import SnapshotTesting
46
57#if os(iOS)
6- let platform = " ios "
8+ let platform = " ios "
79#elseif os(tvOS)
8- let platform = " tvos "
10+ let platform = " tvos "
911#elseif os(macOS)
10- let platform = " macos "
11- extension NSTextField {
12- var text : String {
13- get { return self . stringValue }
14- set { self . stringValue = newValue }
15- }
12+ let platform = " macos "
13+ extension NSTextField {
14+ var text : String {
15+ get { return self . stringValue }
16+ set { self . stringValue = newValue }
1617 }
18+ }
1719#endif
1820
1921#if os(macOS) || os(iOS) || os(tvOS)
20- extension CGPath {
21- /// Creates an approximation of a heart at a 45º angle with a circle above, using all available element types:
22- static var heart : CGPath {
23- let scale : CGFloat = 30.0
24- let path = CGMutablePath ( )
25-
26- path. move ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
27- path. addLine ( to: CGPoint ( x: 0.0 * scale, y: 2.0 * scale) )
28- path. addQuadCurve (
29- to: CGPoint ( x: 1.0 * scale, y: 3.0 * scale) ,
30- control: CGPoint ( x: 0.125 * scale, y: 2.875 * scale)
31- )
32- path. addQuadCurve (
33- to: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
34- control: CGPoint ( x: 1.875 * scale, y: 2.875 * scale)
35- )
36- path. addCurve (
37- to: CGPoint ( x: 3.0 * scale, y: 1.0 * scale) ,
38- control1: CGPoint ( x: 2.5 * scale, y: 2.0 * scale) ,
39- control2: CGPoint ( x: 3.0 * scale, y: 1.5 * scale)
40- )
41- path. addCurve (
42- to: CGPoint ( x: 2.0 * scale, y: 0.0 * scale) ,
43- control1: CGPoint ( x: 3.0 * scale, y: 0.5 * scale) ,
44- control2: CGPoint ( x: 2.5 * scale, y: 0.0 * scale)
45- )
46- path. addLine ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
47- path. closeSubpath ( )
48-
49- path. addEllipse (
50- in: CGRect (
51- origin: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
52- size: CGSize ( width: scale, height: scale)
53- ) )
54-
55- return path
56- }
22+ extension CGPath {
23+ /// Creates an approximation of a heart at a 45º angle with a circle above, using all available element types:
24+ static var heart : CGPath {
25+ let scale : CGFloat = 30.0
26+ let path = CGMutablePath ( )
27+
28+ path. move ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
29+ path. addLine ( to: CGPoint ( x: 0.0 * scale, y: 2.0 * scale) )
30+ path. addQuadCurve (
31+ to: CGPoint ( x: 1.0 * scale, y: 3.0 * scale) ,
32+ control: CGPoint ( x: 0.125 * scale, y: 2.875 * scale)
33+ )
34+ path. addQuadCurve (
35+ to: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
36+ control: CGPoint ( x: 1.875 * scale, y: 2.875 * scale)
37+ )
38+ path. addCurve (
39+ to: CGPoint ( x: 3.0 * scale, y: 1.0 * scale) ,
40+ control1: CGPoint ( x: 2.5 * scale, y: 2.0 * scale) ,
41+ control2: CGPoint ( x: 3.0 * scale, y: 1.5 * scale)
42+ )
43+ path. addCurve (
44+ to: CGPoint ( x: 2.0 * scale, y: 0.0 * scale) ,
45+ control1: CGPoint ( x: 3.0 * scale, y: 0.5 * scale) ,
46+ control2: CGPoint ( x: 2.5 * scale, y: 0.0 * scale)
47+ )
48+ path. addLine ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
49+ path. closeSubpath ( )
50+
51+ path. addEllipse (
52+ in: CGRect (
53+ origin: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
54+ size: CGSize ( width: scale, height: scale)
55+ ) )
56+
57+ return path
5758 }
59+ }
5860#endif
5961
6062#if os(iOS) || os(tvOS)
61- extension UIBezierPath {
62- /// Creates an approximation of a heart at a 45º angle with a circle above, using all available element types:
63- static var heart : UIBezierPath {
64- UIBezierPath ( cgPath: . heart)
65- }
63+ extension UIBezierPath {
64+ /// Creates an approximation of a heart at a 45º angle with a circle above, using all available element types:
65+ static var heart : UIBezierPath {
66+ UIBezierPath ( cgPath: . heart)
6667 }
68+ }
6769#endif
6870
6971#if os(macOS)
70- extension NSBezierPath {
71- /// Creates an approximation of a heart at a 45º angle with a circle above, using all available element types:
72- static var heart : NSBezierPath {
73- let scale : CGFloat = 30.0
74- let path = NSBezierPath ( )
75-
76- path. move ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
77- path. line ( to: CGPoint ( x: 0.0 * scale, y: 2.0 * scale) )
78- path. curve (
79- to: CGPoint ( x: 1.0 * scale, y: 3.0 * scale) ,
80- controlPoint1: CGPoint ( x: 0.0 * scale, y: 2.5 * scale) ,
81- controlPoint2: CGPoint ( x: 0.5 * scale, y: 3.0 * scale)
82- )
83- path. curve (
84- to: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
85- controlPoint1: CGPoint ( x: 1.5 * scale, y: 3.0 * scale) ,
86- controlPoint2: CGPoint ( x: 2.0 * scale, y: 2.5 * scale)
87- )
88- path. curve (
89- to: CGPoint ( x: 3.0 * scale, y: 1.0 * scale) ,
90- controlPoint1: CGPoint ( x: 2.5 * scale, y: 2.0 * scale) ,
91- controlPoint2: CGPoint ( x: 3.0 * scale, y: 1.5 * scale)
92- )
93- path. curve (
94- to: CGPoint ( x: 2.0 * scale, y: 0.0 * scale) ,
95- controlPoint1: CGPoint ( x: 3.0 * scale, y: 0.5 * scale) ,
96- controlPoint2: CGPoint ( x: 2.5 * scale, y: 0.0 * scale)
97- )
98- path. line ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
99- path. close ( )
100-
101- path. appendOval (
102- in: CGRect (
103- origin: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
104- size: CGSize ( width: scale, height: scale)
105- ) )
106-
107- return path
108- }
72+ extension NSBezierPath {
73+ /// Creates an approximation of a heart at a 45º angle with a circle above, using all available element types:
74+ static var heart : NSBezierPath {
75+ let scale : CGFloat = 30.0
76+ let path = NSBezierPath ( )
77+
78+ path. move ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
79+ path. line ( to: CGPoint ( x: 0.0 * scale, y: 2.0 * scale) )
80+ path. curve (
81+ to: CGPoint ( x: 1.0 * scale, y: 3.0 * scale) ,
82+ controlPoint1: CGPoint ( x: 0.0 * scale, y: 2.5 * scale) ,
83+ controlPoint2: CGPoint ( x: 0.5 * scale, y: 3.0 * scale)
84+ )
85+ path. curve (
86+ to: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
87+ controlPoint1: CGPoint ( x: 1.5 * scale, y: 3.0 * scale) ,
88+ controlPoint2: CGPoint ( x: 2.0 * scale, y: 2.5 * scale)
89+ )
90+ path. curve (
91+ to: CGPoint ( x: 3.0 * scale, y: 1.0 * scale) ,
92+ controlPoint1: CGPoint ( x: 2.5 * scale, y: 2.0 * scale) ,
93+ controlPoint2: CGPoint ( x: 3.0 * scale, y: 1.5 * scale)
94+ )
95+ path. curve (
96+ to: CGPoint ( x: 2.0 * scale, y: 0.0 * scale) ,
97+ controlPoint1: CGPoint ( x: 3.0 * scale, y: 0.5 * scale) ,
98+ controlPoint2: CGPoint ( x: 2.5 * scale, y: 0.0 * scale)
99+ )
100+ path. line ( to: CGPoint ( x: 0.0 * scale, y: 0.0 * scale) )
101+ path. close ( )
102+
103+ path. appendOval (
104+ in: CGRect (
105+ origin: CGPoint ( x: 2.0 * scale, y: 2.0 * scale) ,
106+ size: CGSize ( width: scale, height: scale)
107+ ) )
108+
109+ return path
109110 }
111+ }
110112#endif
111113
112114#if canImport(UIKit)
113115import UIKit
114116
115117func _getFirstPixelColorHex( from image: UIImage ) -> String ? {
116- guard let cgImage = image. cgImage else { return nil }
117-
118- let pixelData = calloc ( 1 , 4 ) // 4 bytes for RGBA
119- let colorSpace = CGColorSpaceCreateDeviceRGB ( )
120- let bitmapInfo = CGImageAlphaInfo . premultipliedLast. rawValue
121-
122- guard let context = CGContext (
123- data: pixelData,
124- width: 1 ,
125- height: 1 ,
126- bitsPerComponent: 8 ,
127- bytesPerRow: 4 ,
128- space: colorSpace,
129- bitmapInfo: bitmapInfo
130- ) else {
131- free ( pixelData)
132- return nil
133- }
134-
135- // Draw the image in the 1x1 context to get the first pixel's color
136- context. draw ( cgImage, in: CGRect ( x: 0 , y: 0 , width: 1 , height: 1 ) )
137-
138- // Get the color components
139- let data = pixelData!. assumingMemoryBound ( to: UInt8 . self)
140- let r = data [ 0 ]
141- let g = data [ 1 ]
142- let b = data [ 2 ]
143- let a = data [ 3 ]
144-
118+ guard let cgImage = image. cgImage else { return nil }
119+
120+ let pixelData = calloc ( 1 , 4 ) // 4 bytes for RGBA
121+ let colorSpace = CGColorSpaceCreateDeviceRGB ( )
122+ let bitmapInfo = CGImageAlphaInfo . premultipliedLast. rawValue
123+
124+ guard let context = CGContext (
125+ data: pixelData,
126+ width: 1 ,
127+ height: 1 ,
128+ bitsPerComponent: 8 ,
129+ bytesPerRow: 4 ,
130+ space: colorSpace,
131+ bitmapInfo: bitmapInfo
132+ ) else {
145133 free ( pixelData)
146-
147- // Return the hex string
148- return String ( format: " #%02X%02X%02X%02X " , r, g, b, a)
134+ return nil
135+ }
136+
137+ // Draw the image in the 1x1 context to get the first pixel's color
138+ context. draw ( cgImage, in: CGRect ( x: 0 , y: 0 , width: 1 , height: 1 ) )
139+
140+ // Get the color components
141+ let data = pixelData!. assumingMemoryBound ( to: UInt8 . self)
142+ let r = data [ 0 ]
143+ let g = data [ 1 ]
144+ let b = data [ 2 ]
145+ let a = data [ 3 ]
146+
147+ free ( pixelData)
148+
149+ // Return the hex string
150+ return String ( format: " #%02X%02X%02X%02X " , r, g, b, a)
149151}
150152#endif
151153
152154#if canImport(AppKit)
153155import AppKit
154156
155157func _getFirstPixelColorHex( from image: NSImage ) -> String ? {
156- guard let cgImage = image. cgImage ( forProposedRect: nil , context: nil , hints: nil ) else { return nil }
157-
158- let pixelData = calloc ( 1 , 4 ) // 4 bytes for RGBA
159- let colorSpace = CGColorSpaceCreateDeviceRGB ( )
160- let bitmapInfo = CGImageAlphaInfo . premultipliedLast. rawValue
161-
162- guard let context = CGContext (
163- data: pixelData,
164- width: 1 ,
165- height: 1 ,
166- bitsPerComponent: 8 ,
167- bytesPerRow: 4 ,
168- space: colorSpace,
169- bitmapInfo: bitmapInfo
170- ) else {
171- free ( pixelData)
172- return nil
173- }
174-
175- // Draw the image in the 1x1 context to get the first pixel's color
176- context. draw ( cgImage, in: CGRect ( x: 0 , y: 0 , width: 1 , height: 1 ) )
177-
178- // Get the color components
179- let data = pixelData!. assumingMemoryBound ( to: UInt8 . self)
180- let r = data [ 0 ]
181- let g = data [ 1 ]
182- let b = data [ 2 ]
183- let a = data [ 3 ]
184-
158+ guard let cgImage = image. cgImage ( forProposedRect: nil , context: nil , hints: nil ) else { return nil }
159+
160+ let pixelData = calloc ( 1 , 4 ) // 4 bytes for RGBA
161+ let colorSpace = CGColorSpaceCreateDeviceRGB ( )
162+ let bitmapInfo = CGImageAlphaInfo . premultipliedLast. rawValue
163+
164+ guard let context = CGContext (
165+ data: pixelData,
166+ width: 1 ,
167+ height: 1 ,
168+ bitsPerComponent: 8 ,
169+ bytesPerRow: 4 ,
170+ space: colorSpace,
171+ bitmapInfo: bitmapInfo
172+ ) else {
185173 free ( pixelData)
186-
187- // Return the hex string
188- return String ( format: " #%02X%02X%02X%02X " , r, g, b, a)
174+ return nil
175+ }
176+
177+ // Draw the image in the 1x1 context to get the first pixel's color
178+ context. draw ( cgImage, in: CGRect ( x: 0 , y: 0 , width: 1 , height: 1 ) )
179+
180+ // Get the color components
181+ let data = pixelData!. assumingMemoryBound ( to: UInt8 . self)
182+ let r = data [ 0 ]
183+ let g = data [ 1 ]
184+ let b = data [ 2 ]
185+ let a = data [ 3 ]
186+
187+ free ( pixelData)
188+
189+ // Return the hex string
190+ return String ( format: " #%02X%02X%02X%02X " , r, g, b, a)
189191}
190192#endif
191193
@@ -197,8 +199,9 @@ typealias SnapImage = NSImage
197199
198200func getFirstPixelColorHex( from image: SnapImage ) -> String ? {
199201#if canImport(UIKit)
200- return _getFirstPixelColorHex ( from: image)
202+ return _getFirstPixelColorHex ( from: image)
201203#elseif canImport(AppKit)
202- return _getFirstPixelColorHex ( from: image)
204+ return _getFirstPixelColorHex ( from: image)
203205#endif
204206}
207+ #endif
0 commit comments