Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit c390f8c

Browse files
authored
Merge branch 'master' into tvos-support
2 parents 4919ae6 + 32e33e2 commit c390f8c

30 files changed

+49
-65
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# UIImageColors
22

3-
iTunes style color fetcher for UIImage. This is an *almost* identical port of [Panic's OS X ColorArt](https://github.com/panicinc/ColorArt/) for iOS Swift.
3+
iTunes style color fetcher for UIImage. This is based on [Panic's OS X ColorArt](https://github.com/panicinc/ColorArt/) for iOS Swift.
44

55
In other words, it fetches the most dominant and prominent colors.
66

Diff for: Sources/UIImageColors.swift

+21-37
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ extension UIImage {
148148
var result = UIImageColors()
149149

150150
let cgImage = self.resizeForUIImageColors(newSize: scaleDownSize).cgImage!
151-
let width = cgImage.width
152-
let height = cgImage.height
151+
let width: Int = cgImage.width
152+
let height: Int = cgImage.height
153+
154+
let blackColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
155+
let whiteColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
153156

154-
let bytesPerPixel: Int = 4
155-
let bytesPerRow: Int = width * bytesPerPixel
156-
let bitsPerComponent: Int = 8
157157
let randomColorsThreshold = Int(CGFloat(height)*0.01)
158158
let sortedColorComparator: Comparator = { (main, other) -> ComparisonResult in
159159
let m = main as! PCCountedColor, o = other as! PCCountedColor
@@ -165,50 +165,34 @@ extension UIImage {
165165
return ComparisonResult.orderedAscending
166166
}
167167
}
168-
let blackColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
169-
let whiteColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
170-
171-
let colorSpace = CGColorSpaceCreateDeviceRGB()
172-
let raw = malloc(bytesPerRow * height)
173-
defer {
174-
free(raw)
175-
}
176-
let bitmapInfo = CGImageAlphaInfo.premultipliedFirst.rawValue
177-
guard let ctx = CGContext(data: raw, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else {
178-
fatalError("UIImageColors.getColors failed: could not create CGBitmapContext")
168+
169+
guard let data = CFDataGetBytePtr(cgImage.dataProvider!.data) else {
170+
fatalError("UIImageColors.getColors failed: could not get cgImage data")
179171
}
180-
let drawingRect = CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height))
181-
ctx.draw(cgImage, in: drawingRect)
182172

183-
let data = ctx.data?.assumingMemoryBound(to: UInt8.self)
184-
185-
let leftEdgeColors = NSCountedSet(capacity: height)
173+
// Filter out and collect pixels from image
186174
let imageColors = NSCountedSet(capacity: width * height)
187175

188176
for x in 0..<width {
189177
for y in 0..<height {
190-
let pixel = ((width * y) + x) * bytesPerPixel
191-
let color = UIColor(
192-
red: CGFloat((data?[pixel+1])!)/255,
193-
green: CGFloat((data?[pixel+2])!)/255,
194-
blue: CGFloat((data?[pixel+3])!)/255,
195-
alpha: 1
196-
)
197-
198-
// A lot of images have white or black edges from crops, so ignore the first few pixels
199-
if 5 <= x && x <= 10 {
200-
leftEdgeColors.add(color)
178+
let pixel: Int = ((width * y) + x) * 4
179+
// Only consider pixels with 50% opacity or higher
180+
if 127 <= data[pixel+3] {
181+
imageColors.add(UIColor(
182+
red: CGFloat(data[pixel+2])/255,
183+
green: CGFloat(data[pixel+1])/255,
184+
blue: CGFloat(data[pixel])/255,
185+
alpha: 1.0
186+
))
201187
}
202-
203-
imageColors.add(color)
204188
}
205189
}
206190

207191
// Get background color
208-
var enumerator = leftEdgeColors.objectEnumerator()
209-
var sortedColors = NSMutableArray(capacity: leftEdgeColors.count)
192+
var enumerator = imageColors.objectEnumerator()
193+
var sortedColors = NSMutableArray(capacity: imageColors.count)
210194
while let kolor = enumerator.nextObject() as? UIColor {
211-
let colorCount = leftEdgeColors.count(for: kolor)
195+
let colorCount = imageColors.count(for: kolor)
212196
if randomColorsThreshold < colorCount {
213197
sortedColors.add(PCCountedColor(color: kolor, count: colorCount))
214198
}

Diff for: Supporting Files/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0</string>
18+
<string>1.3.0</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSPrincipalClass</key>

Diff for: UIImageColors.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = "UIImageColors"
3-
spec.version = "1.2.2"
3+
spec.version = "1.3.1"
44
spec.license = "MIT"
55
spec.summary = "iTunes style color fetcher for UIImage."
66
spec.homepage = "https://github.com/jathu/UIImageColors"

Diff for: UIImageColorsPlayground.playground/Contents.swift

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
//: Playground - noun: a place where people can play
22

33
import UIKit
4-
import XCPlayground
4+
import PlaygroundSupport
55
import UIImageColors
66

7-
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
7+
PlaygroundPage.current.needsIndefiniteExecution = true
88

99
let Albums: [Album] = [
10-
Album(albumFile: "OK Computer.png", albumName: "OK Computer", artistName: "Radiohead", year: 1997),
11-
Album(albumFile: "Nothing Was the Same.png", albumName: "Nothing Was the Same", artistName: "Drake", year: 2013),
12-
Album(albumFile: "My Beautiful Dark Twisted Fantasy.png", albumName: "My Beautiful Dark Twisted Fantasy", artistName: "Kanye West", year: 2010),
13-
Album(albumFile: "Kind of Blue.png", albumName: "Kind of Blue", artistName: "Miles Davis", year: 1959),
14-
Album(albumFile: "If You're Reading This It's Too Late.png", albumName: "If You're Reading This It's Too Late", artistName: "Drake", year: 2015),
15-
Album(albumFile: "New Morning.png", albumName: "New Morning", artistName: "Bob Dylan", year: 1970),
16-
Album(albumFile: "Since I Left You.png", albumName: "Since I Left You", artistName: "The Avalanches", year: 2000),
17-
Album(albumFile: "Random Access Memories.png", albumName: "Random Access Memories", artistName: "Daft Punk", year: 2013),
18-
Album(albumFile: "The Eraser.png", albumName: "The Eraser", artistName: "Thome Yorke", year: 2006),
19-
Album(albumFile: "Operation Doomsday.png", albumName: "Operation Doomsday", artistName: "MF Doom", year: 1999),
20-
Album(albumFile: "Cupid Deluxe.png", albumName: "Cupid Deluxe", artistName: "Blood Orange", year: 2013),
21-
Album(albumFile: "Black on Both Sides.png", albumName: "Black on Both Sides", artistName: "Mos Def", year: 1999)
10+
Album(albumFile: "df.png", albumName: "My Beautiful Dark Twisted Fantasy", artistName: "Kanye West", year: 2010),
11+
Album(albumFile: "nw.jpg", albumName: "Nothing Was the Same", artistName: "Drake", year: 2013),
12+
Album(albumFile: "ok.png", albumName: "OK Computer", artistName: "Radiohead", year: 1997),
13+
Album(albumFile: "yz.jpg", albumName: "Yeezus", artistName: "Kanye West", year: 2013),
14+
Album(albumFile: "ab.jpg", albumName: "Ágætis byrjun", artistName: "Sigur Rós", year: 1999),
15+
Album(albumFile: "cd.jpg", albumName: "Cupid Deluxe", artistName: "Blood Orange", year: 2013),
16+
Album(albumFile: "ed.jpg", albumName: "Endtroducing.....", artistName: "DJ Shadow", year: 1996),
17+
Album(albumFile: "dj.jpg", albumName: "Duke Ellington & John Coltrane", artistName: "Duke Ellington & John Coltrane", year: 1963),
18+
Album(albumFile: "ph.jpg", albumName: "Purple Haze", artistName: "Cam'ron", year: 2004),
19+
Album(albumFile: "rm.png", albumName: "Random Access Memories", artistName: "Daft Punk", year: 2013),
20+
Album(albumFile: "sk.jpg", albumName: "Skream!", artistName: "Skream", year: 2006),
21+
Album(albumFile: "b6.jpg", albumName: "Barter 6", artistName: "Young Thug", year: 2015)
2222
]
2323

2424
func makeBox(_ asynchronous: Bool, completionHandler: @escaping (UIView) -> Void) {
@@ -30,7 +30,7 @@ func makeBox(_ asynchronous: Bool, completionHandler: @escaping (UIView) -> Void
3030
let c = Container(album: Albums[Int(i)])
3131
c.frame.origin = CGPoint(x: sample.frame.width*(i.truncatingRemainder(dividingBy: 2)), y: sample.frame.height*floor(i/2))
3232
box.addSubview(c)
33-
33+
3434
if asynchronous {
3535
c.albumImageView.image!.getColors { colors in
3636
c.backgroundColor = colors.backgroundColor
@@ -55,7 +55,7 @@ func makeBox(_ asynchronous: Bool, completionHandler: @escaping (UIView) -> Void
5555
}
5656

5757
// Make a box of albums
58-
makeBox(true) { box in
58+
makeBox(false) { box in
5959
box.alpha = 1
60-
XCPlaygroundPage.currentPage.finishExecution()
60+
PlaygroundPage.current.finishExecution()
6161
}
Binary file not shown.
-2.46 MB
Binary file not shown.
Binary file not shown.
-250 KB
Binary file not shown.
Binary file not shown.
-2.24 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-1.03 MB
Binary file not shown.

Diff for: UIImageColorsPlayground.playground/Resources/ab.jpg

69 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/b6.jpg

48 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/cd.jpg

56.2 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/df.png

546 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/dj.jpg

47.6 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/ed.jpg

65.8 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/nw.jpg

52.2 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/ph.jpg

144 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/sk.jpg

72 KB
Loading

Diff for: UIImageColorsPlayground.playground/Resources/yz.jpg

60 KB
Loading
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='ios' last-migration='0800'>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false' last-migration='0800'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

Diff for: UIImageColorsPlayground.playground/timeline.xctimeline

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3117&amp;EndingColumnNumber=23&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=497787292.509668"
6+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2917&amp;EndingColumnNumber=23&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=515104523.006536"
77
selectedRepresentationIndex = "0"
88
shouldTrackSuperviewWidth = "NO">
99
</LoggerValueHistoryTimelineItem>
1010
<LoggerValueHistoryTimelineItem
11-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3117&amp;EndingColumnNumber=20&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=497787292.509811"
11+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2917&amp;EndingColumnNumber=20&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=515104523.00672"
1212
selectedRepresentationIndex = "0"
1313
shouldTrackSuperviewWidth = "NO">
1414
</LoggerValueHistoryTimelineItem>
1515
<LoggerValueHistoryTimelineItem
16-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3117&amp;EndingColumnNumber=19&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=497787292.509913"
16+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2917&amp;EndingColumnNumber=19&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=515104523.00685"
1717
selectedRepresentationIndex = "0"
1818
shouldTrackSuperviewWidth = "NO">
1919
</LoggerValueHistoryTimelineItem>
2020
<LoggerValueHistoryTimelineItem
21-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3117&amp;EndingColumnNumber=22&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=497787292.51001"
21+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2917&amp;EndingColumnNumber=22&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=515104523.006985"
2222
selectedRepresentationIndex = "0"
2323
shouldTrackSuperviewWidth = "NO">
2424
</LoggerValueHistoryTimelineItem>
2525
<LoggerValueHistoryTimelineItem
26-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3117&amp;EndingColumnNumber=3&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=497787292.510102"
26+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2917&amp;EndingColumnNumber=3&amp;EndingLineNumber=59&amp;StartingColumnNumber=1&amp;StartingLineNumber=59&amp;Timestamp=515104523.007109"
2727
selectedRepresentationIndex = "0"
2828
shouldTrackSuperviewWidth = "NO">
2929
</LoggerValueHistoryTimelineItem>
3030
<LoggerValueHistoryTimelineItem
31-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3117&amp;EndingColumnNumber=8&amp;EndingLineNumber=59&amp;StartingColumnNumber=5&amp;StartingLineNumber=59&amp;Timestamp=497787292.510192"
31+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2917&amp;EndingColumnNumber=8&amp;EndingLineNumber=59&amp;StartingColumnNumber=5&amp;StartingLineNumber=59&amp;Timestamp=515104523.007231"
3232
selectedRepresentationIndex = "0"
3333
shouldTrackSuperviewWidth = "NO">
3434
</LoggerValueHistoryTimelineItem>
3535
<LoggerValueHistoryTimelineItem
36-
documentLocation = "#CharacterRangeLen=3&amp;CharacterRangeLoc=3113&amp;EndingColumnNumber=8&amp;EndingLineNumber=58&amp;StartingColumnNumber=5&amp;StartingLineNumber=58&amp;Timestamp=497787293.750125"
36+
documentLocation = "#CharacterRangeLen=3&amp;CharacterRangeLoc=2913&amp;EndingColumnNumber=8&amp;EndingLineNumber=58&amp;StartingColumnNumber=5&amp;StartingLineNumber=58&amp;Timestamp=515104523.007466"
3737
lockedSize = "{546, 444}"
3838
selectedRepresentationIndex = "0"
3939
shouldTrackSuperviewWidth = "NO">

Diff for: preview.png

-14.4 KB
Loading

0 commit comments

Comments
 (0)