RoundBoxedGradient()

Share your advanced PureBasic knowledge/code with the community.
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

RoundBoxedGradient()

Post by STARGÅTE »

RoundBoxedGradient() works by the same way as
- RoundBox (representation)
- BoxedGradient (GradientColor)

Little difference is: is omitted RoundY : set RoundY = RoundX.

Code: Select all

Structure RoundBoxedGradient
  x.i
  y.i
  Width.i
  Height.i
  RoundX.i
  RoundY.i
EndStructure


Procedure.f RoundBoxedGradientCallback(x.i, y.i)
  Shared RoundBoxedGradient.RoundBoxedGradient
  With RoundBoxedGradient
    x - \x
    y - \y
    If x < \RoundX 
      If y < \RoundY
        ProcedureReturn Sqr(Pow((\RoundX-x)/\RoundX,2)+Pow((\RoundY-y)/\RoundY,2))
      ElseIf y >= \Height-\RoundY
        ProcedureReturn Sqr(Pow((\RoundX-x)/\RoundX,2)+Pow((\RoundY+y+1-\Height)/\RoundY,2))
      Else
        ProcedureReturn (\RoundX-x)/\RoundX
      EndIf
    ElseIf x >= \Width-\RoundX
      If y < \RoundY
        ProcedureReturn Sqr(Pow((\RoundX+x+1-\Width)/\RoundX,2)+Pow((\RoundY-y)/\RoundY,2))
      ElseIf y >= \Height-\RoundY
        ProcedureReturn Sqr(Pow((\RoundX+x+1-\Width)/\RoundX,2)+Pow((\RoundY+y+1-\Height)/\RoundY,2))
      Else
        ProcedureReturn (\RoundX+x+1-\Width)/\RoundX
      EndIf
    Else
      If y < \RoundY
        ProcedureReturn (\RoundY-y)/\RoundY
      ElseIf y >= \Height-\RoundY
        ProcedureReturn (\RoundY+y+1-\Height)/\RoundY
      Else
        ProcedureReturn 0
      EndIf   
    EndIf
  EndWith
EndProcedure


Procedure RoundBoxedGradient(x.i, y.i, Width.i, Height.i, RoundX.i, RoundY.i=#PB_Default)
  Shared RoundBoxedGradient.RoundBoxedGradient
  With RoundBoxedGradient
    \x = x
    \y = y
    \Width = Width
    \Height = Height
    \RoundX = RoundX
    \RoundY = RoundY
    If RoundY=#PB_Default : \RoundY = RoundX : EndIf
    If \RoundX < 0 : \RoundX = 0 : EndIf
    If \RoundY < 0 : \RoundY = 0 : EndIf
    If \RoundX*2 > Width : \RoundX = Width/2 : EndIf
    If \RoundY*2 > Height : \RoundY = Height/2 : EndIf
  EndWith
  CustomGradient(@RoundBoxedGradientCallback())
EndProcedure
Example:

Code: Select all

Enumeration
  #Window : #Gadget : #Image
EndEnumeration

CreateImage(#Image, 400, 400, 24)

StartDrawing(ImageOutput(#Image))
  DrawingMode(#PB_2DDrawing_Gradient)
  GradientColor(1.0, $FF0000)
  GradientColor(0.0, $0000FF)
  RoundBoxedGradient(0,0,200,200,50,50)
  RoundBox(0,0,200,200,50,50)
  RoundBoxedGradient(200,0,200,400,100,100)
  RoundBox(200,0,200,400,100,100)
  RoundBoxedGradient(0,200,200,200,80,40)
  RoundBox(0,200,200,200,80,40)
StopDrawing()

OpenWindow(#Window, 0, 0, ImageWidth(#Image), ImageHeight(#Image), "Image", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  ImageGadget(#Gadget, 0, 0, ImageWidth(#Image), ImageHeight(#Image), ImageID(#Image))

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Image
Last edited by STARGÅTE on Fri Jul 23, 2010 1:10 pm, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: RoundBoxedGradient()

Post by rsts »

Nice.

Thanks for sharing :)

cheers
Post Reply