Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible to get the User's highlight colour on OS X using Cocoa?

I want that colour value using Cocoa:

enter image description here

Thanks.

like image 562
DNB5brims Avatar asked Mar 19 '14 06:03

DNB5brims


People also ask

How do you make highlights darker on a Mac?

Go to the  Apple menu and go to “System Preferences” Choose the “General” preference panel. Near the top of the panel pulldown the menu next to “Highlight color:” and pick the color to change to.


2 Answers

You can use the methods on the NSColor class to get the user's preferences.

The Accessing System Colors section in Color Programming Topics states that

NSColor has a number of methods that return system colors: colors controlled by user preferences. These colors—currently only selectedControlColor and selectedTextBackgroundColor—should be used by developers who want to create custom controls or subclass existing controls while honoring the user's color preferences.

like image 20
cacau Avatar answered Sep 28 '22 07:09

cacau


The NSColor for the users Highlight color will be the control color selectedControlColor

As far as I know you first have to convert the selectedControlColor to a known color space as it is not based on the NSNamedColorSpace.

NSNamedColorSpace = Catalog name and color name components The components of this color space are indexes into lists or catalogs of prepared colors. The catalogs of named colors come with lookup tables that are able to generate the correct color on a given device.


Generally, it is recommended that you use calibrated (or generic) color spaces instead of device color spaces. The colors in device color spaces can vary widely from device to device, whereas calibrated color spaces usually result in a reasonably accurate color. Device color spaces, on the other hand, might yield better performance under certain circumstances, so if you know for certain the device that will render or capture the color, use a device color space instead.


A code example

 NSColor *aColor = [[NSColor selectedControlColor] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
    if (aColor) {
        NSLog(@"   Red %f, Green %f, Blue %f,  Alpha %f,", aColor.redComponent,aColor.greenComponent,aColor.blueComponent,aColor.alphaComponent);
    }

see the Creating and Converting Color Spaces section in the Color Programming Topics

Which will give you more idea of how this works and finding how many components a colour has.

like image 53
markhunte Avatar answered Sep 28 '22 05:09

markhunte