Page 1 of 1

Control border color

Posted: Fri Apr 05, 2019 11:45 am
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

Re: Control border color

Posted: Fri Apr 05, 2019 4:30 pm
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

Re: Control border color

Posted: Fri Apr 05, 2019 6:43 pm
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.

Re: Control border color

Posted: Fri Apr 05, 2019 9:47 pm
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:

Re: Control border color

Posted: Fri Apr 05, 2019 10:38 pm
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.

Re: Control border color

Posted: Mon Apr 15, 2019 3:32 pm
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