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

Color Picker Extension #11

Open
HuakunShen opened this issue Sep 24, 2024 · 0 comments
Open

Color Picker Extension #11

HuakunShen opened this issue Sep 24, 2024 · 0 comments

Comments

@HuakunShen
Copy link
Contributor

HuakunShen commented Sep 24, 2024

Sample CLI swift code, get value from stdout.

import AppKit

struct Color: Encodable {
    let red: Float
    let blue: Float
    let green: Float
    let alpha: Float
    let colorSpace: String
}

struct ColorPickerCLI {

    static func pickColor() async -> Color? {
        let colorSampler = NSColorSampler()
        guard let color = await colorSampler.sample()?.usingColorSpace(.displayP3) else {
            return nil
        }
        
        return Color(
            red: Float(color.redComponent),
            blue: Float(color.blueComponent),
            green: Float(color.greenComponent),
            alpha: Float(color.alphaComponent),
            colorSpace: "p3"
        )
    }

    static func run() {
        // Run the asynchronous color picking in a Task
        Task {
            if let color = await pickColor() {
                print("Color picked:")
                print("Red: \(color.red)")
                print("Green: \(color.green)")
                print("Blue: \(color.blue)")
                print("Alpha: \(color.alpha)")
                print("Color Space: \(color.colorSpace)")
                // Exit the program after printing the color information
                exit(0)
            } else {
                print("No color picked or an error occurred.")
            }
        }
        // Keep the program running to allow async tasks to finish
        RunLoop.current.run()
    }
}

ColorPickerCLI.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

No branches or pull requests

1 participant