It is currently Tue Sep 17, 2019 3:42 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Module NSColor
PostPosted: Mon Apr 15, 2019 11:17 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1902
Location: Germany
Before I lose my code again. :wink:
Code:
;-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

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Module NSColor
PostPosted: Tue Aug 20, 2019 5:54 pm 
Offline
New User
New User

Joined: Sun Aug 18, 2019 8:07 pm
Posts: 5
mk-soft wrote:
Before I lose my code again. :wink:
Code:
;-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 [url=https://tracktrace.onl/usps/]usps tracking[/url]
      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

usps tracking

Thanks for providing the code.

Regards
Oliver


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye