Skip to content

Instantly share code, notes, and snippets.

@ollieatkinson
Last active July 24, 2019 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollieatkinson/01c49ab54d64d75a04852d7ac1a44346 to your computer and use it in GitHub Desktop.
Save ollieatkinson/01c49ab54d64d75a04852d7ac1a44346 to your computer and use it in GitHub Desktop.
Assert that two colors are similar using XCTest
func XCTAssertColor(_ l: UIColor?, _ r: UIColor, accuracy: CGFloat, file: StaticString = #file, line: UInt = #line) {
guard let l = l else {
XCTFail("Color is 'nil'", file: file, line: line)
return
}
var (lh, rh) = (CGFloat(0), CGFloat(0))
var (lb, rb) = (CGFloat(0), CGFloat(0))
var (ls, rs) = (CGFloat(0), CGFloat(0))
var (la, ra) = (CGFloat(0), CGFloat(0))
l.getHue(&lh, saturation: &ls, brightness: &lb, alpha: &la)
r.getHue(&rh, saturation: &rs, brightness: &rb, alpha: &ra)
XCTAssertEqual(lh, rh, accuracy: accuracy, "Hue", file: file, line: line)
XCTAssertEqual(ls, rs, accuracy: accuracy, "Saturation", file: file, line: line)
XCTAssertEqual(lb, rb, accuracy: accuracy, "Brightness", file: file, line: line)
XCTAssertEqual(la, ra, accuracy: accuracy, "Alpha", file: file, line: line)
}
@ollieatkinson
Copy link
Author

ollieatkinson commented Jul 24, 2019

XCTAssertColor(#colorLiteral(red: 1, green: 0, blue: 0.6037723036, alpha: 1),  #colorLiteral(red: 1, green: 0.4980392157, blue: 0, alpha: 1), accuracy: 0.001)
XCTAssertColor(#colorLiteral(red: 1, green: 0, blue: 0.6047723036, alpha: 1),  #colorLiteral(red: 1, green: 0, blue: 0.6037723036, alpha: 1), accuracy: 0.001)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment