G4C

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

WINBIG 211 65 233 105 'Palette'
wintype 11110001
resinfo 8 640 256  ; info for auto-resize

BOX 0 0 233 105 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 8 4 217 28 
	gadid 5
	attr resize 0022
	gadhelp 'Click on a color, (or click RMB anywhere else)'
	gosub palette.gc $type

XHSLIDER 27 33 167 12 R red 0 15 0 %2ld
	attr resize 0220
	gadhelp "Adjust RED intensity"
	setcolor palette.gc $$color.num $red $green $blue
	gadid 1

XHSLIDER 27 45 167 12 G green 0 15 0 %2ld
	attr resize 0220
	gadhelp "Adjust GREEN intensity"
	setcolor palette.gc $$color.num $red $green $blue
	gadid 2

XHSLIDER 27 57 167 12 B blue 0 15 0 %2ld
	attr resize 0220
	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 'Color $num of $total'


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

XBUTTON 9 72 72 14 Undo     ; undo last changes
	attr resize 1210
	setcolor palette.gc $$color.num $oldred $oldgreen $oldblue
	gosub palette.gc update


; ---------------------------------------------------------------
;	Load a previously saved palette 
;	(new IFF or old gui type palettes..)
; ---------------------------------------------------------------

XBUTTON 81 72 72 14 Load..
	attr resize 1210
	gadhelp "Load an IFF or an old GUI type palette"
	filename = ''
	ReqFile -1 -1 300 -40 'Load Palette gui:' LOAD filename 'GUIs:tools/palette'
	if $filename > ' '

	   if $filename H= "G4C"				; old type palettes..
	      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

		elseif $filename H= "FORM????ILBM"
			palette load $filename tempimage
			palette set tempimage '*'
			freeimage tempimage
	      gosub palette.gc update			; redraw the sliders etc

	   else
	      ezreq 'Please choose a Gui4Cli script pallete' OK ''
	   endif

	endif


; ---------------------------------------------------------------
;	Save a palette as normal iff
; ---------------------------------------------------------------

XBUTTON 153 72 72 14 Save..
	attr resize 1210
	gadhelp "Save palette as normal IFF palette"
	filename = ''
	ReqFile -1 -1 300 -40 'Save Palette as:' SAVE filename 'Guis:tools/palette'
	if $filename = ''
	   stop
	endif

	setwintitle palette.gc 'Saving Palette...'
	palette get '*' tempimage
	palette save tempimage $filename
	freeimage tempimage
	gosub palette.gc update


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

XBUTTON 9 87 72 14 Copy
	attr resize 1210
	gosub palette.gc fixvalues
	type = 'copy'

XBUTTON 81 87 72 14 Exchange
	attr resize 1210
	gosub palette.gc fixvalues
	type = 'exchange'

XBUTTON 153 87 72 14 Spread
	attr resize 1210
	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

