简体   繁体   中英

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

I want that colour value using Cocoa:

在此输入图像描述

Thanks.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM