Mojave System Accent Color

  1. last year

    Controls like checkboxes, popup menus do respect the users accent color choice, is there a way to get this for custom controls?
    I can find the HighlightColor but not something like AccentColor.

  2. Ulrich B

    30 Dec 2018 Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    https://xojo.gitbook.io/add-ons/cstruecolors/documentation/nscoloraddition#controlaccent_color-as-color .
    Or MBS or probably MacOSLib or Declares.

  3. @Ulrich B, thanks for the link. I've tried with macOSlib, there's a appearance-class, but this one is still carbon and does not fire if returning to default blue. As xojo provides the highlight color I've just made a method returning the color hard coded dependent of the highlight color, fine enough for now. But I think xojo should provide this property.

  4. Horst J

    29 May 2019 Testers, Xojo Pro Europe, Germany, Stuttgart

    If you have MBS Plugins, you can use this:

    Public Function GetSystemAccentColor() as Color
    dim ns as new NSButtonMBS
    dim n as NSColorMBS
    n = ns.backgroundColor.controlAccentColor
    return n.colorValue
    End Function

  5. oh really: A big Plugin for just getting a color?

    I prefer a few declares:

    Public Function controlAccentColor() as Color
      Declare Function NSClassFromString  Lib "AppKit" (aClassName As CFStringRef) As Ptr
      Declare Function controlAccentColor Lib "AppKit" selector "controlAccentColor" (ob_id As Ptr) As Ptr
      Declare Function getRedComponent    Lib "AppKit" selector "redComponent"(ob_id As Ptr) As CGFloat
      Declare Function getGreenComponent  Lib "AppKit" selector "greenComponent"(ob_id As Ptr) As CGFloat
      Declare Function getBlueComponent   Lib "AppKit" selector "blueComponent"(ob_id As Ptr) As CGFloat
      Declare Function getAlphaComponent  Lib "AppKit" selector "alphaComponent"(ob_id As Ptr) As CGFloat
      Declare Function colorUsingColorSpaceName Lib "AppKit" selector "colorUsingColorSpaceName:"(ob_id As Ptr, csName As CFStringRef) As Ptr
      
      Dim c As Ptr = controlAccentColor(NSClassFromString("NSColor"))
      c = colorUsingColorSpaceName(c, "NSCalibratedRGBColorSpace")
      
      Dim r As Integer = getRedComponent(c)*255
      Dim g As Integer = getGreenComponent(c)*255
      Dim b As Integer = getBlueComponent(c)*255
      Dim a As Integer = 256-getAlphaComponent(c)*255
      
      Return RGB(r, g, b, a)
    End Function
  6. Tim P

    29 May 2019 Testers, Xojo Pro Rochester, NY

    I like how simple plugins make things, I've used MBS in several projects. But if you don't have them the declare is very helpful, thank you! To those using the declare and plugin, keep in mind that controlAccentColor is 10.14+ so wrap it in an "respondsToSelector" check.

    What I don't understand is why we don't have access to the color via Xojo code. Listbox clearly has access to it, so why isn't it exposed to us? :(

  7. @Tim P To those using the declare and plugin, keep in mind that controlAccentColor is 10.14+ so wrap it in an "respondsToSelector" check.

    Good catch. Thank you.

    What I don't understand is why we don't have access to the color via Xojo code. Listbox clearly has access to it, so why isn't it exposed to us? :(

    Full ACK

  8. 8 months ago

    Horst J

    7 Oct 2019 Testers, Xojo Pro Europe, Germany, Stuttgart
    Edited 8 months ago

    If you would have the "Selection Color", use this:

    Public Function GetSelectionColor() as Color
      
      Declare Function NSClassFromString  Lib "AppKit" (aClassName As CFStringRef) As Ptr
      Declare Function controlAccentColor Lib "AppKit" selector "selectedControlColor" (ob_id As Ptr) As Ptr
      Declare Function getRedComponent    Lib "AppKit" selector "redComponent"(ob_id As Ptr) As CGFloat
      Declare Function getGreenComponent  Lib "AppKit" selector "greenComponent"(ob_id As Ptr) As CGFloat
      Declare Function getBlueComponent   Lib "AppKit" selector "blueComponent"(ob_id As Ptr) As CGFloat
      Declare Function getAlphaComponent  Lib "AppKit" selector "alphaComponent"(ob_id As Ptr) As CGFloat
      Declare Function colorUsingColorSpaceName Lib "AppKit" selector "colorUsingColorSpaceName:"(ob_id As Ptr, csName As CFStringRef) As Ptr
      
      Dim c As Ptr = controlAccentColor(NSClassFromString("NSColor"))
      c = colorUsingColorSpaceName(c, "NSCalibratedRGBColorSpace")
      
      Dim r As Integer = getRedComponent(c)*255
      Dim g As Integer = getGreenComponent(c)*255
      Dim b As Integer = getBlueComponent(c)*255
      Dim a As Integer = 256-getAlphaComponent(c)*255
      
      Return RGB(r, g, b, a)
      
    End Function

    Edit: added code tags

  9. Greg O

    7 Oct 2019 Xojo Inc scout.galaxy.barn

    Please keep in mind that this code will work for 98% of the named colors, but there are still some that are patterns. You should add some error/exception handling around that code just in case.

    Also, AccentColor is only available on 10.14+. that code will crash an app running on earlier versions without some checks.

  10. Horst J

    8 Oct 2019 Testers, Xojo Pro Europe, Germany, Stuttgart

    If anyone will use the system colors for macOS 10.14 and 10.15, you can use these two projects (without plugin and include check OS release:
    2019R2 => GetSystemColors
    2019R1.1 => GetSystemColor2019R1.1

or Sign Up to reply!