Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension function to get dominant colors of an image #19

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ktiays
Copy link
Member

@ktiays ktiays commented Oct 23, 2022

No description provided.

Copy link
Member

@unixzii unixzii left a comment

Choose a reason for hiding this comment

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

Looks great but there are some small issues need to be solved before a merge.

@@ -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?

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.

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants