Skip to content

Commit

Permalink
Merge pull request #544 from rechsteiner/fix-color-interpolation-crash
Browse files Browse the repository at this point in the history
Fix crash when using UIColor(patternImage:)
  • Loading branch information
rechsteiner authored Dec 15, 2020
2 parents d73110c + 02a97e9 commit 811927d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Parchment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -272,6 +274,8 @@
95591F20222C3A0400677B4B /* PagingControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingControllerTests.swift; sourceTree = "<group>"; };
95591F22222C522800677B4B /* PagingController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingController.swift; sourceTree = "<group>"; };
955D02432434E56900416E71 /* PagingViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingViewTests.swift; sourceTree = "<group>"; };
9566F55E257982B500CCA8FC /* UIColorInterpolationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorInterpolationTests.swift; sourceTree = "<group>"; };
9566F566257983B200CCA8FC /* Resources.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Resources.xcassets; sourceTree = "<group>"; };
9568922A222C525C00AFF250 /* CollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionView.swift; sourceTree = "<group>"; };
956EBE5D248BC426003ED4BA /* PagingCollectionViewLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingCollectionViewLayoutTests.swift; sourceTree = "<group>"; };
9575BEB22461FEF9002403F6 /* CreateDistance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateDistance.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -920,6 +926,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9566F567257983B200CCA8FC /* Resources.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -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 */,
Expand Down
4 changes: 3 additions & 1 deletion Parchment/Extensions/UIColor+interpolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 6 additions & 0 deletions ParchmentTests/Resources.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
12 changes: 12 additions & 0 deletions ParchmentTests/Resources.xcassets/Green.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "Green.png",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions ParchmentTests/UIColorInterpolationTests.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 811927d

Please sign in to comment.