Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another way to draw rounded rectangles
#1
This uses SVG format to draw rounded rectangles. 

Code: (Select All)
'Draw rounded rectangles using SVG format
'by James D. Jarvis, use as you wish

Screen _NewImage(800, 500, 32)
_Title "Press any key to draw a new set of rectangles or Q to quit"
Randomize Timer
Dim si As _Unsigned Long
Do
    'refresh screen
    'define the svg description of 10 rounded rectangles
    Cls , _RGB32(100 + Int(Rnd * 150), 100 + Int(Rnd * 150), 100 + Int(Rnd * 150))
    ss$ = ""
    CX = 100: CY = 100: cRAD = 50
    For RR = 1 To 10
        xx = Int(Rnd * (_Width * .75)): yy = Int(Rnd * (_Height * .75))
        cRAD = Int(3 + Rnd * 12)
        WW = Int(30 + Rnd * 300): HH = Int(30 + Rnd * 300)
        ss$ = ss$ + doRrect$(xx, yy, cRAD, cRAD, WW, HH, _RGB32(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256)), _RGB32(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256)), Int(1 + Rnd * 25))
    Next RR
    simg$ = "<svg width='" + _Trim$(Str$(_Width)) + "' height='" + _Trim$(Str$(_Height)) + "'>" + ss$ + " </svg>"
    si = _LoadImage(simg$, 32, "memory") 'load the SVG image
    _PutImage (0, 0), si 'display the SVG image
    _FreeImage si
    Do
        kk$ = InKey$
    Loop Until kk$ <> ""
Loop Until UCase$(kk$) = "Q"
System
Function doRrect$ (x, y, rx, ry, w, h, sk As _Unsigned Long, fk As _Unsigned Long, swid)
    'returns the SVG description of a rounded rectangle
    'x,y are coordinates of rounded rectabgle on the screen
    'rx,ry are the radius of the rectangle corners
    'h,y are height and width of the rectangle
    'sk is the _rgb32 stroke color
    'fk is the _rgb32 fill color
    'swid is the stroke width of the rectangle
    doRrect$ = " <rect x='" + _Trim$(Str$(x)) + "' y='" + _Trim$(Str$(y)) + "' rx='" + _Trim$(Str$(rx)) + "' ry='" + _Trim$(Str$(ry)) + "' width='" + _Trim$(Str$(h)) + "' height='" + _Trim$(Str$(w)) + "' stroke='" + _Trim$(packcolorN$(sk)) + "' fill='" + _Trim$(packcolorN$(fk)) + "' stroke-width='" + _Trim$(Str$(swid)) + "'/>"
End Function

Function packcolorN$ (klr As _Unsigned Long)
    'convert an unsigned long color value into a hexidecimal string # that will be used in an SVG
    Dim As _Unsigned Long rk, gk, bk
    'get the color channels of the unsinged long color
    rk = _Red32(klr)
    gk = _Green32(klr)
    bk = _Blue32(klr)
    'convert those channel values into hexidecimal
    If rk < 16 Then
        r$ = "0" + Hex$(rk) 'put in a padded hexidcimal value
    Else
        r$ = Hex$(rk) 'put in a 2 digit hexidecimal value
    End If
    If gk < 16 Then
        g$ = "0" + Hex$(gk) 'put in a padded hexidcimal value
    Else
        g$ = Hex$(gk) 'put in a 2 digit hexidecimal value
    End If
    If bk < 16 Then
        b$ = "0" + Hex$(bk) 'put in a padded hexidcimal value
    Else
        b$ = Hex$(bk) 'put in a 2 digit hexidecimal value
    End If
    packcolorN$ = "#" + r$ + g$ + b$
End Function
Reply
#2
A simpler version of your packcolor, which you might want to use to help things run a little more efficiently, if that's ever an issue for you:

Code: (Select All)
$Color:32
Print packcolorN(Red), pcN(Red)
Print packcolorN(Green), pcN(Green)
Print packcolorN(Blue), pcN(Blue)
Print packcolorN(Gold), pcN(Gold)
Print packcolorN(BrickRed), pcN(BrickRed)



Function pcN$ (klr As _Unsigned Long)
pcN$ = "#" + Right$("000000" + Hex$(klr And &HFFFFFF), 6)
End Function


Function packcolorN$ (klr As _Unsigned Long)
'convert an unsigned long color value into a hexidecimal string # that will be used in an SVG
Dim As _Unsigned Long rk, gk, bk
'get the color channels of the unsinged long color
rk = _Red32(klr)
gk = _Green32(klr)
bk = _Blue32(klr)
'convert those channel values into hexidecimal
If rk < 16 Then
r$ = "0" + Hex$(rk) 'put in a padded hexidcimal value
Else
r$ = Hex$(rk) 'put in a 2 digit hexidecimal value
End If
If gk < 16 Then
g$ = "0" + Hex$(gk) 'put in a padded hexidcimal value
Else
g$ = Hex$(gk) 'put in a 2 digit hexidecimal value
End If
If bk < 16 Then
b$ = "0" + Hex$(bk) 'put in a padded hexidcimal value
Else
b$ = Hex$(bk) 'put in a 2 digit hexidecimal value
End If
packcolorN$ = "#" + r$ + g$ + b$
End Function
Reply
#3
Thank you. I think I wrote packcolorN$ originally when there was a bug in how the Hexidecimal values were evaluated.   

I can always use a smaller and swifter routine.
Reply
#4
Well the results not impressive but the method is an eye opener!

It looks like SVG does not do transparent alpha but uses fore and backcolors, or Mr Jarvis just didn't bother with alphas?
b = b + ...
Reply
#5
(09-20-2024, 12:48 PM)bplus Wrote: Well the results not impressive but the method is an eye opener!

It looks like SVG does not do transparent alpha but uses fore and backcolors, or Mr Jarvis just didn't bother with alphas?

SVGs keep track of transparency as opacity   with a flag like "stroke-opacity='0.5' and the SVG parser QB64PE uses doesn't seem to recognize that.
Reply




Users browsing this thread: 2 Guest(s)