*		filename: palette.asm
*
*		1/22/90
*
*		Authors: Randy Jouett & CJ Fruge
*
*		inspired by c sheppner's "palette tool", of cbm	
*		(actually, ripped almost totally)
*		who was in turn inspired by cheath, of microsmiths.
*
*		"I stand on the shoulders of giants." -- ae
*
*		NOTE: This file was assembled using the CAPE
*		assembler.  Some changes will undoubtedly be
*		necessary for other assemblers.
*	
*		This program was written to be a _stand-alone_
*		replacement for Palette Tool on 1.3 Extras Disk.	
*

	include	"asm.i"			; general amiga includes
;	include "macros.i"		; included by "asm.i"


	BASEREG	B
	OPTIMON
;	ADDSYM

	XLVO	OpenLibrary	
	XLVO	CloseLibrary	
	XLVO	OpenWindow
	XLVO	CloseWindow
	XLVO	WaitPort
	XLVO	GetMsg	
	XLVO	ReplyMsg
	XLVO	AllocMem
	XLVO	FreeMem
	XLVO	SetAPen
	XLVO	SetBPen
	XLVO	SetDrMd
	XLVO	DrawBorder
	XLVO	Draw
	XLVO	Move
	XLVO	Text
	XLVO	ModifyProp
	XLVO	GetRGB4
	XLVO	LoadRGB4
	XLVO	RectFill
	XLVO	FindTask
	XLVO	Forbid
	
	XREF	_LinkerDB	; for base relative addressing	

* ---------------------------------------
* ----- E Q U A T E S -------------------
* ---------------------------------------

* program limits

MXCOLRS	equ	32			; max colors (5 planes)
MXPLNS	equ	5			; max planes
V_EXTRA_HALFBRITE	equ	$80	; not equ'd in asm includes

* window specs

PWIDE	equ	192			; palette window width
PBHIGH	equ	72			; Height offset for window

* Gadget ID's

