From 02a97e99c6efa535734ac1e11bf8380c29c5122d Mon Sep 17 00:00:00 2001 From: Martin Rechsteiner Date: Tue, 15 Dec 2020 20:53:19 +0100 Subject: [PATCH] Fix crash when using UIColor(patternImage:) Colors initialized with UIColor(patternImage:) have only 1 which crashes the color interpolation. Since the color can contain any type of image we just default to black. --- Parchment.xcodeproj/project.pbxproj | 8 ++++++++ .../Extensions/UIColor+interpolation.swift | 4 +++- .../Resources.xcassets/Contents.json | 6 ++++++ .../Green.imageset/Contents.json | 12 ++++++++++++ .../Green.imageset/Green.png | Bin 0 -> 139 bytes ParchmentTests/UIColorInterpolationTests.swift | 17 +++++++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ParchmentTests/Resources.xcassets/Contents.json create mode 100644 ParchmentTests/Resources.xcassets/Green.imageset/Contents.json create mode 100644 ParchmentTests/Resources.xcassets/Green.imageset/Green.png create mode 100644 ParchmentTests/UIColorInterpolationTests.swift diff --git a/Parchment.xcodeproj/project.pbxproj b/Parchment.xcodeproj/project.pbxproj index 9a0c0ad9..7f98a074 100644 --- a/Parchment.xcodeproj/project.pbxproj +++ b/Parchment.xcodeproj/project.pbxproj @@ -81,6 +81,8 @@ 95591F21222C3A0400677B4B /* PagingControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95591F20222C3A0400677B4B /* PagingControllerTests.swift */; }; 95591F23222C522800677B4B /* PagingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95591F22222C522800677B4B /* PagingController.swift */; }; 955D02442434E56900416E71 /* PagingViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955D02432434E56900416E71 /* PagingViewTests.swift */; }; + 9566F55F257982B500CCA8FC /* UIColorInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9566F55E257982B500CCA8FC /* UIColorInterpolationTests.swift */; }; + 9566F567257983B200CCA8FC /* Resources.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9566F566257983B200CCA8FC /* Resources.xcassets */; }; 9568922B222C525C00AFF250 /* CollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9568922A222C525C00AFF250 /* CollectionView.swift */; }; 956EBE5E248BC426003ED4BA /* PagingCollectionViewLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 956EBE5D248BC426003ED4BA /* PagingCollectionViewLayoutTests.swift */; }; 9575BEB32461FEF9002403F6 /* CreateDistance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9575BEB22461FEF9002403F6 /* CreateDistance.swift */; }; @@ -272,6 +274,8 @@ 95591F20222C3A0400677B4B /* PagingControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingControllerTests.swift; sourceTree = ""; }; 95591F22222C522800677B4B /* PagingController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingController.swift; sourceTree = ""; }; 955D02432434E56900416E71 /* PagingViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingViewTests.swift; sourceTree = ""; }; + 9566F55E257982B500CCA8FC /* UIColorInterpolationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorInterpolationTests.swift; sourceTree = ""; }; + 9566F566257983B200CCA8FC /* Resources.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Resources.xcassets; sourceTree = ""; }; 9568922A222C525C00AFF250 /* CollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionView.swift; sourceTree = ""; }; 956EBE5D248BC426003ED4BA /* PagingCollectionViewLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingCollectionViewLayoutTests.swift; sourceTree = ""; }; 9575BEB22461FEF9002403F6 /* CreateDistance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateDistance.swift; sourceTree = ""; }; @@ -454,8 +458,10 @@ 3E5E93E21CE7D899000762A1 /* PagingStateTests.swift */, 954842601F4251F90072038C /* PagingViewControllerTests.swift */, 955D02432434E56900416E71 /* PagingViewTests.swift */, + 9566F55E257982B500CCA8FC /* UIColorInterpolationTests.swift */, 95D78FE5228728FD00E6EE7C /* Mocks */, 9575BEB12461FEE3002403F6 /* Utilities */, + 9566F566257983B200CCA8FC /* Resources.xcassets */, ); path = ParchmentTests; sourceTree = ""; @@ -920,6 +926,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9566F567257983B200CCA8FC /* Resources.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -981,6 +988,7 @@ 95FEEA4524215FCA009B5B64 /* PageViewManagerTests.swift in Sources */, 95D78FDB2287138F00E6EE7C /* MockCollectionView.swift in Sources */, 9575BEB72463490B002403F6 /* PagingDistanceCenteredTests.swift in Sources */, + 9566F55F257982B500CCA8FC /* UIColorInterpolationTests.swift in Sources */, 954E7DEE1F48AE6E00342ECF /* Item.swift in Sources */, 954842631F4252070072038C /* PagingDiffTests.swift in Sources */, 95D78FDD228713B800E6EE7C /* MockCollectionViewLayout.swift in Sources */, diff --git a/Parchment/Extensions/UIColor+interpolation.swift b/Parchment/Extensions/UIColor+interpolation.swift index 71a293a3..ab579231 100644 --- a/Parchment/Extensions/UIColor+interpolation.swift +++ b/Parchment/Extensions/UIColor+interpolation.swift @@ -7,7 +7,9 @@ extension UIColor { func components() -> (CGFloat, CGFloat, CGFloat, CGFloat) { guard let c = cgColor.components else { return (0, 0, 0, 1) } - if (cgColor.numberOfComponents == 2) { + if cgColor.numberOfComponents == 1 { + return (0, 0, 0, 1) + } else if cgColor.numberOfComponents == 2 { return (c[0], c[0], c[0], c[1]) } else { return (c[0], c[1], c[2], c[3]) diff --git a/ParchmentTests/Resources.xcassets/Contents.json b/ParchmentTests/Resources.xcassets/Contents.json new file mode 100644 index 00000000..73c00596 --- /dev/null +++ b/ParchmentTests/Resources.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ParchmentTests/Resources.xcassets/Green.imageset/Contents.json b/ParchmentTests/Resources.xcassets/Green.imageset/Contents.json new file mode 100644 index 00000000..d27eb583 --- /dev/null +++ b/ParchmentTests/Resources.xcassets/Green.imageset/Contents.json @@ -0,0 +1,12 @@ +{ + "images" : [ + { + "filename" : "Green.png", + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ParchmentTests/Resources.xcassets/Green.imageset/Green.png b/ParchmentTests/Resources.xcassets/Green.imageset/Green.png new file mode 100644 index 0000000000000000000000000000000000000000..9108e419a27537af2a39cf1c89f3b86bb63ba57d GIT binary patch literal 139 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4aTa()7Bet#3xhBt!>l*8o|0J>k`3jycjv*C{Z%=JxWMJT7bUgXbI+>;QQhhc1 fuLbTm64<$H<}wK_tpCReRLkJ$>gTe~DWM4fw_YNB literal 0 HcmV?d00001 diff --git a/ParchmentTests/UIColorInterpolationTests.swift b/ParchmentTests/UIColorInterpolationTests.swift new file mode 100644 index 00000000..fc5a4a4d --- /dev/null +++ b/ParchmentTests/UIColorInterpolationTests.swift @@ -0,0 +1,17 @@ +import CoreGraphics +import XCTest +@testable import Parchment + +final class UIColorInterpolationTests: XCTestCase { + // Colors initialized with UIColor(patternImage:) have only 1 + // color component. This test ensures we don't crash. + func testImageFromPatternImageDefaultToBlack() { + let from = UIColor.red + let bundle = Bundle(for: Self.self) + let image = UIImage(named: "Green", in: bundle, compatibleWith: nil)! + let to = UIColor(patternImage: image) + let result = UIColor.interpolate(from: from, to: to, with: 1) + + XCTAssertEqual(result, UIColor(red: 0, green: 0, blue: 0, alpha: 1)) + } +}