Page 1 of 1

RoundBoxedGradient()

Posted: Tue Jun 22, 2010 11:09 am
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

Re: RoundBoxedGradient()

Posted: Tue Jun 22, 2010 12:03 pm
by rsts
Nice.

Thanks for sharing :)

cheers