+[NSColor highlightColor] returns white instead of actual color

  • I'm overriding -[NSTableView highlightSelectionInClipRect:] because I
    don't want all the columns of the selected row to be highlighted.
    Works fine except +[NSColor highlightColor] returns white instead of
    actual highlight color I have set in System Preferences > Appearance,
    which is green.

    Here's the code:

    color = [NSColor highlightColor] ;
    color = [color colorUsingColorSpaceName:@"NSDeviceRGBColorSpace"] ;
    NSLog(@"highlight color = %0.2f %0.2f %0.2f %0.2f",
              [color redComponent],
              [color greenComponent],
              [color blueComponent],
              [color alphaComponent]) ;

    Here's the console output:

    highlight color = 1.00 1.00 1.00 1.00

    {1,1,1,1} is solid white, not green.  What am I doing wrong?

    Thank you,

    Jerry Krinock
  • On Sun, Feb 1, 2009 at 1:11 AM, Jerry Krinock <jerry...> wrote:
    > I'm overriding -[NSTableView highlightSelectionInClipRect:] because I don't
    > want all the columns of the selected row to be highlighted.  Works fine
    > except +[NSColor highlightColor] returns white instead of actual highlight
    > color I have set in System Preferences > Appearance, which is green.

    > From the docs: "Returns the system color that represents the virtual
    light source on the screen."

    You want +[NSColor controlHighlightColor].
  • On 1 Feb 2009, at 6:11 pm, Jerry Krinock wrote:

    > except +[NSColor highlightColor] returns white instead of actual
    > highlight color I have set in System Preferences > Appearance, which
    > is green

    The green (or whatever) colour set in System Preferences is returned
    by +[NSColor selectedTextBackgroundColor]. The other reply you
    received is incorrect, as far as I can tell.

    --Graham
  • On 2009 Feb 01, at 2:50, Graham Cox wrote:

    > The...

    colour set in System Preferences > Appearance > Highlight Color (for
    selected text)

    > is returned by +[NSColor selectedTextBackgroundColor].

    Thanks - indeed it is.  And I found another one all by myself, with a
    good guess!...

    The method which, I would say, "returns the gray background color used
    behind selected text when its view is not the key view",
    +secondarySelectedControlColor, is described in the documentation as
    "returns the system color used in non-key views."  ?!?!?!?

    I sent 'em a little Feedback on that one.
  • Sorry, there's more to this....

    +[NSColor selectedTextBackgroundColor] definitely gives a color based
    on System Preferences > Appearance > Highlight Color (for selected
    text).  However, the color actually used in NSTableView is darker than
    this.

    For example, I set the color in System Preferences to RGB = {255, 0,
    255}, but when I use DigitalColor Meter.app on a highlighted cell in a
    "stock" NSTableView, I measure {204, 0, 204}.  Now, 204=51*4 and
    255=51*5.  So it looks like someone in the NSTextFieldCell Department
    decided to reduce the brightness by 20%, because, of course, if
    someone set their "highlight" (sic) color to all white, the white text
    drawn on it by NSTextFieldCell would be invisible.

    So, I was able to match the "stock" NSTextFieldCell using a little
    brightness-tweaking method that I had lying around.  Is this 20%
    reduction in brightness documented anywhere?  I even searched Human
    Interface Guldelines for "highlight" but didn't find any mention of
    this.

    Interestingly, in NSTextViews (TextEdit docs, this message in
    Mail.app), the text is drawn in black instead of white, and the 20%
    reduction is not applied.  And then, Xcode's editor ignores my System
    Preferences and uses black on orange, like it or not.  Seems like we
    need some User Interface Police here.

    ***************************************************

    color = [NSColor selectedTextBackgroundColor] ;
    color = [color colorTweakBrightness:-.20] ;

    ***************************************************

    #import <Cocoa/Cocoa.h>

    @interface NSColor (Tweak)

    /*!
      @brief    Returns a copy of the receiver, but with the
      brightness altered by a given tweak factor.

      @details  If tweak > 0, the brightness is altered by
      blending the receiver with +tweak fraction of pure white.&nbsp;
      If tweak < 0, the brightness is altered by
      blending the receiver with -tweak fraction of pure black.&nbsp;
      if tweak = 0, the returned color will be an identical copy of
      the receiver.

      @param    tweak  The factor by which to tweak the brightness.&nbsp;
      A value greater than +1.0 is clipped to +1.0.
      A value less than -1.0 is clipped to -1.0.
      @result  A tweaked, autoreleased copy of the receiver
    */
    - (NSColor*)colorTweakBrightness:(float)tweak ;

    @end

    ***************************************************

    #import "NSColor+Tweak.h"

    @implementation NSColor (Tweak)

    - (NSColor*)colorTweakBrightness:(float)tweak {
        SEL blendColorSelector = (tweak > 0) ? @selector(whiteColor) :
    @selector (blackColor) ;
        NSColor* blendColor = [NSColor
    performSelector:blendColorSelector] ;
        float blend = fabs(tweak) ;
        blend = MIN(blend, 1.0) ;
        return [self blendedColorWithFraction:(CGFloat)blend
                                      ofColor:blendColor] ;
    }

    @end
  • Hi jerry,

    My memory is that this is the alternateSelectedControlColor. I'm not
    at a mac right now, so I cannot verify.

    -Ken

    On Feb 1, 2009, at 6:46 AM, Jerry Krinock <jerry...> wrote:

    > Sorry, there's more to this....
    >
    > +[NSColor selectedTextBackgroundColor] definitely gives a color
    > based on System Preferences > Appearance > Highlight Color (for
    > selected text).  However, the color actually used in NSTableView is
    > darker than this.
    >
    > For example, I set the color in System Preferences to RGB = {255, 0,
    > 255}, but when I use DigitalColor Meter.app on a highlighted cell in
    > a "stock" NSTableView, I measure {204, 0, 204}.  Now, 204=51*4 and
    > 255=51*5.  So it looks like someone in the NSTextFieldCell
    > Department decided to reduce the brightness by 20%, because, of
    > course, if someone set their "highlight" (sic) color to all white,
    > the white text drawn on it by NSTextFieldCell would be invisible.
    >
    > So, I was able to match the "stock" NSTextFieldCell using a little
    > brightness-tweaking method that I had lying around.  Is this 20%
    > reduction in brightness documented anywhere?  I even searched Human
    > Interface Guldelines for "highlight" but didn't find any mention of
    > this.
    >
    > Interestingly, in NSTextViews (TextEdit docs, this message in
    > Mail.app), the text is drawn in black instead of white, and the 20%
    > reduction is not applied.  And then, Xcode's editor ignores my
    > System Preferences and uses black on orange, like it or not.  Seems
    > like we need some User Interface Police here.
    >
    > ***************************************************
    >
    > color = [NSColor selectedTextBackgroundColor] ;
    > color = [color colorTweakBrightness:-.20] ;
    >
    > ***************************************************
    >
    > #import <Cocoa/Cocoa.h>
    >
    > @interface NSColor (Tweak)
    >
    > /*!
    > @brief    Returns a copy of the receiver, but with the
    > brightness altered by a given tweak factor.
    >
    > @details  If tweak > 0, the brightness is altered by
    > blending the receiver with +tweak fraction of pure white.&nbsp;
    > If tweak < 0, the brightness is altered by
    > blending the receiver with -tweak fraction of pure black.&nbsp;
    > if tweak = 0, the returned color will be an identical copy of
    > the receiver.
    >
    > @param    tweak  The factor by which to tweak the brightness.&nbsp;
    > A value greater than +1.0 is clipped to +1.0.
    > A value less than -1.0 is clipped to -1.0.
    > @result  A tweaked, autoreleased copy of the receiver
    > */
    > - (NSColor*)colorTweakBrightness:(float)tweak ;
    >
    > @end
    >
    > ***************************************************
    >
    > #import "NSColor+Tweak.h"
    >
    >
    > @implementation NSColor (Tweak)
    >
    > - (NSColor*)colorTweakBrightness:(float)tweak {
    > SEL blendColorSelector = (tweak > 0) ? @selector(whiteColor) :
    > @selector (blackColor) ;
    > NSColor* blendColor = [NSColor
    > performSelector:blendColorSelector] ;
    > float blend = fabs(tweak) ;
    > blend = MIN(blend, 1.0) ;
    > return [self blendedColorWithFraction:(CGFloat)blend
    > ofColor:blendColor] ;
    > }
    >
    > @end
  • On 2 Feb 2009, at 1:46 am, Jerry Krinock wrote:

    > +[NSColor selectedTextBackgroundColor] definitely gives a color
    > based on System Preferences > Appearance > Highlight Color (for
    > selected text). However, the color actually used in NSTableView is
    > darker than this.
    >
    > For example, I set the color in System Preferences to RGB = {255, 0,
    > 255}, but when I use DigitalColor Meter.app on a highlighted cell in
    > a "stock" NSTableView, I measure {204, 0, 204}.  Now, 204=51*4 and
    > 255=51*5.  So it looks like someone in the NSTextFieldCell
    > Department decided to reduce the brightness by 20%, because, of
    > course, if someone set their "highlight" (sic) color to all white,
    > the white text drawn on it by NSTextFieldCell would be invisible.
    >
    > So, I was able to match the "stock" NSTextFieldCell using a little
    > brightness-tweaking method that I had lying around.  Is this 20%
    > reduction in brightness documented anywhere?  I even searched Human
    > Interface Guldelines for "highlight" but didn't find any mention of
    > this.
    >
    > Interestingly, in NSTextViews (TextEdit docs, this message in
    > Mail.app), the text is drawn in black instead of white, and the 20%
    > reduction is not applied.  And then, Xcode's editor ignores my
    > System Preferences and uses black on orange, like it or not.  Seems
    > like we need some User Interface Police here.
    >
    > ***************************************************
    >
    > color = [NSColor selectedTextBackgroundColor] ;
    > color = [color colorTweakBrightness:-.20] ;

    Just to make sure you're aware of it, but -[NSColor shadowWithLevel:]
    (and its sister method, -[NSColor highlightWithLevel:]) are darkening
    and brightening methods that NSColor has - there's no need to write
    your own unless they're doing something unusually funky.

    I can't shed any light on what NSTableView uses, maybe Corbin could
    let you know? (I modified the title of the message so he might notice
    it ;-)

    Xcode is using my system prefs for text highlight here - never seen
    black on orange, maybe your editor settings have been changed at some
    point?

    --Graham
  • On Feb 1, 2009, at 2:41 PM, Graham Cox wrote:

    >
    > On 2 Feb 2009, at 1:46 am, Jerry Krinock wrote:
    >
    >> +[NSColor selectedTextBackgroundColor] definitely gives a color
    >> based on System Preferences > Appearance > Highlight Color (for
    >> selected text). However, the color actually used in NSTableView is
    >> darker than this.
    >>
    >> For example, I set the color in System Preferences to RGB = {255,
    >> 0, 255}, but when I use DigitalColor Meter.app on a highlighted
    >> cell in a "stock" NSTableView, I measure {204, 0, 204}.  Now,
    >> 204=51*4 and 255=51*5.  So it looks like someone in the
    >> NSTextFieldCell Department decided to reduce the brightness by 20%,
    >> because, of course, if someone set their "highlight" (sic) color to
    >> all white, the white text drawn on it by NSTextFieldCell would be
    >> invisible.
    >>
    >> So, I was able to match the "stock" NSTextFieldCell using a little
    >> brightness-tweaking method that I had lying around.  Is this 20%
    >> reduction in brightness documented anywhere?  I even searched Human
    >> Interface Guldelines for "highlight" but didn't find any mention of
    >> this.
    >>
    >> Interestingly, in NSTextViews (TextEdit docs, this message in
    >> Mail.app), the text is drawn in black instead of white, and the 20%
    >> reduction is not applied.  And then, Xcode's editor ignores my
    >> System Preferences and uses black on orange, like it or not.  Seems
    >> like we need some User Interface Police here.
    >>
    >> ***************************************************
    >>
    >> color = [NSColor selectedTextBackgroundColor] ;
    >> color = [color colorTweakBrightness:-.20] ;
    >
    >
    > Just to make sure you're aware of it, but -[NSColor
    > shadowWithLevel:] (and its sister method, -[NSColor
    > highlightWithLevel:]) are darkening and brightening methods that
    > NSColor has - there's no need to write your own unless they're doing
    > something unusually funky.
    >
    > I can't shed any light on what NSTableView uses, maybe Corbin could
    > let you know? (I modified the title of the message so he might
    > notice it ;-)

    Good call; I have a Smart Folder to only show things with "TableView/
    OutlineView and SavePanel/OpenPanel" in the title. That's a hint to
    people with questions on those subjects.

    The blue-highlight color that NSTableView uses is this:

    + (NSColor *)alternateSelectedControlColor;    // Similar to
    selectedControlColor; for use in lists and tables

    Note: for custom cells, you may want to simply return nil from this
    method to avoid having the NSCell do any background highlight color
    drawing:

    - (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView
    *)controlView;

    That only works in NSTableViews; it won't work with cells in
    NSBrowsers, which actually rely on this method to draw the
    highlighting (since it is a matrix of cells).

    corbin