Page 1 of 1

Gradient Fill windows

Posted: Mon Jul 26, 2004 10:23 pm
by Flype
let's try this funny window fx

First this is the include file GradientFill.pb :

Code: Select all

;-

Enumeration ; #GF_Orientation
  #GF_Left2Right
  #GF_Top2Bottom
EndEnumeration

;-

Structure GF_Struct
  Orientation.b
  ImgSrc.l
  ImgDst.l
EndStructure

Global GF.GF_Struct

;-

Procedure.l GF_Max(x.l,y.l)
  If x>y
    ProcedureReturn x
  EndIf
  ProcedureReturn y
EndProcedure
Procedure.l GF_Max3(x.l,y.l,z.l)
  ProcedureReturn GF_Max(GF_Max(x,y),z)
EndProcedure
Procedure.l GF_Clamp(x.l,min.l,max.l)
  If x<min
    ProcedureReturn min
  EndIf
  If x>max
    ProcedureReturn max
  EndIf
  ProcedureReturn x
EndProcedure

;-

Procedure GF_Init(Orientation,red,green,blue)
  
  w.l=800
  h.l=600
  
  GF\Orientation = Orientation
  
  Select GF\Orientation
    Case #GF_Top2Bottom
      a.f=GF_Max3(red,green,blue)/h : iw=1 : ih=h : imax=h
    Case #GF_Left2Right
      a.f=GF_Max3(red,green,blue)/w : iw=w : ih=1 : imax=w
  EndSelect
  
  If GF\ImgSrc <> #Null
    FreeImage(GF\ImgSrc)
  EndIf
  
  GF\ImgSrc = CreateImage(#PB_Any,iw,ih)
  
  StartDrawing(ImageOutput())
    For i=0 To imax
      z=i*a
      Select GF\Orientation
        Case #GF_Left2Right
          Plot(i,0,RGB(GF_Clamp(red-z,0,255),GF_Clamp(green-z,0,255),GF_Clamp(blue-z,0,255)))
        Case #GF_Top2Bottom
          Plot(0,i,RGB(GF_Clamp(red-z,0,255),GF_Clamp(green-z,0,255),GF_Clamp(blue-z,0,255)))
      EndSelect
    Next
  StopDrawing()  
  
EndProcedure
Procedure GF_Draw(hWindow.l)
  
  CopyImage(GF\ImgSrc,GF\ImgDst)
  
  Select GF\Orientation
    Case #GF_Left2Right : ResizeImage(GF\ImgDst,WindowWidth(),1)
    Case #GF_Top2Bottom : ResizeImage(GF\ImgDst,1,WindowHeight())
  EndSelect
  
  SetWinBackgroundImage(hWindow,UseImage(GF\ImgDst))
  UpdateWindow_(hWindow)
  
EndProcedure
And, finally, an example :

Code: Select all

IncludeFile "GradientFill.pb"

OpenWindow(0,0,0,800,600,$CD0001,"Gradient Fill")
CreateGadgetList(WindowID(0))
ButtonGadget(0,0,0,100,30,"Exit")
ButtonGadget(1,0,0,100,30,"Change")
ButtonGadget(2,0,0,100,30,"Mode",#PB_Button_Toggle)
GF_Init(#GF_Top2Bottom,57,109,164)
CreateStatusBar(0,WindowID(0))
StatusBarText(0,0,"")

Repeat
  Select WaitWindowEvent()
    Case #WM_Size
      GF_Draw(WindowID(0))
      ResizeGadget(0,WindowWidth()-108,10,-1,-1)
      ResizeGadget(1,WindowWidth()-216,10,-1,-1)
      ResizeGadget(2,WindowWidth()-324,10,-1,-1)
    Case #PB_Event_CloseWindow : Break
    Case #PB_Event_Gadget
      Select EventGadgetID()
        Case 0 : Break
        Case 1
          GF_Init(GetGadgetState(2),Random(255),Random(255),Random(255))
          GF_Draw(WindowID(0))
      EndSelect
  EndSelect
ForEver