colID	equ	1			; color gadget ids (col# + 1)
okID	equ	MXCOLRS+1		; id = 33
resID	equ	MXCOLRS+2		; id = 34
canID	equ	MXCOLRS+3		; id = 35
rID	equ	MXCOLRS+4		; id = 36
bID	equ	MXCOLRS+5		; id = 37
gID	equ	MXCOLRS+6		; id = 38

* Proportional RGB Gadgets

PRPX	equ	52			; x coord, relative to bottom
PRPY	equ	-62			; Top prop, relative to bottom
PRPW	equ	90			; rgb gadget width
PRPH	equ	11			; rgb gadget height
RGB_BDY	equ	$FFF			; horizbody value for rgb gadgets

* Text Selection

TXTX	equ	8			; x coord, relative to bottom
TXTY	equ	-17			; y coord, relative to bottom
TXTW	equ	54			; ok, cancel, & reset text height
TXTH	equ	13			; ok, cancel, & reset text width

* Palette Color Gadgets

COLX	equ	10			; x offset of first color gadget
COLY	equ	14			; y offset of first color gadget
COLSW	equ	4			; space between color gads
COLH	equ	10			; Image height
COLDY	equ	(COLH+COLSW)		; y spacing between rows

CHX1	equ	8			; x1 coord for color swatch
CHX2	equ	34			; x2 coord for color swatch

* ---------------------------------------
* ----- M A I N   C O D E   S E C T I O N
* ---------------------------------------

	SECTION	palcode,CODE

	XDEF	_palette		; public reference

_palette:

	movem.l	d2-d7/a2-a6,-(sp)	; Save data & address regs.

	lea	_LinkerDB,a4		; need for base rel addressing

	sub.l	a1,a1			; clear the address reg
	SYS	FindTask,4		; find this task

	move.l	a6,EBase(a4)		; save ExecBase pointer in fast ram
	move.l	d0,a2			; save ptr to process struct
	tst.l	pr_CLI(a2)		; is it CLI?
	bne.s	fromcli			; yep, from cli

	lea	pr_MsgPort(a2),a0	; get message port for this process
	move.l	a0,a2			; save mess port in a2
	SYS	WaitPort		; wait for WB message
	
	move.l	a2,a0			; put port back in a0
	SYS	GetMsg			; get the WB message
	move.l	d0,wb_message(a4)	; save the message		

fromcli:
	lea	gfxname(a4),a1		; open gfx lib
	moveq	#0,d0
	SYS	OpenLibrary
	move.l	d0,GBase(a4)
	beq	nogfx

	lea	intuiname(a4),a1	; open intuition lib	
	moveq	#0,d0
	SYS	OpenLibrary
	move.l	d0,IBase(a4)
	beq	nointui

	move.l	d0,a0			; get IntuitionBase
	move.l	ib_FirstScreen(a0),d1	; get ptr to FirstScreen
	move.l	d1,pScr(a4)		; save the screen ptr
	move.l	d1,a0
	lea	sc_ViewPort(a0),a1	; get effective address
	move.l	a1,pVp(a4)		; store the ViewPort ptr
	move.w	vp_Modes(a1),d1		; get modes in d0
	move.l	d1,d0			; stash for the and
	and.w	#V_HAM,d0		; is it HAM?
	bne	cant_handle		; cant handle HAM
	and.w	#V_EXTRA_HALFBRITE,d1	; is it extra half bright?
	bne	cant_handle		; cant handle EHB
	lea	sc_RastPort(a0),a0	; get rastport
	move.l	rp_BitMap(a0),a0	; get bitmap
	moveq	#0,d0			; clr d0.
	move.b	bm_Depth(a0),d0		; get depth
	move.w	d0,nPlanes(a4)		; store depth
	cmp.w	#MXPLNS,d0		; is planes > 5?
	bgt	cant_handle		; cant handle colors>32, get out
	moveq	#1,d3			; clr d1
	lsl.w	d0,d3			; 1 << depth = # colors
	move.w	d3,nColors(a4)		; save number of colors

* allocate memory for (#colors * (sizeof struct image + gadget))

	move.l	#(gg_SIZEOF+ig_SIZEOF),d0
	mulu.w	d3,d0
	move.l	d0,d2
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	SYS	AllocMem
	move.l	d0,pGads(a4)		; save ptr to mem alloc'ed
	beq	nogadmem		; cudnt get mem

	move.l	d2,gadmemsize(a4)	; save mem for gads' & images'.
	moveq	#0,d1			; clr the reg
	move.w	nColors(a4),d1		; get colors in d1	
	mulu.w	#gg_SIZEOF,d1		; size of gadget struct
	add.l	d1,d0			; colors*image size + gad ptr
	move.l	d0,pImgs(a4)		; stash the images ptr	
	moveq	#0,d1
	moveq	#0,d2			; clr the regs
	moveq	#0,d6
	moveq	#0,d0
	move.w	nPlanes(a4),d0		; get depth
	subq.w	#1,d0			; (depth-1) index.
	lea	gadwidth(a4),a0		; a0 -> gadwidth[0]
	move.b	0(a0,d0.w),d1		; index depth-1 bytes into array
	lea	gadrows(a4),a0		; a0 -> gadrows[0]
	move.b	0(a0,d0.w),d2		; index depth-1 bytes into array
	lea	colBorXY(a4),a0		; a0 -> colBorXY[0]
	move.w	d1,d6			; keep d1
	addq.w	#1,d6			; width + 1
	moveq	#4,d7			; index 2 words
	move.w	d6,0(a0,d7.w)		; stuff borxy[2] with width+1
	addq.w	#4,d7			; index 2 more words
	move.w	d6,0(a0,d7.w)		; stuff borxy[4] with width+1

	addq	#COLSW,d1		; colorgad Dx
	lea	colGadX(a4),a0		; a0 -> colGadX
	lea	colGadY(a4),a1		; a1 -> colGadY
	moveq	#0,d6			; clr d6, row cntr	
	moveq	#0,d7			; clr d7, col cntr	
	moveq	#0,d0			; clr d0, x coordinate
	moveq	#0,d5			; clr d5, y coordinate
	moveq	#0,d3			; set word ptr

do_cols_rows:
	moveq	#0,d0			; d1=colg dx, d2=#rows
	move.b	d7,d0			; move col cntr into d0
	mulu	d1,d0			; multiply colgadx to get x coord 
	add.w	#COLX,d0		; add offset to get x coord
	moveq	#0,d5			; zero the calc y coord
	move.w	d6,d5			; move row cntr to d5
	mulu	#COLDY,d5		; multiply to get y coord
	add.w	#COLY,d5		; add offset to get y coord
	move.w	d0,0(a0,d3.w)		; stuff gadx[index] with x coord
	move.w	d5,0(a1,d3.w)		; stuff gady[index] with y coord
	addq.w	#2,d3			; increment index
	addq.w	#1,d7			; increment col cntr
	cmp.w	nColors(a4),d7		; is col > #colors ?
	beq.s	xy_fini			; the CORRECT french spelling!
	cmp.b	#8,d7			; full column is 8 gadgets
	bne.s	do_cols_rows		; finish the row
	moveq	#0,d7			; clr the column counter
	addq.w	#1,d6			; increment row cntr
	cmp.b	d2,d6			; is row>number of rows ?
	bne.s	do_cols_rows		; start new row

xy_fini:
	move.l	pGads(a4),a2		; a2 -> gadget alloc
	move.l	pImgs(a4),a3		; a3 -> image alloc
	moveq	#colID,d6		; initialize planeonoff variable
	moveq	#0,d4			; clear pen plane cntr
	move.w	nColors(a4),d3		; put in # colors
	subq.w	#1,d3			; colors-1 for dbra
	subq	#COLSW,d1		; change colg dx to colgw

* initialize gadget & image structures

do_gads_imgs:
	move.w	(a0)+,gg_LeftEdge(a2)	; gadget->LeftEdge = colGadX[n]
	move.w	(a1)+,gg_TopEdge(a2)	; gadget->TopEdge = colGadY[n]
	move.w	d1,gg_Width(a2)		; gadget->Width = colw
	move.w	#COLH,gg_Height(a2)		   ; gadget->Height= COLH
	move.w	#(GADGHBOX!GADGIMAGE),gg_Flags(a2) ; gadget->Flags = 
	move.w	#GADGIMMEDIATE,gg_Activation(a2)   ; gadget->Activation =
	move.w	#BOOLGADGET,gg_GadgetType(a2)	   ; gadget->Type =
	move.l	a3,gg_GadgetRender(a2)	; gadget->GadgetRender = &image[n]
	move.w	d6,gg_GadgetID(a2)	; gadget->GadgetID = colid+cntr
	move.l	a2,a5			; save ptr
	add.l	#gg_SIZEOF,a2		; bump addr reg	
	move.l	a2,gg_NextGadget(a5)	; gadget->NextGadget = gad++

	move.w	d1,ig_Width(a3)		; image->Width = coldx
	move.w	#COLH,ig_Height(a3)	; image->Height = COLH
	move.w	#1,ig_Depth(a3)		; image->Depth = 1
	move.b	d4,ig_PlaneOnOff(a3)	; image->PlaneOnOff = pen plane cntr
	add.l	#ig_SIZEOF,a3		; bump address reg
	addq.b	#1,d6			; inc gad id cntr.
	addq.b	#1,d4			; inc pen plane cntr.
	dbra	d3,do_gads_imgs		; top of loop
					; fall thru and
	lea	okGad(a4),a0		; put ok gad address in a0
	move.l	a0,gg_NextGadget(a5)	; gadget->NextGadget = &okGad

* set window to screen characteristics

	lea	newpWin(a4),a0		; put addr of newin struct in a0
	move.l	pGads(a4),nw_FirstGadget(a0)	; stuff nw_FirstGadget ptr
	move.l	pScr(a4),a1			; get the screen ptr
	move.l	a1,nw_Screen(a0)	; stuff screen ptr in nw_Screen ptr
	moveq	#0,d0			; clr d0
	move.w	sc_Flags(a1),d0		; put sc_Flags var in d0
	and.w	#SCREENTYPE,d0		; and the flags variable
	move.w	d0,nw_Type(a0)		; put it nw_Type var 
	mulu	#COLDY,d2		; # rows * COLDY
	add.w	#10,d2			; + row offset
	add.w	#PBHIGH,d2		; + window height offset
	move.w	d2,nw_Height(a0)	; put in nw_Height var
	SYS	OpenWindow,IBase(a4)	; open window
	move.l	d0,pWin(a4)		; save the window ptr
	beq	nowin			; get the f**k out

	move.l	d0,a0			; put winptr in a0
	move.l	wd_RPort(a0),pRp(a4)	; put Rport in pRp
	move.l	wd_UserPort(a0),pUp(a4)	; put UserPort in pUp

	moveq	#0,d4
	move.w	nColors(a4),d4		; move #colors in d4
	subq	#1,d4			; subtract 1 for dbra
	moveq	#0,d3			; clr d3
	lea	curColor(a4),a2		; get ptr to curcolors in a2
	lea	resColor(a4),a3		; get ptr to rescolors in a3
	move.l	pVp(a4),a5		; get screen's viewport ptr in a5
	move.l	vp_ColorMap(a5),a5	; put colormap ptr in a5

colorloop:
	move.l	a5,a0			; put colormap ptr in a0
	move.l	d3,d0			; put entry index in d0
	SYS	GetRGB4,GBase(a4)	; get colors from system
	move.w	d0,(a2)+		; move colorvalue into curcolor
	move.w	d0,(a3)+		; move colorvalue into rescolor
	addq	#1,d3			; increment entry index
	dbra	d4,colorloop		; do it color times

	moveq	#0,d6			; old color reg in d6 = color0
	moveq	#0,d7			; new color reg in d7 = color0
	bsr	select_color		; select_color uses d6 & d7

Forever:				; start of infinite loop
	move.l	pUp(a4),a2		; UserPort in a0	
	move.l	a2,a0
	SYS	WaitPort,EBase(a4)	; wait on the port
	
	move.l	a2,a0
	SYS	GetMsg			; get the message
	tst.l	d0			; null message?
	beq.s	Forever

	moveq	#0,d5			; clr the reg
	move.l	d0,a1			; copy message addr to a1
	move.l	im_Class(a1),d7		; d7 has the im_Class 
	move.l	im_IAddress(a1),a0	; a0 has the im_IAddress
	move.w	gg_GadgetID(a0),d5	; d5 has the gadget id
	SYS	ReplyMsg		; reply with ptr in a1

	cmp.l	#MOUSEMOVE,d7		; is it a mousemove ?
	bne.s	notmousemove		; nope
	moveq	#0,d6			; clr the reg
	move.w	pReg(a4),d6		; move pReg to d6 for call
	bsr	mod_color		; modify the color(s)
	bra.s	Forever			; done & go wait for messages

notmousemove:
	cmp.l	#GADGETDOWN,d7		; is it a gadgetdown event ?
	bne.s	gadgetup		; nope, hasta be gadget up
	cmp.w	#okID,d5		; is the gad id < okid ?
	blt.s	colorgad		; yes, gad id is a color gad

slid_gadgets:
	moveq	#0,d6			; clr the reg
	move.w	pReg(a4),d6		; move pReg to d6 for call
	bsr	mod_color		; modify the color(s)
	bra.s	Forever			; done & go wait for messages

colorgad:
	cmp.w	#colID,d5		; it the gad id >= colID
	blt.s	Forever			; no, get outa here
	moveq	#0,d6			; clr the reg
	move.w	pReg(a4),d6		; move pReg to d6 for call
	move.w	d5,d7			; move gadget id to d7
	subq.w	#colID,d7		; subtract to get color selected
	move.w	d7,pReg(a4)		; store the new color
	bsr	select_color		; draw the user's selection
	bra	Forever			; done & go wait for messages

gadgetup:
	cmp.w	#okID,d5		; is the gad id < okID ?
	blt.s	slid_gadgets		; yes, user slid gadget
	cmp.w	#okID,d5		; user hit ok ?
	bne.s	reset			; nope
	beq.s	clean_exit		; user is happy / colors
	bra	Forever			; gone & wait for messages

reset:
	cmp.w	#canID,d5		; user hit cancel ?
	beq.s	cancel			; hit cancel
	cmp.w	#resID,d5		; user hit reset ?
	bne.s	slid_gadgets		; no, user slid the gadgets
	bsr	reset_colors		; reset the colors
	move.w	pReg(a4),d6		; get old color
	moveq	#0,d7			; new color is color 0
	move.w	d7,pReg(a4)		; save as selected color
	bsr.s	select_color		; re-select color 0
	bra	Forever			; gone & wait for messages

cancel:
	bsr	reset_colors		; reset the colors

* clean up and back outs

clean_exit:
	move.l	pUp(a4),a0		; get the userport in a0
	SYS	GetMsg,EBase(a4)
	tst.l	d0			; any messages ?
	beq.s	no_messages		; no, exit

	move.l	d0,a1			; move mes ptr to a1
	SYS	ReplyMsg		; reply the message
	bra.s	clean_exit		; go & eat them all

no_messages:
	move.l	pWin(a4),a0		; get window ptr in a0
	SYS	CloseWindow,IBase(a4)

nowin:
	move.l	pGads(a4),a1		; get mem ptr
	move.l	gadmemsize,d0		; get size of memory alloc'ed
	SYS	FreeMem,EBase(a4)

nogadmem:
	move.l	IBase(a4),a1		; move ibase to a1
	SYS	CloseLibrary		; close intuition

cant_handle:
nointui:
	move.l	GBase(a4),a1		; move gbase to a1
	SYS	CloseLibrary		; close graphics
	
	move.l	wb_message(a4),d2	; get the stashed wb message
	beq.s	nogfx			; no message, exit
	SYS	Forbid			; dont UnLoadSeg me yet
	
	move.l	d2,a1			; put wb message in a1
	SYS	ReplyMsg		; reply to WorkBench

nogfx:
	movem.l	(sp)+,d2-d7/a2-a6	; restore the stack
	rts				; END OF PROGRAM

* ---------------------------------------
* ----- S U B R O U T I N E S -----------
* ---------------------------------------

* ------------------------------	
* update color selection -------
* ------------------------------	

select_color:				; d6=old, d7=new
					; erase the old border / pen 0
	move.w	d7,d3			; stash new col in d3		
	add.w	d6,d6			; convert old col to byte index
	add.w	d7,d7			; convert new col to byte index
	lea	colGadX(a4),a5		; save gadx in a5
	lea	colGadY(a4),a3		; save gady in a3
	move.l	pRp(a4),a2		; save the rastport in a2	

	lea	colBor(a4),a1		; ptr to colbor in a1
	move.b	#0,bd_FrontPen(a1)	; set front pen to zero
	moveq	#0,d0
	moveq	#0,d1
	move.w	0(a5,d6.w),d0		; colGadX[old]
	move.w	0(a3,d6.w),d1		; colGadY[old]
	move.l	a2,a0			; rastport in a0
	SYS	DrawBorder,IBase(a4)	; draw the border
					; draw the new border / pen 1
	lea	colBor(a4),a1		; ptr to colBor in a1
	move.b	#1,bd_FrontPen(a1)	; set front pen to 1
	moveq	#0,d0
	moveq	#0,d1
	move.w	0(a5,d7.w),d0		; colGadX[new]
	move.w	0(a3,d7.w),d1		; colGadY[new]
	move.l	a2,a0			; rastport in a0
	SYS	DrawBorder		; draw the border

	move.l	a2,a1			; rastport in a1
	move.l	#RP_JAM1,d0		; draw mode in d0
	SYS	SetDrMd,GBase(a4)		; set the draw mode

	move.l	a2,a1			; rastport in a1
	moveq	#1,d0			; set pen to 1 in d0
	SYS	SetAPen			; set the area pen to 1
					; color swatch from window height
	move.l	pWin(a4),a0		; get ptr to window
	moveq	#0,d4
	moveq	#0,d5
	move.w	wd_Height(a0),d5	; put window height in d5
	move.w	d5,d4			; put window height in d4
	sub.w	#63,d5			; gives d5=CHY1
	sub.w	#23,d4			; gives d4=CHY2
					; border around color swatch
	move.l	a2,a1			; rastport in a1
	move.l	#CHX1,d0		; chx1 in d0		
	move.l	d5,d1			; chy1 in d1		
	SYS	Move			; move tuda spot

	move.l	a2,a1			; rastport in a1
	move.l	#CHX2,d0		; chx2 in d0		
	move.l	d5,d1			; chy1 in d1		
	SYS	Draw			; draw tudere

	move.l	a2,a1			; rastport in a1
	move.l	#CHX2,d0		; chx2 in d0		
	move.l	d4,d1			; chy2 in d1		
	SYS	Draw			; draw tudere

	move.l	a2,a1			; rastport in a1
	move.l	#CHX1,d0		; chx1 in d0		
	move.l	d4,d1			; chy2 in d1		
	SYS	Draw			; draw tudere

	move.l	a2,a1			; rastport in a1
	move.l	#CHX1,d0		; chx1 in d0		
	move.l	d5,d1			; chy1 in d1		
	SYS	Draw			; draw tudere

	move.l	a2,a1			; rastport in a1
	moveq	#0,d0
	move.w	d3,d0			; set pen color to new col in d3
	SYS	SetAPen			; set the area pen to 1

	move.l	a2,a1			; rastport in a1
	move.l	#CHX1,d0		; chx1 in d0
	addq.w	#2,d0			; add offset
	move.l	d5,d1			; chy1 in d1
	addq.w	#2,d1			; add offset
	move.l	#CHX2,d2		; chx2 in d2	
	subq.w	#2,d2			; subtract to get offset	
	move.l	d4,d3			; put chy2 in d3	
	subq.w	#2,d3			; subtract to get offset	
	SYS	RectFill		; rect fill that baby

	lea	curColor(a4),a1		; get curcolor ptr
	move.w	0(a1,d7.w),d6		; put curcolor[new] in d6=rgb
	bsr	mod_color_prop		; mod_color_props uses d6=rgb
	bsr.s	print_RGB		; print_RGB uses d6=rgb
	rts

* refresh color selected -------

mod_color:				; color reg in d6
					; modify sel'd color from slider
	moveq	#12,d3			; 12 bit shifter
	lea	rInf(a4),a0		; get ptr to red info struct
	move.w	pi_HorizPot(a0),d0	; get horiz pot value in d0
	asr.w	d3,d0			; asr 12 bits
	and.l	#$0F,d0			; mask the result
	
	lea	gInf(a4),a0		; get ptr to green info struct
	move.w	pi_HorizPot(a0),d1	; get horiz pot value in d1
	asr.w	d3,d1			; asr 12 bits
	and.l	#$0F,d1			; mask the result
	
	lea	bInf(a4),a0		; get ptr to blue info struct
	move.w	pi_HorizPot(a0),d2	; get horiz pot value in d2
	asr.w	d3,d2			; asr 12 bits
	and.l	#$0F,d2			; mask the result
					; d0=red, d1=green, d2=blue
	asl.l	#8,d0			; shift the red 8 bits
	asl.l	#4,d1			; shift the green 4 bits
	or.l	d1,d0			; OR them with blue
	or.l	d2,d0			; new color value
	move.w	d6,d1			; stash the rgb value
	add.w	d1,d1			; get color*2 bytes in d1
	lea	curColor(a4),a1		; get ptr to curcolor
	move.w	d0,0(a1,d1.w)		; stuff curcolor[n] with colorvalue	
	move.w	d0,d6			; put current color in d6
	moveq	#0,d0			; clr d0 for the call
	move.w	nColors(a4),d0		; move # colors to d0
	move.l	pVp(a4),a0		; viewport in a0
	SYS	LoadRGB4,GBase(a4)	; load the colors
					; fall thru into print_RGB

* print RGB hex value ----------

print_RGB:
					; rgb value is in d6
	move.w	d6,d1			; move value red reg
	asr.w	#8,d1			; red value
	and.l	#$F,d1			; mask the red

	move.w	d6,d2			; move value green reg
	asr.w	#4,d2			; green value
	and.l	#$F,d2			; mask the green

	move.w	d6,d0			; move value blue reg
	and.l	#$F,d0			; mask the blue
	
	lea	rgbstring(a4),a5	; get address of format string
	lea	rgbdisplay(a4),a2	; get address of display string
	move.b	0(a5,d1.w),(a2)+	; format the red into value	
	move.b	0(a5,d2.w),(a2)+	; format the green into value
	move.b	0(a5,d0.w),(a2)		; format the blue into value

	move.l	pRp(a4),a3		; save rastport ptr in a3

	move.l	a3,a1			; rastport in a1 for the call
	moveq	#1,d0			; got as a long
	SYS	SetAPen,GBase(a4)	; set the area pen

	move.l	a3,a1			; rastport in a1 for the call
	moveq	#0,d0			; clr the reg
	SYS	SetBPen			; set the background pen

	move.l	a3,a1			; rastport in a1 for the call
	move.l	#RP_JAM2,d0		; got as a long
	SYS	SetDrMd			; set the draw mode to overwrite

	move.l	a3,a1			; rastport in a1 for the call
	move.l	#PWIDE,d0		; put in the x offset
	sub.b	#40,d0			; get the x value
	move.l	pWin(a4),a0		; get window ptr
	moveq	#0,d1
	move.w	wd_Height(a0),d1	; get window height
	sub.b	#40,d1			; get the y value
	SYS	Move			; move tudere
			
	lea     rgbdisplay(a4),a0	; string ptr in a0
	move.l	a3,a1			; rastport in a1 for the call
	moveq	#3,d0			; count in d0
	SYS	Text			; spit it out
	rts
	
* ------------------------------
* modify proportional gads -----
* ------------------------------

* ModifyProp (IBase)			; rgb value = d6
* gadget->  	a0
* window->  	a1
* requester-> 	a2
* pi_Flags	d0		
* pi_HorizPot	d1
* pi_VertPot	d2
* pi_HorizBody	d3
* pi_VertBody	d4

mod_color_prop:
					; modify the red gadget
	sub.l	a2,a2			; clr the requester arg
	move.l	#$11111111,d7		; store the multiplier in d7

	move.w	d6,d1			; move value red reg
	asr.w	#8,d1			; red value
	and.l	#$F,d1			; mask the red
	muls.w	d7,d1			; mult to get horiz pot val
	lea	rGad(a4),a0		; get the gadget address in a0
	move.l	pWin(a4),a1		; get the window ptr in a1
	lea	rInf(a4),a3		; get rInf ptr
	moveq	#0,d0
	move.l	d0,d2
	move.l	d0,d3
	move.l	d0,d4
	move.w	pi_Flags(a3),d0		; flags in d0
	move.w	pi_VertPot(a3),d2	; horiz pot in d2
	move.w	pi_HorizBody(a3),d3	; horiz body in d3
	move.w	pi_VertBody(a3),d4	; horiz body in d3
	SYS	ModifyProp,IBase(a4)
					; modify the green gad
	move.w	d6,d1			; move rgb value green reg
	asr.w	#4,d1			; green value
	and.l	#$F,d1			; mask the green
	muls.w	d7,d1			; mult to get horiz pot val
	lea	gGad(a4),a0		; get the gadget address in a0
	move.l	pWin(a4),a1		; get the window ptr in a1
	lea	gInf(a4),a3		; get rInf ptr
	moveq	#0,d0
	move.l	d0,d2
	move.l	d0,d3
	move.l	d0,d4
	move.w	pi_Flags(a3),d0		; flags in d0
	move.w	pi_VertPot(a3),d2	; horiz pot in d2
	move.w	pi_HorizBody(a3),d3	; horiz body in d3
	move.w	pi_VertBody(a3),d4	; horiz body in d3
	SYS	ModifyProp
					; modify the blue gad
	move.w	d6,d1			; move rgb value blue reg
	and.l	#$F,d1			; mask the blue
	muls.w	d7,d1			; mult to get horiz pot val
	lea	bGad(a4),a0		; get the gadget address in a0
	move.l	pWin(a4),a1		; get the window ptr in a1
	lea	bInf(a4),a3		; get rInf ptr
	moveq	#0,d0
	move.l	d0,d2
	move.l	d0,d3
	move.l	d0,d4
	move.w	pi_Flags(a3),d0		; flags in d0
	move.w	pi_VertPot(a3),d2	; horiz pot in d2
	move.w	pi_HorizBody(a3),d3	; horiz body in d3
	move.w	pi_VertBody(a3),d4	; horiz body in d3
	SYS	ModifyProp		; modify the gadget
	rts

* ------------------------------		
* reset_colors------------------
* ------------------------------

reset_colors:				; no args
					; reset the default colors
	lea	curColor(a4),a0		; addr of curcolors in a0
	lea	resColor(a4),a1		; addr of resetcolors in a1
	move.w	nColors(a4),d0		; get the number of colors
	subq.w	#1,d0			; ready for dbra

resetloop:

	move.w	(a1)+,(a0)+		; word copy the array
	dbra	d0,resetloop		; loop

	lea	resColor(a4),a1		; get ptr to curcolor
	moveq	#0,d0
	move.w	nColors(a4),d0		; move # colors to d0
	move.l	pVp(a4),a0		; viewport in a0
	SYS	LoadRGB4,GBase(a4)	; load the colors
	rts

* ---------------------------------------
* ----- INITIALIZED DATA SECTION --------
* ---------------------------------------

	SECTION	__MERGED,DATA

* internally referenced data

r_text:
	dc.b	'R',0
	ds.w	0

g_text:
	dc.b	'G',0
	ds.w	0

b_text:
	dc.b	'B',0
	ds.w	0

can_text:
	dc.b	'CANCEL',0
	ds.w	0

reset_text:
	dc.b	'RESET',0
	ds.w	0

ok_text:
	dc.b	'OK',0
	ds.w	0

newpW_title:
	dc.b	'Palette Tool V1.1',0
	ds.w	0

canGad:					; Gadget structure
	dc.l	rGad			; gg_NextGadget
	dc.w	TXTX+122		; gg_LeftEdge
	dc.w	TXTY			; gg_TopEdge
	dc.w	TXTW			; gg_Width
	dc.w	TXTH			; gg_Height
	dc.w	GADGHCOMP!GRELBOTTOM	; gg_Flags
	dc.w	RELVERIFY		; gg_Activation
	dc.w	BOOLGADGET		; gg_GadgetType
	dc.l	txtBor			; gg_GadgetRender
	dc.l	0			; gg_SelectRender
	dc.l	canTxt			; gg_GadgetText
	dc.l	0			; gg_MutualExclude
	dc.l	0			; gg_SpecialInfo
	dc.w	canID			; gg_GadgetID
	dc.l	0			; gg_UserData

resGad:					; Gadget structure
	dc.l	canGad			; gg_NextGadget
	dc.w	TXTX+61			; gg_LeftEdge
	dc.w	TXTY			; gg_TopEdge
	dc.w	TXTW			; gg_Width
	dc.w	TXTH			; gg_Height
	dc.w	GADGHCOMP!GRELBOTTOM	; gg_Flags
	dc.w	RELVERIFY		; gg_Activation
	dc.w	BOOLGADGET		; gg_GadgetType
	dc.l	txtBor			; gg_GadgetRender
	dc.l	0			; gg_SelectRender
	dc.l	resTxt			; gg_GadgetText
	dc.l	0			; gg_MutualExclude
	dc.l	0			; gg_SpecialInfo
	dc.w	resID			; gg_GadgetID
	dc.l	0			; gg_UserData
		
txtBorXY:
	dc.w	0			; points for border structure
	dc.w	0
	dc.w	TXTW-1
	dc.w	0
	dc.w	TXTW-1
	dc.w	TXTH-1
	dc.w	0
	dc.w	TXTH-1
	dc.w	0
	dc.w	0

txtBor:					; Border structure
	dc.w	0			; bd_LeftEdge
	dc.w	0			; bd_TopEdge
	dc.b	3			; bd_FrontPen
	dc.b	0			; bd_BackPen
	dc.b	RP_JAM1			; bd_DrawMode
	dc.b	5			; bd_Count
	dc.l	txtBorXY		; bd_XY
	dc.l	0			; bd_NextBorder

rTxt:					; IntuiText structure
	dc.b	1			; it_FrontPen
	dc.b	0			; it_BackPen
	dc.b	RP_JAM1			; it_DrawMode
	dc.b	0			; it_KludgeFill00
	dc.w	-11			; it_LeftEdge
	dc.w	2			; it_TopEdge
	dc.l	0			; it_ITextFont
	dc.l	r_text			; it_IText
	dc.l	0			; it_NextText

gTxt:					; IntuiText structure
	dc.b	1			; it_FrontPen
	dc.b	0			; it_BackPen
	dc.b	RP_JAM1			; it_DrawMode
	dc.b	0			; it_KludgeFill00
	dc.w	-11			; it_LeftEdge
	dc.w	2			; it_TopEdge
	dc.l	0			; it_ITextFont
	dc.l	g_text			; it_IText
	dc.l	0			; it_NextText

bTxt:					; IntuiText structure
	dc.b	1			; it_FrontPen
	dc.b	0			; it_BackPen
	dc.b	RP_JAM1			; it_DrawMode
	dc.b	0			; it_KludgeFill00
	dc.w	-11			; it_LeftEdge
	dc.w	2			; it_TopEdge
	dc.l	0			; it_ITextFont
	dc.l	b_text			; it_IText
	dc.l	0			; it_NextText

canTxt:					; IntuiText structure
	dc.b	1			; it_FrontPen
	dc.b	0			; it_BackPen
	dc.b	RP_JAM1			; it_DrawMode
	dc.b	0			; it_KludgeFill00
	dc.w	4			; it_LeftEdge
	dc.w	3			; it_TopEdge
	dc.l	0			; it_ITextFont
	dc.l	can_text		; it_IText
	dc.l	0			; it_NextText

resTxt:					; IntuiText structure
	dc.b	1			; it_FrontPen
	dc.b	0			; it_BackPen
	dc.b	RP_JAM1			; it_DrawMode
	dc.b	0			; it_KludgeFill00
	dc.w	8			; it_LeftEdge
	dc.w	3			; it_TopEdge
	dc.l	0			; it_ITextFont
	dc.l	reset_text		; it_IText
	dc.l	0			; it_NextText

okTxt:					; IntuiText structure
	dc.b	1			; it_FrontPen
	dc.b	0			; it_BackPen
	dc.b	RP_JAM1			; it_DrawMode
	dc.b	0			; it_KludgeFill00
	dc.w	19			; it_LeftEdge
	dc.w	3			; it_TopEdge
	dc.l	0			; it_ITextFont
	dc.l	ok_text			; it_IText
	dc.l	0			; it_NextText

* externally referenced data

	XDEF	rInf
	XDEF	gInf
	XDEF	bInf

	XDEF	rGad
	XDEF	gGad
	XDEF	bGad
	XDEF	okGad

	XDEF	colBorXY
	XDEF	colBor

	XDEF	newpWin
	XDEF	pWin
	XDEF	pScr

	XDEF	EBase
	XDEF	GBase
	XDEF	IBase
	XDEF	wb_message

	XDEF	pImgs
	XDEF	pGads

	XDEF	pUp
	XDEF	pRp
	XDEF	pVp

	XDEF	gadmemsize
	XDEF	nColors
	XDEF	nPlanes

	XDEF	gadwidth
	XDEF	gadrows

	XDEF	gfxname
	XDEF	intuiname
	XDEF	rgbstring
	XDEF	rgbdisplay
	XDEF	pReg

rInf:					; PropInfo structure
	dc.w	AUTOKNOB!FREEHORIZ	; pi_Flags
	dc.w	0			; pi_HorizPot
	dc.w	0			; pi_VertPot
	dc.w	RGB_BDY			; pi_HorizBody
	dc.w	0			; pi_VertBody
	dc.w	0			; pi_CWidth
	dc.w	0			; pi_CHeight
	dc.w	0			; pi_HPotRes
	dc.w	0			; pi_VpotRes
	dc.w	0			; pi_LeftBorder
	dc.w	0			; pi_TopBorder

gInf:					; PropInfo structure
	dc.w	AUTOKNOB!FREEHORIZ	; pi_Flags
	dc.w	0			; pi_HorizPot
	dc.w	0			; pi_VertPot
	dc.w	RGB_BDY			; pi_HorizBody
	dc.w	0			; pi_VertBody
	dc.w	0			; pi_CWidth
	dc.w	0			; pi_CHeight
	dc.w	0			; pi_HPotRes
	dc.w	0			; pi_VpotRes
	dc.w	0			; pi_LeftBorder
	dc.w	0			; pi_TopBorder

bInf:					; PropInfo structure
	dc.w	AUTOKNOB!FREEHORIZ	; pi_Flags
	dc.w	0			; pi_HorizPot
	dc.w	0			; pi_VertPot
	dc.w	RGB_BDY			; pi_HorizBody
	dc.w	0			; pi_VertBody
	dc.w	0			; pi_CWidth
	dc.w	0			; pi_CHeight
	dc.w	0			; pi_HPotRes
	dc.w	0			; pi_VpotRes
	dc.w	0			; pi_LeftBorder
	dc.w	0			; pi_TopBorder

rGad:					; Gadget structure
	dc.l	gGad			; gg_NextGadget
	dc.w	PRPX			; gg_LeftEdge
	dc.w	PRPY			; gg_TopEdge
	dc.w	PRPW			; gg_Width
	dc.w	PRPH			; gg_Height
	dc.w	GADGHNONE!GADGIMAGE!GRELBOTTOM		; gg_Flags
	dc.w	GADGIMMEDIATE!RELVERIFY!FOLLOWMOUSE	; gg_Activation
	dc.w	PROPGADGET		; gg_GadgetType
	dc.l	rImg			; gg_GadgetRender
	dc.l	0			; gg_SelectRender
	dc.l	rTxt			; gg_GadgetText
	dc.l	0			; gg_MutualExclude
	dc.l	rInf			; gg_SpecialInfo
	dc.w	rID			; gg_GadgetID
	dc.l	0			; gg_UserData

gGad:					; Gadget structure
	dc.l	bGad			; gg_NextGadget
	dc.w	PRPX			; gg_LeftEdge
	dc.w	PRPY+15			; gg_TopEdge
	dc.w	PRPW			; gg_Width
	dc.w	PRPH			; gg_Height
	dc.w	GADGHNONE!GADGIMAGE!GRELBOTTOM		; gg_Flags
	dc.w	GADGIMMEDIATE!RELVERIFY!FOLLOWMOUSE	; gg_Activation
	dc.w	PROPGADGET		; gg_GadgetType
	dc.l	gImg			; gg_GadgetRender
	dc.l	0			; gg_SelectRender
	dc.l	gTxt			; gg_GadgetText
	dc.l	0			; gg_MutualExclude
	dc.l	gInf			; gg_SpecialInfo
	dc.w	gID			; gg_GadgetID
	dc.l	0			; gg_UserData
		
bGad:					; Gadget structure
	dc.l	0			; gg_NextGadget
	dc.w	PRPX			; gg_LeftEdge
	dc.w	PRPY+30			; gg_TopEdge
	dc.w	PRPW			; gg_Width
	dc.w	PRPH			; gg_Height
	dc.w	GADGHNONE!GADGIMAGE!GRELBOTTOM		; gg_Flags
	dc.w	GADGIMMEDIATE!RELVERIFY!FOLLOWMOUSE	; gg_Activation
	dc.w	PROPGADGET		; gg_GadgetType
	dc.l	bImg			; gg_GadgetRender
	dc.l	0			; gg_SelectRender
	dc.l	bTxt			; gg_GadgetText
	dc.l	0			; gg_MutualExclude
	dc.l	bInf			; gg_SpecialInfo
	dc.w	bID			; gg_GadgetID
	dc.l	0			; gg_UserData

okGad:					; Gadget structure
	dc.l	resGad			; gg_NextGadget
	dc.w	TXTX			; gg_LeftEdge
	dc.w	TXTY			; gg_TopEdge
	dc.w	TXTW			; gg_Width
	dc.w	TXTH			; gg_Height
	dc.w	GADGHCOMP!GRELBOTTOM	; gg_Flags
	dc.w	RELVERIFY		; gg_Activation
	dc.w	BOOLGADGET		; gg_GadgetType
	dc.l	txtBor			; gg_GadgetRender
	dc.l	0			; gg_SelectRender
	dc.l	okTxt			; gg_GadgetText
	dc.l	0			; gg_MutualExclude
	dc.l	0			; gg_SpecialInfo
	dc.w	okID			; gg_GadgetID
	dc.l	0			; gg_UserData

colBorXY:
	dc.w	-2			; points for border structure
	dc.w	-2
	dc.w	0
	dc.w	-2
	dc.w	0
	dc.w	COLH+1
	dc.w	-2
	dc.w	COLH+1
	dc.w	-2
	dc.w	-2

colBor:
	dc.w	0			; bd_LeftEdge
	dc.w	0			; bd_TopEdge
	dc.b	1			; bd_FrontPen
	dc.b	0			; bd_BackPen
	dc.b	RP_JAM1			; bd_DrawMode
	dc.b	5			; bd_Count
	dc.l	colBorXY		; bd_XY
	dc.l	0			; bd_NextBorder

newpWin:
					; NewWindow structure
	dc.w	36			; nw_LeftEdge
	dc.w	24			; nw_Top Edge
	dc.w	PWIDE			; nw_Width
	dc.w	0			; nw_Height	(SET before open)
	dc.b	2			; nw_DetailPen
	dc.b	1			; nw_BlockPen
	dc.l	MOUSEMOVE!GADGETUP!GADGETDOWN		; nw_IDCMPFlags
	dc.l	WINDOWDRAG!ACTIVATE!SMART_REFRESH	;nw_Flags
	dc.l	0			; nw_FirstGadget(SET before open)	
	dc.l	0			; nw_CheckMark
	dc.l	newpW_title		; nw_Title
	dc.l	0			; nw_Screen	(SET before open)
	dc.l	0			; nw_BitMap
	dc.w	0			; nw_MinWidth
	dc.w	0			; nw_MinHeight
	dc.w	0			; nw_MaxWidth
	dc.w	0			; nw_MaxHeight
	dc.w	0			; nw_Type	(SET before open)

EBase:
	dc.l	0			; execbase in fast RAM

wb_message:
	dc.l	0			; storage for WB message

GBase:
	dc.l	0			; graphics in fast RAM

IBase:
	dc.l	0			; intuition in fast RAM

pImgs:
	dc.l	0			; Ptr to Image struct

pGads:
	dc.l	0			; Ptr to Gadget struct

pWin:
	dc.l	0			; pointer to Window structure

pVp:
	dc.l	0			; pointer to ViewPort structure

pRp:
	dc.l	0			; pointer to RastPort structure

pUp:
	dc.l	0			; pointer to UserPort structure

pScr:
	dc.l	0			; pointer to Screen structure

gadmemsize:
	dc.l	0			; size of memory alloc'ed

nColors:
	dc.w	0			; number of colors

nPlanes:	
	dc.w	0			; number of bitplanes

gadwidth:
	dc.b	84			; width for 2 colors
	dc.b	40			; width for 4 colors
	dc.b	18			; width for 8 colors
	dc.b	18			; width for 16 colors
	dc.b	18			; width for 32 colors
	ds.w	0

gadrows:
	dc.b	1			; rows for 2 colors
	dc.b	1			; rows for 4 colors
	dc.b	1			; rows for 8 colors	
	dc.b	2			; rows for 16 colors
	dc.b	4			; rows for 32 colors
	ds.w	0

gfxname:
	dc.b	'graphics.library',0
	ds.w	0

intuiname:
	dc.b	'intuition.library',0
	ds.w	0

rgbstring:
	dc.b	'0123456789ABCDEF',0	; display char array
	ds.w	0

rgbdisplay:				; RGB display string
	dc.b	'000',0
	ds.w	0

pReg:
	dc.w	0			;current color selected.

* ---------------------------------------
* ----- UNITIALIZED BSS DATA SECTION ----
* ---------------------------------------

	SECTION	__MERGED,BSS

* externally referenced data

	XDEF	rImg
	XDEF	gImg
	XDEF	bImg
	XDEF	colGadX
	XDEF	colGadY
	XDEF	curColor
	XDEF	resColor

rImg:					; size of Image structure
	ds.b	ig_SIZEOF

gImg:					; size of Image structure
	ds.b	ig_SIZEOF

bImg:					; size of Image structure
	ds.b	ig_SIZEOF

colGadX:				; x-coord array for gads
	ds.w	MXCOLRS

colGadY:				; y-coord array for gads
	ds.w	MXCOLRS

curColor:				; current color array
	ds.w	MXCOLRS

resColor:				; reset color array
	ds.w	MXCOLRS

	END
