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.
https://xojo.gitbook.io/add-ons/cstruecolors/documentation/nscoloraddition#controlaccent_color-as-color.
Or MBS or probably MacOSLib or Declares.
@Ulrich Bogun, 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.
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
oh really: A big Plugin for just getting a color?
I prefer a few declares:
[code]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©*255
Dim g As Integer = getGreenComponent©*255
Dim b As Integer = getBlueComponent©*255
Dim a As Integer = 256-getAlphaComponent©*255
Return RGB(r, g, b, a)
End Function
[/code]
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?
Good catch. Thank you.
Full ACK
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
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.
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