ColorKit icon indicating copy to clipboard operation
ColorKit copied to clipboard

macOS support

Open lbrndnr opened this issue 1 year ago • 10 comments

First of all, thanks for this great package! I would love to use it on macOS as well but noticed that it only supports iOS. Are there any plans to make this work for macOS (and/or tvOS) too?

lbrndnr avatar Jul 12 '23 18:07 lbrndnr

Same here! Looking for an alternative.

doriansgithub avatar Apr 16 '24 19:04 doriansgithub

Same.

aviwad avatar Aug 16 '24 23:08 aviwad

On MacOs there is a very simple set of code that does the trick.

If you need it let me know.

doriansgithub avatar Aug 17 '24 00:08 doriansgithub

Would love it if you could share it. Thank you :)

aviwad avatar Aug 17 '24 00:08 aviwad

Here you go…

var currentSongArtworkDominantColor : NSColor? var useColorGUI = true

extension NSImage { var dominantColor: NSColor? { guard let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil) else { print("Error: Unable to create CGImage from NSImage.") return nil }

    let ciImage = CIImage(cgImage: cgImage)
    
    guard let filter = CIFilter(name: "CIAreaAverage") else {
        print("Error: Unable to create CIFilter.")
        return nil
    }
    
    filter.setDefaults()
    filter.setValue(ciImage, forKey: kCIInputImageKey)
    
    guard let outputImage = filter.outputImage else {
        print("Error: Unable to get output image from filter.")
        return nil
    }
    
    var bitmap = [UInt8](repeating: 0, count: 4)
    let context = CIContext(options: nil)
    context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)
    
    let color = NSColor(deviceRed: CGFloat(bitmap[0])/255.0, green: CGFloat(bitmap[1])/255.0, blue: CGFloat(bitmap[2])/255.0, alpha: CGFloat(bitmap[3])/255.0)
    
    return color
}

}

useColorGUI = UserDefaults.standard.bool(forKey: "kUseAutoColor") if useColorGUI { if let nsColor = currentSongArtworkDominantColor { // Convert NSColor to CGColor let cgColor = nsColor.cgColor // Use cgColor print(cgColor) mainBackgroundColorEffect.wantsLayer = true mainBackgroundColorEffect.layer?.backgroundColor = cgColor } }

On Aug 16, 2024, at 8:13 PM, Avi Wadhwa @.***> wrote:

Would love it if you could share it. Thank you :)

— Reply to this email directly, view it on GitHub https://github.com/Boris-Em/ColorKit/issues/26#issuecomment-2294473856, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLTNEGI3JLGRBP7U2K2IE3ZR2ITTAVCNFSM6AAAAAA2H3M2FSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJUGQ3TGOBVGY. You are receiving this because you commented.

doriansgithub avatar Aug 17 '24 14:08 doriansgithub

Left this out…

    if useColorGUI {
        if let dominantColor = artworkImage.dominantColor {
            currentSongArtworkDominantColor = dominantColor
            print("Dominant color: \(dominantColor)")
        } else {
            print("Unable to determine the dominant color.")
        }
    }

On Aug 16, 2024, at 8:13 PM, Avi Wadhwa @.***> wrote:

Would love it if you could share it. Thank you :)

— Reply to this email directly, view it on GitHub https://github.com/Boris-Em/ColorKit/issues/26#issuecomment-2294473856, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLTNEGI3JLGRBP7U2K2IE3ZR2ITTAVCNFSM6AAAAAA2H3M2FSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJUGQ3TGOBVGY. You are receiving this because you commented.

doriansgithub avatar Aug 17 '24 14:08 doriansgithub

this only gives me a single color. the package we are commenting on gives a whole palette. :( thanks anyway

aviwad avatar Aug 17 '24 22:08 aviwad

Update. I have ported ColorKit to macOS 😁 however make sure to use .best quality because #27 plagues macOS 14 too. https://github.com/aviwad/ColorKit-macOS enjoy

aviwad avatar Aug 18 '24 00:08 aviwad