G4C

; Palette.gc by D.Keletsekis & J.Collett
; ---------------------------------------------------------------
; A full palette gui with many capabilities
; Press CONTROL-J to make it jump screens
; ---------------------------------------------------------------

winbig -1 -1 235 120 'Palette'
wintype 11110001

box 0 0 0 0 out button

; ---------------------------------------------------------------
;	system events
; ---------------------------------------------------------------

xonload
guiopen palette.gc

xonopen
info palette palette.gc/5	; get info on *our* platte
type = 'update'			; set the subroutine type
gosub palette.gc update		; Subroutine

xonRMB		
; on rmb we update to the color under mouse position
update palette.gc 5 $$mouse.color
; and update since we did not click on the palette
gosub palette.gc update

xonclose
guiquit palette.gc

; ---------------------------------------------------------------
;	palette & sliders
; ---------------------------------------------------------------

xPalette 10 5 225 30
gadid 5
gadhelp 'Click on a color, (or click RMB anywhere else)'
gosub palette.gc $type

xHSlider 40 40 160 12 R red 0 15 0 %2ld
gadhelp "Adjust RED intensity"
setcolor palette.gc $$color.num $red $green $blue
gadid 1

xHSlider 40 52 160 12 G green 0 15 0 %2ld
gadhelp "Adjust GREEN intensity"
setcolor palette.gc $$color.num $red $green $blue
gadid 2

xHSlider 40 64 160 12 B blue 0 15 0 %2ld
gadhelp "Adjust BLUE intensity"
setcolor palette.gc $$color.num $red $green $blue
gadid 3


; ---------------------------------------------------------------
;	routine to update sliders & get variables
; ---------------------------------------------------------------

xRoutine update
; get the current values from internal variables
red   = $$color.r
green = $$color.g
blue  = $$color.b
num   = $$color.num
total = $$color.total

oldred   = $red		; store old values for undo
oldgreen = $green
oldblue  = $blue

update palette.gc 1 $red	; Update sliders
update palette.gc 2 $green
update palette.gc 3 $blue

setwintitle palette.gc 'Palette color $num of $total'


; ---------------------------------------------------------------
;	Undo button
; ---------------------------------------------------------------

xbutton 10 85 70 14 Undo     ; undo last changes
setcolor palette.gc $$color.num $oldred $oldgreen $oldblue
gosub palette.gc update


; ---------------------------------------------------------------
;	Load a previously saved palette 
;	(i.e. a normal GUI with a list of setcolor commands)
; ---------------------------------------------------------------

xbutton  80 85 70 14 Load..
gadhelp "Load a previously saved gui, which sets the palette."
filename = ''
ReqFile -1 -1 300 -40 'Load Palette gui:' LOAD filename 'GUIs:tools/palette'
if $filename > ' '
   if $filename H= "G4C"
      guiload $filename palette.gc	; pass our gui name as argument
      update palette.gc 5 1		; go back to color No 1
      info palette palette.gc/5		; get info on palette
      gosub palette.gc update		; redraw the sliders etc
   else
      ezreq 'Please choose a Gui4Cli script pallete' OK ''
   endif
endif


; ---------------------------------------------------------------
;	Save a palette
;	make a loop and write a gui to set all colors
; ---------------------------------------------------------------

xbutton  150 85 70 14 Save..
gadhelp "Save the current palette as a Gui."
filename = ''
ReqFile -1 -1 300 -40 'Save Palette as:' SAVE filename 'Guis:tools/palette'
if $filename = ''
   stop
endif
guiwindow palette.gc wait
setwintitle palette.gc 'Saving Palette...'
info palette palette.gc/5       ; make sure we're talcking about *our* palette
.palette = 'G4C\n\n;Palette\n\nxOnLoad gui\n'
count = 0
while $count < $total
    update palette.gc 5 $count  ; choose each colors in sequence
    append env:.palette 'setcolor \$gui $count $$color.r $$color.g $$color.b\n'
    ++count
endwhile
extract filename file guiname
append env:.palette 'guiquit $guiname\n'
copy env:.palette $filename
delete env:.palette
guiwindow palette.gc resume
update palette.gc 5 1
info palette palette.gc/5
gosub palette.gc update


; ---------------------------------------------------------------
; 	Copy, Exchange, Spread buttons
; ---------------------------------------------------------------

xbutton  10 100 70 14 Copy
gosub palette.gc fixvalues
type = 'copy'

xbutton  80 100 70 14 Exchange
gosub palette.gc fixvalues
type = 'exchange'

xbutton  150 100 70 14 Spread
gosub palette.gc fixvalues
type = 'spread'

; store the current values in undo since we'll need them
xRoutine fixvalues
oldred   = $red
oldgreen = $green
oldblue  = $blue

; ---------------------------------------------------------------
;	routine to copy values
; ---------------------------------------------------------------

xRoutine copy
;Set new colour to old values
setcolor palette.gc $$color.num $oldred $oldgreen $oldblue
type = 'update'		; Reset routine flag
setwintitle palette.gc 'Copied color $num to $$color.num'
num = $$color.num
update palette.gc 5 $num
gosub palette.gc update


; ---------------------------------------------------------------
;	routine to exchange values
; ---------------------------------------------------------------

xRoutine exchange
pal.on = $num       ; Store existing colour number
red = $$color.r     ; get new values  
green = $$color.g
blue = $$color.b
num = $$color.num
;Set each colour to other values
setcolor palette.gc $num  $oldred $oldgreen $oldblue
setcolor palette.gc $pal.on  $red $green $blue
type = 'update'             ; Reset routine flag
; Update window title
setwintitle palette.gc 'Exchanged colors $pal.on and $num'
update palette.gc 5 $num
gosub palette.gc update


; ---------------------------------------------------------------
;	Routine to do the spreading.
; ---------------------------------------------------------------

xROUTINE spread 
; use local variables..
local col2/r2/g2/b2/rstep/gstep/bstep/sign/colnum/r/g/b
; get the 2nd color values
col2 = $$color.num
r2   = $$color.r 
g2   = $$color.g
b2   = $$color.b

; if there is no spread, return
if $(abs($num - $col2)) <= 1
   return
endif

; which direction are we going
sign = 1
if $num > $col2
   sign = -1
endif

; how many colors are we spreading ?
colnum = $(abs($col2 - $num) - 1)

; work out the change step for each color
rstep  = $(($r2 - $oldred)   / $colnum) 
gstep  = $(($g2 - $oldgreen) / $colnum) 
bstep  = $(($b2 - $oldblue)  / $colnum) 

; set all colors
count = 0
while $count < $colnum
   ++count
   ; use == so we get integers (should add trunc(x) function)
   r == $($oldred   + ($count * $rstep)) * 1
   g == $($oldgreen + ($count * $gstep)) * 1
   b == $($oldblue  + ($count * $bstep)) * 1
   setcolor palette.gc $($num + ($count * $sign)) $r $g $b
endwhile

type = 'update'             ; Reset routine flag
gosub palette.gc update

