Draw round corners with different colors
Posted: Sat May 18, 2024 6:50 pm
I'm trying to draw a box with rounded corners but with each half corner drawn with a different color. I adapted this great code from eddy:
https://www.purebasic.fr/english/viewto ... t=roundbox
With big corner radius it seems to work but not with small ones, this is how it looks:

I'm pretty bad at drawing, i spent a lot of time with this with no success!
Can someone help?, here's the code:
https://www.purebasic.fr/english/viewto ... t=roundbox
With big corner radius it seems to work but not with small ones, this is how it looks:

I'm pretty bad at drawing, i spent a lot of time with this with no success!
Can someone help?, here's the code:
Code: Select all
EnableExplicit
Structure BORDER_RADIUS
topLeft.d
topRight.d
bottomLeft.d
bottomRight.d
EndStructure
Procedure.l drawRoundBox(x.d, y.d, width.d, height.d, lineWidth.d, *bRadius.BORDER_RADIUS)
Protected.d px, py
Protected.l xColor, yColor
xColor = RGBA(255, 0, 0, 255)
yColor = RGBA(0, 0, 255, 255)
;TOP BORDER
MovePathCursor(x + *bRadius\topLeft, y, #PB_Path_Relative)
;Top Left half corner
VectorSourceColor(xColor)
AddPathEllipse(0, *bRadius\topLeft, *bRadius\topLeft, *bRadius\topLeft, 225, -90, #PB_Path_Relative)
;Border top line
AddPathLine(width - *bRadius\topLeft - *bRadius\topRight, 0, #PB_Path_Relative)
px = PathCursorX() : py = PathCursorY()
;Top right half corner
AddPathEllipse(0, *bRadius\topRight, *bRadius\topRight, *bRadius\topRight, -90, -45, #PB_Path_Connected | #PB_Path_Relative)
StrokePath(lineWidth)
;RIGHT BORDER
;Top right half corner
MovePathCursor(px, py)
VectorSourceColor(yColor)
AddPathEllipse(0, *bRadius\topRight, *bRadius\topRight, *bRadius\topRight, -45, 0, #PB_Path_Relative)
;Border right line
AddPathLine(0, Height-*bRadius\topRight-*bRadius\bottomRight, #PB_Path_Relative)
px = PathCursorX() : py = PathCursorY()
;Bottom right half corner
AddPathEllipse(-*bRadius\bottomRight, 0, *bRadius\bottomRight, *bRadius\bottomRight, 0, 45, #PB_Path_Connected | #PB_Path_Relative)
StrokePath(lineWidth)
;BOTTOM BORDER
;Bottom down half corner
MovePathCursor(px, py)
AddPathEllipse(-*bRadius\bottomRight, 0, *bRadius\bottomRight, *bRadius\bottomRight, 45, 90, #PB_Path_Relative)
VectorSourceColor(xColor)
;bottom border line
AddPathLine(*bRadius\bottomRight + *bRadius\bottomLeft - width, 0, #PB_Path_Relative)
px = PathCursorX() : py = PathCursorY()
;Bottom left half corner
AddPathEllipse(0, -*bRadius\bottomLeft, *bRadius\bottomLeft, *bRadius\bottomLeft, 90, 135, #PB_Path_Connected | #PB_Path_Relative)
StrokePath(lineWidth)
;LEFT BORDER
;Bottom left half corner
MovePathCursor(px, py)
VectorSourceColor(yColor)
AddPathEllipse(0, -*bRadius\bottomLeft, *bRadius\bottomLeft, *bRadius\bottomLeft, 135, 180, #PB_Path_Relative)
;Left border line
AddPathLine(x, y + *bRadius\topLeft)
px = PathCursorX() : py = PathCursorY()
;Top left half border
AddPathEllipse(*bRadius\topLeft, 0, *bRadius\topLeft, *bRadius\topLeft, 180, -135, #PB_Path_Relative) ;180, -135
StrokePath(lineWidth)
ProcedureReturn #True
EndProcedure
Procedure AddPathRoundBox(x.d, y.d, Width.d, Height.d, lineWidth.d, *bRadius.BORDER_RADIUS)
;Clockwise
MovePathCursor(x + *bRadius\topLeft, y, #PB_Path_Relative)
;top left corner
AddPathEllipse(0, *bRadius\topLeft, *bRadius\topLeft, *bRadius\topLeft, 180, -90, #PB_Path_Relative)
;Border top
AddPathLine(Width - *bRadius\topLeft - *bRadius\topRight, 0, #PB_Path_Relative)
;
; ;Top right corner
AddPathEllipse(0, *bRadius\topRight, *bRadius\topRight, *bRadius\topRight, -90, 0, #PB_Path_Connected | #PB_Path_Relative)
;
; ;border right
AddPathLine(0, Height-*bRadius\topRight-*bRadius\bottomRight, #PB_Path_Relative)
;
; ;bottom right corner
AddPathEllipse(-*bRadius\bottomRight, 0, *bRadius\bottomRight, *bRadius\bottomRight, 0, 90, #PB_Path_Connected | #PB_Path_Relative)
;
; ;bottom border
AddPathLine(*bRadius\bottomRight + *bRadius\bottomLeft - Width, 0, #PB_Path_Relative)
;
; ;bottom left corner
AddPathEllipse(0, -*bRadius\bottomLeft, *bRadius\bottomLeft, *bRadius\bottomLeft, 90, 180, #PB_Path_Connected | #PB_Path_Relative)
;
ClosePath() ;border left
StrokePath(lineWidth, #PB_Path_RoundEnd)
MovePathCursor(0, -*bRadius\topLeft, #PB_Path_Relative)
EndProcedure
Procedure main()
Protected.l ev
Protected.BORDER_RADIUS bRadius
Protected.d lineWidth
OpenWindow(0, 0, 0, 800, 600, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, 800, 600)
If StartVectorDrawing(CanvasVectorOutput(0))
bRadius\topLeft = 6
bRadius\topRight = 6
bRadius\bottomLeft = 6
bRadius\bottomRight = 6
lineWidth = 8
drawRoundBox(DesktopScaledX(10), DesktopScaledY(10), DesktopScaledX(350), DesktopScaledY(350), lineWidth, @bRadius)
AddPathRoundBox(DesktopScaledX(400), DesktopScaledY(10), DesktopScaledX(350), DesktopScaledY(350), lineWidth, @bRadius)
StopVectorDrawing()
EndIf
Repeat
ev = WaitWindowEvent()
Until ev = #PB_Event_CloseWindow
EndProcedure
main()