Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let package = Package(
.target(name: "CyanConcurrency"),
.target(name: "CyanSwiftUI", dependencies: ["CyanExtensions"]),
.target(name: "CyanUI", dependencies: ["CyanSwiftUI"]),
.target(name: "CyanImage", dependencies: ["CyanExtensions"]),
.target(
name: "CyanKit",
dependencies: [
Expand All @@ -27,6 +28,7 @@ let package = Package(
"CyanConcurrency",
"CyanSwiftUI",
"CyanUI",
"CyanImage",
]),
.testTarget(
name: "CyanConcurrencyTests",
Expand Down
41 changes: 41 additions & 0 deletions Sources/CyanImage/Color.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we use Image+Color.swift as the file name to imply that this is an extension?

// Created by ktiays on 2022/10/17.
// Copyright (c) 2022 ktiays. All rights reserved.
//

#if canImport(UIKit)

import UIKit

extension UIImage {

public convenience init?(with color: UIColor, size: CGSize = .init(width: 1, height: 1)) {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRect(origin: CGPoint.zero, size: size))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else {
return nil
}
self.init(cgImage: cgImage)
}

}

#elseif canImport(AppKit)

import AppKit

extension NSImage {

public convenience init(color: NSColor, size: NSSize = .init(width: 1, height: 1)) {
self.init(size: size)
lockFocus()
color.drawSwatch(in: NSRect(origin: .zero, size: size))
unlockFocus()
}

}

#endif
73 changes: 73 additions & 0 deletions Sources/CyanImage/DominantColors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Created by ktiays on 2022/10/11.
// Copyright (c) 2022 ktiays. All rights reserved.
//

import CoreImage
import CyanExtensions

extension CIImage {

public func dominantColor(clusterCount: Int = 5) -> [PlatformColor] {
guard let kMeansFilter = CIFilter(name: "CIKMeans") else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The k prefix is for constants I believe, we should just remove it.

return []
}

kMeansFilter.setValue(self, forKey: kCIInputImageKey)
kMeansFilter.setValue(CIVector(cgRect: self.extent), forKey: kCIInputExtentKey)
kMeansFilter.setValue(clusterCount, forKey: "inputCount")
kMeansFilter.setValue(20, forKey: "inputPasses")

guard var outputImage = kMeansFilter.outputImage else {
return []
}

outputImage = outputImage.settingAlphaOne(in: outputImage.extent)

let context = CIContext()
var bitmap = [UInt8](repeating: 0, count: 4 * clusterCount)
context.render(outputImage,
toBitmap: &bitmap,
rowBytes: 4 * clusterCount,
bounds: outputImage.extent,
format: CIFormat.RGBA8,
colorSpace: self.colorSpace!)

var dominantColors = [PlatformColor]()

for i in 0..<clusterCount {
let color = PlatformColor(red: CGFloat(bitmap[i * 4 + 0]) / 255.0, green: CGFloat(bitmap[i * 4 + 1]) / 255.0, blue: CGFloat(bitmap[i * 4 + 2]) / 255.0, alpha: CGFloat(bitmap[i * 4 + 3]) / 255.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some line breaks would be nice to have.

dominantColors.append(color)
}

return dominantColors
}

}

#if canImport(UIKit)

import UIKit

extension UIImage {

@inlinable
public func dominantColors(clusterCount: Int = 5) -> [UIColor] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a PlatformImage typealias to unify these APIs.

CIImage(image: self)?.dominantColor(clusterCount: clusterCount) ?? []
}

}

#elseif canImport(AppKit)

import AppKit

extension NSImage {

public func dominantColors(clusterCount: Int = 5) -> [NSColor] {
ciImage?.dominantColor(clusterCount: clusterCount) ?? []
}

}

#endif
32 changes: 32 additions & 0 deletions Sources/CyanImage/NSImage+CIImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by ktiays on 2022/10/14.
// Copyright (c) 2022 ktiays. All rights reserved.
//

#if canImport(AppKit)

import AppKit

extension NSImage {

public var ciImage: CIImage? {
guard let data = self.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: data) else {
return nil
}
return CIImage(bitmapImageRep: bitmap)
}

}

extension NSImage {

public convenience init(ciImage: CIImage) {
let imageRep = NSCIImageRep(ciImage: ciImage)
self.init(size: imageRep.size)
self.addRepresentation(imageRep)
}

}

#endif