Module NSColor

MAC OSX spezifisches Forum
Beiträge, die plattformübergreifend sind, gehören ins 'Allgemein'-Forum.
Benutzeravatar
mk-soft
Beiträge: 3312
Registriert: 24.11.2004 13:12
Wohnort: Germany

Module NSColor

Beitrag von mk-soft »

Bevor ich den Code wieder verbummele. :roll:

Code: Alles auswählen

;-TOP
; by mk-soft 
; v1.01
; 15.05.2019

DeclareModule NSColor
  Declare NSColorToRGB(NSColor)
  Declare NSColorToRGBA(NSColor)
  Declare NSColorByNameToRGB(NSColorName.s)
  Declare NSColorByNameToRGBA(NSColorName.s)
  Declare RGBtoNSColor(Color)
  Declare RGBAtoNSColor(Color)
  Declare BlendColor(Color1.i, Color2.i, Scale.i) ; Thanks to Thorsten
EndDeclareModule

Module NSColor
  
  Procedure NSColorToRGB(NSColor)
    Protected.cgfloat red, green, blue
    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 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 NSColorByNameToRGB(NSColorName.s)
    Protected.cgfloat red, green, blue
    Protected nscolorspace, rgb
    nscolorspace = CocoaMessage(0, CocoaMessage(0, 0, "NSColor " + NSColorName), "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 NSColorByNameToRGBA(NSColorName.s)
    Protected.cgfloat red, green, blue, alpha
    Protected nscolorspace, rgba
    nscolorspace = CocoaMessage(0, CocoaMessage(0, 0, "NSColor " + NSColorName), "colorUsingColorSpaceName:$", @"NSCalibratedRGBColorSpace")
    If nscolorspace
      CocoaMessage(@red, nscolorspace, "redComponent")
      CocoaMessage(@green, nscolorspace, "greenComponent")
      CocoaMessage(@blue, nscolorspace, "blueComponent")
      CocoaMessage(@alpha, nscolorspace, "alphaComponent")
      rgb = RGBA(red * 255.0, green * 255.0, blue * 255.0, alpha * 255.0)
      ProcedureReturn rgb
    EndIf
  EndProcedure
  
  Procedure RGBtoNSColor(Color)
    Protected Alpha.CGFloat, Blue.CGFloat, Green.CGFloat, Red.CGFloat
    Red = Red(Color) / 255
    Green = Green(Color) / 255
    Blue = Blue(Color) / 255
    Alpha = 1.0
    ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithDeviceRed:@", 
                 @Red, "green:@", @Green, "blue:@", @Blue, "alpha:@", @Alpha)
  EndProcedure
  
  Procedure RGBAtoNSColor(Color)
    Protected Alpha.CGFloat, Blue.CGFloat, Green.CGFloat, Red.CGFloat
    Red = Red(Color) / 255
    Green = Green(Color) / 255
    Blue = Blue(Color) / 255
    Alpha = Alpha(Color) / 255
    ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithDeviceRed:@", 
                 @Red, "green:@", @Green, "blue:@", @Blue, "alpha:@", @Alpha)
  EndProcedure
  
  Procedure BlendColor(Color1.i, Color2.i, Scale.i) ; 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
  
EndModule

;- End Module

CompilerIf #PB_Compiler_IsMainFile
  
  UseModule NSColor
  
  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, WindowID(0), "backgroundColor")
      ;color = BlendColor(NSColorToRGB(nscolor), $FFFFFF, 90) ; Correction for solid gadget
      color = BlendColor(NSColorByNameToRGB("controlBackgroundColor"), $FFFFFF, 85) ; Correction for solid gadget
      Box(0, 0, 200, 200, color)
      color = NSColorByNameToRGB("lightGrayColor")
      DrawingMode(#PB_2DDrawing_Outlined)
      Box(10, 10, 180, 180, color)
      StopDrawing()
    EndIf
        
    Repeat
      Event = WaitWindowEvent()
      
    Until Event = #PB_Event_CloseWindow
  EndIf
  
CompilerEndIf
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul / OPC-Helper DLL
PB v3.30 / v5.7x - OS Mac Mini OSX 10.xx / Window 10 Pro. (X64) /Window 7 Pro. (X64) / Window XP Pro. (X86) / Ubuntu 14.04
Downloads auf My Webspace