Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a color picker for macOS

Tags:

macos

I need to implement a simpler version of Digital Color Meter, that means just a color picker of the current state of my screen.

I do not need any preview, or advanced controls, as Digital Color Meter does. All I need instead is to get the color of the selected pixel. in the clipboard.

Could you give me a hint about how could I implement something like this in macOS?

like image 549
StuckOverFlow Avatar asked Oct 23 '25 17:10

StuckOverFlow


1 Answers

You don't need to, those lovely guys at Apple have done it for you. Just start the Color Picker, let the user choose a colour and it will be returned to your bash script.

So, the code looks like this in the Terminal or a bash script:

osascript -e "set theColor to choose color default color {0,0,0}"

The user must (probably) click the pipette just above the Cancel button, and click a pixel on the screen then click OK. The RGB returned is in the range 0..65535 for Red, Green and Blue.

enter image description here

As regards putting those RGB values on the Clipboard:

osascript <<EOF
set chosen to (choose color default color {65535, 65535, 65535})
set AppleScript's text item delimiters to ","
set the clipboard to chosen as string
EOF

That will give you something like this on the clipboard:

12000,37456,0

If you prefer something like this on the clipboard:

rgb(64633,1408,8371)

You can use:

osascript <<EOF
set chosen to (choose color default color {65535, 65535, 65535})
set col to "rgb(" & item 1 of chosen & "," & item 2 of chosen & "," &item 3 of chosen & ")"
set the clipboard to col
EOF
like image 65
Mark Setchell Avatar answered Oct 26 '25 08:10

Mark Setchell