Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/Quartz Gradients
Function:
Required plugins for this example: MBS MacCG Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/Quartz Gradients
This example is the version from Sat, 23th Mar 2018.
Project "Quartz Gradients.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawAxialGradient g, true, RGB(68, 154, 228), RGB(0, 95, 206), 0, 0, me.Width, me.Height End EventHandler
End Control
Control Canvas2 Inherits Canvas
ControlInstance Canvas2 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawAxialGradient g, false, RGB(68, 154, 228), RGB(0, 95, 206), 0, 0, me.Width, me.Height End EventHandler
End Control
Control Canvas3 Inherits Canvas
ControlInstance Canvas3 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawAxialGradient g, false, RGB(251, 251, 251), RGB(101, 102, 102), 0, 0, me.Width, me.Height End EventHandler
End Control
Control Canvas4 Inherits Canvas
ControlInstance Canvas4 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawAxialGradient g, true, RGB(151, 151, 151), RGB(101, 102, 102), 0, 0, me.Width, me.Height End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control Canvas5 Inherits Canvas
ControlInstance Canvas5 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawRadialGradient g, true, RGB(68, 154, 228), RGB(0, 95, 206), 0, 0, me.Width, me.Height, 10, 20 End EventHandler
End Control
Control Canvas6 Inherits Canvas
ControlInstance Canvas6 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawRadialGradient g, false, RGB(68, 154, 228), RGB(0, 95, 206), 0, 0, me.Width, me.Height, 10, 30 End EventHandler
End Control
Control Canvas7 Inherits Canvas
ControlInstance Canvas7 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawRadialGradient g, false, RGB(251, 251, 251), RGB(101, 102, 102), 0, 0, me.Width, me.Height, 15, 40 End EventHandler
End Control
Control Canvas8 Inherits Canvas
ControlInstance Canvas8 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) DrawRadialGradient g, true, RGB(151, 151, 151), RGB(101, 102, 102), 0, 0, me.Width, me.Height,5,20 End EventHandler
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Module CGShading
Const pi = 3.14159265358979
Sub DrawAxialGradient(dest as Graphics, vertical as Boolean, color1 as Color, color2 as Color, Left as Integer, Top as Integer, Width as Integer, Height as Integer) dim rect as CGRectMBS dim shading as CGShadingMBS dim domainAndRange as MemoryBlock dim startPoint, endPoint as CGPointMBS dim colorSpace as CGColorSpaceMBS = CGColorSpaceMBS.CreateDeviceRGB if not vertical then startPoint = new CGPointMBS(0,Height) endPoint = new CGPointMBS(0,0) else startPoint = new CGPointMBS(0,0) endPoint = new CGPointMBS(width,0) end if dim f as new MyCGFunction f.r1 = color1.Red / 255.0 f.g1 = color1.Green / 255.0 f.b1 = color1.Blue / 255.0 f.a1 = 1.0 f.r2 = color2.Red / 255.0 f.g2 = color2.Green / 255.0 f.b2 = color2.Blue / 255.0 f.a2 = 1.0 #if Target32Bit then domainAndRange = New MemoryBlock(4 * 8) domainAndRange.SingleValue(0*4) = 0 domainAndRange.SingleValue(1*4) = 1 domainAndRange.SingleValue(2*4) = 0 domainAndRange.SingleValue(3*4) = 1 domainAndRange.SingleValue(4*4) = 0 domainAndRange.SingleValue(5*4) = 1 domainAndRange.SingleValue(6*4) = 0 domainAndRange.SingleValue(7*4) = 1 #else domainAndRange = New MemoryBlock(8 * 8) domainAndRange.DoubleValue(0*8) = 0 domainAndRange.DoubleValue(1*8) = 1 domainAndRange.DoubleValue(2*8) = 0 domainAndRange.DoubleValue(3*8) = 1 domainAndRange.DoubleValue(4*8) = 0 domainAndRange.DoubleValue(5*8) = 1 domainAndRange.DoubleValue(6*8) = 0 domainAndRange.DoubleValue(7*8) = 1 #endif dim contextHandle as integer = dest.Handle(dest.HandleTypeCGContextRef) dim context as CGContextMBS = CGContextMBS.contextWithCGContext(contextHandle) if context<>nil then context.SaveGState // Clip to the Specified bounds rect=new CGRectMBS(left, top, Width, Height) f.Create 1, domainAndRange, 4, domainAndRange System.DebugLog "f: "+hex(f.Handle) // Draw Gradient shading = CGShadingCreateAxialMBS(colorSpace, startPoint, endPoint, f, false, false) System.DebugLog "shading: "+hex(shading.Handle) context.DrawShading shading // Close CGContext context.RestoreGState 'context.Flush end if End Sub
Sub DrawRadialGradient(dest as Graphics, vertical as Boolean, color1 as Color, color2 as Color, Left as Integer, Top as Integer, Width as Integer, Height as Integer, startRadius as single, endRadius as single) dim rect as CGRectMBS dim shading as CGShadingMBS dim domainAndRange as MemoryBlock dim startPoint, endPoint as CGPointMBS dim colorSpace as CGColorSpaceMBS = CGColorSpaceMBS.CreateDeviceRGB if not vertical then startPoint = new CGPointMBS(70,70) endPoint = new CGPointMBS(20,20) else startPoint = new CGPointMBS(20,70) endPoint = new CGPointMBS(70,20) end if dim f as new MyCGFunction f.r1 = color1.Red / 255.0 f.g1 = color1.Green / 255.0 f.b1 = color1.Blue / 255.0 f.a1 = 1.0 f.r2 = color2.Red / 255.0 f.g2 = color2.Green / 255.0 f.b2 = color2.Blue / 255.0 f.a2 = 1.0 #if Target32Bit then domainAndRange = New MemoryBlock(4 * 8) domainAndRange.SingleValue(0*4) = 0 domainAndRange.SingleValue(1*4) = 1 domainAndRange.SingleValue(2*4) = 0 domainAndRange.SingleValue(3*4) = 1 domainAndRange.SingleValue(4*4) = 0 domainAndRange.SingleValue(5*4) = 1 domainAndRange.SingleValue(6*4) = 0 domainAndRange.SingleValue(7*4) = 1 #else domainAndRange = New MemoryBlock(8 * 8) domainAndRange.DoubleValue(0*8) = 0 domainAndRange.DoubleValue(1*8) = 1 domainAndRange.DoubleValue(2*8) = 0 domainAndRange.DoubleValue(3*8) = 1 domainAndRange.DoubleValue(4*8) = 0 domainAndRange.DoubleValue(5*8) = 1 domainAndRange.DoubleValue(6*8) = 0 domainAndRange.DoubleValue(7*8) = 1 #endif dim contextHandle as integer = dest.Handle(dest.HandleTypeCGContextRef) dim context as CGContextMBS = CGContextMBS.contextWithCGContext(contextHandle) if context<>nil then context.SaveGState // Clip to the Specified bounds rect=new CGRectMBS(Left, Top, Width, Height) f.Create 1, domainAndRange, 4, domainAndRange // Draw Gradient shading = CGShadingCreateRadialMBS(colorSpace, startPoint, startRadius, endPoint, endRadius, f, false, false) context.DrawShading shading // Close CGContext context.RestoreGState 'context.Flush end if End Sub
End Module
Class MyCGFunction Inherits CGFunctionMBS
EventHandler Sub Evaluate(Input as memoryblock, Output as memoryblock) #if Target32Bit then dim inVal as Single = Input.SingleValue(0) Output.SingleValue(0*4) = (1.0 - inVal) * r1 + inVal * r2 Output.SingleValue(1*4) = (1.0 - inVal) * g1 + inVal * g2 Output.SingleValue(2*4) = (1.0 - inVal) * b1 + inVal * b2 Output.SingleValue(3*4) = (1.0 - inVal) * a1 + inVal * a2 #else dim inVal as Single = Input.DoubleValue(0) Output.DoubleValue(0*8) = (1.0 - inVal) * r1 + inVal * r2 Output.DoubleValue(1*8) = (1.0 - inVal) * g1 + inVal * g2 Output.DoubleValue(2*8) = (1.0 - inVal) * b1 + inVal * b2 Output.DoubleValue(3*8) = (1.0 - inVal) * a1 + inVal * a2 #endif End EventHandler
Property a1 As single
Property a2 As single
Property b1 As single
Property b2 As single
Property g1 As single
Property g2 As single
Property r1 As single
Property r2 As single
End Class
End Project

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS MacCG Plugin.




Links
MBS Xojo blog