Control border color

Mac OSX specific forum
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Control border color

Post by wombats »

Hi,

I use the CanvasGadget for some custom controls. I want the border of them to match the TreeGadget, ListIconGadget, etc., which have a border color of 197, 197, 197.

However, none of the system colors match.

Is there a color I'm missing or is there a way to get a color from another control?

Code: Select all

NewList colors.s()
AddElement(colors()) : colors() = "labelColor"
AddElement(colors()) : colors() = "secondaryLabelColor"
AddElement(colors()) : colors() = "tertiaryLabelColor"
AddElement(colors()) : colors() = "quaternaryLabelColor"
AddElement(colors()) : colors() = "systemRedColor"
AddElement(colors()) : colors() = "systemGreenColor"
AddElement(colors()) : colors() = "systemBlueColor"
AddElement(colors()) : colors() = "systemOrangeColor"
AddElement(colors()) : colors() = "systemYellowColor"
AddElement(colors()) : colors() = "systemBrownColor"
AddElement(colors()) : colors() = "systemPinkColor"
AddElement(colors()) : colors() = "systemPurpleColor"
AddElement(colors()) : colors() = "systemGrayColor"
AddElement(colors()) : colors() = "linkColor"
AddElement(colors()) : colors() = "placeholderTextColor"
AddElement(colors()) : colors() = "windowFrameTextColor"
AddElement(colors()) : colors() = "selectedMenuItemTextColor"
AddElement(colors()) : colors() = "alternateSelectedControlTextColor"
AddElement(colors()) : colors() = "headerTextColor"
AddElement(colors()) : colors() = "separatorColor"
AddElement(colors()) : colors() = "gridColor"
AddElement(colors()) : colors() = "textColor"
AddElement(colors()) : colors() = "textBackgroundColor"
AddElement(colors()) : colors() = "selectedTextColor"
AddElement(colors()) : colors() = "selectedTextBackgroundColor"
AddElement(colors()) : colors() = "unemphasizedSelectedTextBackgroundColor"
AddElement(colors()) : colors() = "unemphasizedSelectedTextColor"
AddElement(colors()) : colors() = "windowBackgroundColor"
AddElement(colors()) : colors() = "underPageBackgroundColor"
AddElement(colors()) : colors() = "controlBackgroundColor"
AddElement(colors()) : colors() = "selectedContentBackgroundColor"
AddElement(colors()) : colors() = "unemphasizedSelectedContentBackgroundColor"
AddElement(colors()) : colors() = "alternatingContentBackgroundColor"
AddElement(colors()) : colors() = "findHighlightColor"
AddElement(colors()) : colors() = "controlColor"
AddElement(colors()) : colors() = "controlTextColor"
AddElement(colors()) : colors() = "selectedControlColor"
AddElement(colors()) : colors() = "selectedControlTextColor"
AddElement(colors()) : colors() = "disabledControlTextColor"
AddElement(colors()) : colors() = "keyboardFocusIndicatorColor"
AddElement(colors()) : colors() = "controlAccentColor"

; By wilbert: https://www.purebasic.fr/english/viewtopic.php?p=419571#p419571
Procedure.i GetCocoaColor(ColorName.s)
  Protected.i Result, Rect.NSRect, Image, NSColor = CocoaMessage(#Null, #Null, "NSColor " + ColorName)
  If NSColor
    Rect\size\width = 1
    Rect\size\height = 1
    Image = CreateImage(#PB_Any, 1, 1)
    StartDrawing(ImageOutput(Image))
    CocoaMessage(#Null, NSColor, "drawSwatchInRect:@", @Rect)
    Result = Point(0, 0)
    StopDrawing()
    FreeImage(Image)
    ProcedureReturn Result
  Else
    ProcedureReturn -1
  EndIf
EndProcedure

ForEach colors()
  color = GetCocoaColor(colors())
  Debug colors() + " = " + Str(Red(color)) + ", " + Str(Green(color)) + ", " + Str(Blue(color))
Next
User avatar
mk-soft
Always Here
Always Here
Posts: 5386
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Control border color

Post by mk-soft »

I haven't found it either... Use lightGray or systemGrayColor

Link https://developer.apple.com/documentati ... ntrolcolor

Code: Select all

;-TOP
; by mk-soft 

Procedure OSX_NSColorToRGBA(NSColor)
  Protected.cgfloat red, green, blue, alpha
  Protected nscolorspace, rgba
  nscolorspace = CocoaMessage(0, nscolor, "colorUsingColorSpaceName:$", @"NSCalibratedRGBColorSpace")
  If nscolorspace
    CocoaMessage(@red, nscolorspace, "redComponent")
    CocoaMessage(@green, nscolorspace, "greenComponent")
    CocoaMessage(@blue, nscolorspace, "blueComponent")
    CocoaMessage(@alpha, nscolorspace, "alphaComponent")
    rgba = RGBA(red * 255.0, green * 255.0, blue * 255.0, alpha * 255.0)
    ProcedureReturn rgba
  EndIf
EndProcedure

Procedure OSX_NSColorToRGB(NSColor)
  Protected.cgfloat red, green, blue
  Protected r, g, b, a
  Protected nscolorspace, rgb
  nscolorspace = CocoaMessage(0, nscolor, "colorUsingColorSpaceName:$", @"NSCalibratedRGBColorSpace")
  If nscolorspace
    CocoaMessage(@red, nscolorspace, "redComponent")
    CocoaMessage(@green, nscolorspace, "greenComponent")
    CocoaMessage(@blue, nscolorspace, "blueComponent")
    rgb = RGB(red * 255.0, green * 255.0, blue * 255.0)
    ProcedureReturn rgb
  EndIf
EndProcedure

Procedure.i BlendColor(Color1.i, Color2.i, Scale.i=50) ; Thanks to Thorsten
  Protected.i R1, G1, B1, R2, G2, B2
  Protected.f Blend = Scale / 100
  R1 = Red(Color1): G1 = Green(Color1): B1 = Blue(Color1)
  R2 = Red(Color2): G2 = Green(Color2): B2 = Blue(Color2)
  ProcedureReturn RGB((R1*Blend) + (R2 * (1-Blend)), (G1*Blend) + (G2 * (1-Blend)), (B1*Blend) + (B2 * (1-Blend)))
EndProcedure


If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200)
  If StartDrawing(CanvasOutput(0))
    nscolor = CocoaMessage(0, 0, "NSColor controlBackgroundColor")
    color = BlendColor(OSX_NSColorToRGB(nscolor), $FFFFFF, 85) ; Correction for solid gadget
    Box(0, 0, 200, 200, color)
    nscolor = CocoaMessage(0, 0, "NSColor systemGrayColor")
    color = OSX_NSColorToRGB(nscolor)
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(10, 10, 180, 180, color)
    StopDrawing()
  EndIf
      
  Repeat
    Event = WaitWindowEvent()
    
  Until Event = #PB_Event_CloseWindow
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Control border color

Post by wombats »

Thank you. I guess that's close enough, but I would prefer to have the actual color in case the user is in Dark Mode.
User avatar
mk-soft
Always Here
Always Here
Posts: 5386
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Control border color

Post by mk-soft »

wombats wrote:Thank you. I guess that's close enough, but I would prefer to have the actual color in case the user is in Dark Mode.
NSColor systemGrayColor also fits in dark and light mode :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Control border color

Post by wombats »

mk-soft wrote:
wombats wrote:Thank you. I guess that's close enough, but I would prefer to have the actual color in case the user is in Dark Mode.
NSColor systemGrayColor also fits in dark and light mode :wink:
Good point! Haha. Thanks again for the example.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Control border color

Post by wilbert »

wombats wrote:Is there a color I'm missing
You seem to have all of them. Maybe it has to do with the alpha channel or different color space.
deviceRGBColorSpace seems to return the same results as the drawSwatchInRect on a canvas method.
When using genericRGBColorSpace / NSCalibratedRGBColorSpace, grid color for example returns 193 instead of 204 for r,g and b.

Code: Select all

ColorList = CocoaMessage(0, 0, "NSColorList colorListNamed:$", @"System")
If ColorList
  ColorSpace = CocoaMessage(0, 0, "NSColorSpace deviceRGBColorSpace")
  Keys = CocoaMessage(0, ColorList, "allKeys")
  NumKeys = CocoaMessage(0, Keys, "count")
  For k = 1 To NumKeys
    Key = CocoaMessage(0, Keys, "objectAtIndex:", k - 1)
    Color = CocoaMessage(0, ColorList, "colorWithKey:", Key)
    Color = CocoaMessage(0, Color, "colorUsingColorSpace:", ColorSpace)
    If Color
      KeyName.s = PeekS(CocoaMessage(0, Key, "UTF8String"), -1, #PB_UTF8)
      CocoaMessage(@r.CGFloat, Color, "redComponent")
      CocoaMessage(@g.CGFloat, Color, "greenComponent")
      CocoaMessage(@b.CGFloat, Color, "blueComponent")
      CocoaMessage(@a.CGFloat, Color, "alphaComponent")
      Debug KeyName + " = RGBA(" + Str(r*255) + "," + Str(g*255) + "," + Str(b*255) + "," + Str(a*255) + ")"
    EndIf
  Next
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply