Skip to content

Commit

Permalink
Merge pull request #28 from hyperoslo/feature/pixel-color
Browse files Browse the repository at this point in the history
Feature: pixel color
  • Loading branch information
onmyway133 authored Aug 31, 2016
2 parents c8b9172 + 334fbc4 commit 32d82ab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
34 changes: 17 additions & 17 deletions HueTests/iOS/UIColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import XCTest
class UIColorTests: XCTestCase {

func testHex() {
let white = UIColor.hex("#FFFFFF")
let black = UIColor.hex("000000")
let red = UIColor.hex("F00")
let blue = UIColor.hex("#00F")
let green = UIColor.hex("#00FF00")
let yellow = UIColor.hex("#FFFF00")
let white = UIColor(hex: "#FFFFFF")
let black = UIColor(hex: "000000")
let red = UIColor(hex: "F00")
let blue = UIColor(hex: "#00F")
let green = UIColor(hex: "#00FF00")
let yellow = UIColor(hex: "#FFFF00")

XCTAssertEqual(white, UIColor(red: 255, green: 255, blue: 255, alpha: 1.0))
XCTAssertEqual(black, UIColor(red: 0, green: 0, blue: 0, alpha: 1.0))
Expand Down Expand Up @@ -53,7 +53,7 @@ class UIColorTests: XCTestCase {
}

func testAlpha() {
let yellowWithAlpha = UIColor.hex("#FFFF00").alpha(0.5)
let yellowWithAlpha = UIColor(hex: "#FFFF00").alpha(0.5)

XCTAssertEqual(yellowWithAlpha, UIColor(red: 255, green: 255, blue: 0, alpha: 1.0).colorWithAlphaComponent(0.5))
}
Expand Down Expand Up @@ -85,40 +85,40 @@ class UIColorTests: XCTestCase {
CGColorSpaceGetModel(CGColorGetColorSpace(UIColor.yellowColor().CGColor)))
XCTAssertEqual(testGradientWithLocation.locations!, [0.25,1.0])
}

func testComponents() {
let blue = UIColor.blueColor()
let green = UIColor.greenColor()
let red = UIColor.redColor()
let black = UIColor.blackColor()
let white = UIColor.whiteColor()

XCTAssertEqual(blue.redComponent, 0.0)
XCTAssertEqual(blue.greenComponent, 0.0)
XCTAssertEqual(blue.blueComponent, 1.0)
XCTAssertEqual(blue.alphaComponent, 1.0)

XCTAssertEqual(red.redComponent, 1.0)
XCTAssertEqual(red.greenComponent, 0.0)
XCTAssertEqual(red.blueComponent, 0.0)
XCTAssertEqual(red.alphaComponent, 1.0)

XCTAssertEqual(green.redComponent, 0.0)
XCTAssertEqual(green.greenComponent, 1.0)
XCTAssertEqual(green.blueComponent, 0.0)
XCTAssertEqual(green.alphaComponent, 1.0)

XCTAssertEqual(black.redComponent, 0.0)
XCTAssertEqual(black.greenComponent, 0.0)
XCTAssertEqual(black.blueComponent, 0.0)
XCTAssertEqual(black.alphaComponent, 1.0)

XCTAssertEqual(white.redComponent, 1.0)
XCTAssertEqual(white.greenComponent, 1.0)
XCTAssertEqual(white.blueComponent, 1.0)
XCTAssertEqual(white.alphaComponent, 1.0)
}

func testBlending() {
let black = UIColor.blackColor()
let white = UIColor.whiteColor()
Expand All @@ -130,17 +130,17 @@ class UIColorTests: XCTestCase {
saturation: 0.1,
brightness: 1.0,
alpha: 1.0)

let testWhite = black.addRGB(white)
XCTAssertEqual(testWhite.redComponent, white.redComponent)
XCTAssertEqual(testWhite.greenComponent, white.greenComponent)
XCTAssertEqual(testWhite.blueComponent, white.blueComponent)

let testYellow = green.addRGB(red)
XCTAssertEqual(testYellow.redComponent, yellow.redComponent)
XCTAssertEqual(testYellow.greenComponent, yellow.greenComponent)
XCTAssertEqual(testYellow.blueComponent, yellow.blueComponent)

let testBlue = deSaturatedBlue.addHue(0.0, saturation: 1.0, brightness: 0.0, alpha: 0.0);
XCTAssertEqual(testBlue.redComponent, blue.redComponent)
XCTAssertEqual(testBlue.greenComponent, blue.greenComponent)
Expand Down
21 changes: 16 additions & 5 deletions HueTests/iOS/UIImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import XCTest

class UIImageTests: XCTestCase {

func testImageColors() {
let accuracy: CGFloat = 0.001
var image: UIImage!

override func setUp() {
super.setUp()
let bundle = NSBundle(forClass: self.classForCoder)
let path = bundle.pathForResource("Random Access Memories", ofType: "png")!
let image = UIImage(contentsOfFile: path)!

image = UIImage(contentsOfFile: path)!
}

func testImageColors() {
XCTAssertNotNil(image)

let accuracy: CGFloat = 0.001
let colors = image.colors()

var (red, green, blue): (CGFloat, CGFloat, CGFloat) = (0,0,0)

colors.background.getRed(&red, green: &green, blue: &blue, alpha: nil)

XCTAssertEqualWithAccuracy(red, 0.035, accuracy: accuracy)
Expand All @@ -41,4 +46,10 @@ class UIImageTests: XCTestCase {
XCTAssertEqualWithAccuracy(blue, 0.85, accuracy: accuracy)
}

func testPixelColorSubscript() {
XCTAssertNotNil(image)
XCTAssertNil(image.color(at: CGPoint(x: 1300, y: -1)))
XCTAssertEqual(image.color(at: CGPoint(x: 0, y: 0))?.hex(), "#090D0E")
XCTAssertEqual(image.color(at: CGPoint(x: 535, y: 513))?.hex(), "#C8DDF0")
}
}
19 changes: 19 additions & 0 deletions Source/iOS/UIImage+Hue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,23 @@ extension UIImage {
secondaryColor ?? (isDarkBackgound ? whiteColor : blackColor),
detailColor ?? (isDarkBackgound ? whiteColor : blackColor))
}

public func color(at point: CGPoint) -> UIColor? {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
guard rect.contains(point) else {
return nil
}

let dataProvider = CGImageGetDataProvider(CGImage)
let dataCopy = CGDataProviderCopyData(dataProvider)
let data = CFDataGetBytePtr(dataCopy)
let pixelInfo = (Int(size.width) * Int(point.y) + Int(point.x)) * 4

let red = CGFloat(data[pixelInfo]) / 255.0
let green = CGFloat(data[pixelInfo + 1]) / 255.0
let blue = CGFloat(data[pixelInfo + 2]) / 255.0
let alpha = CGFloat(data[pixelInfo + 3]) / 255.0

return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}

0 comments on commit 32d82ab

Please sign in to comment.