
*	RF-Tools: XColor V1.2  (7.August.1989)    
*
*	Author : Roger Fischlin
*	         Steigerwaldweg
*	         D-6450 Hanau 7
*	         (West Germay)
*	     
*	         Telephone : (06181) 650266
*
*	  I used the DevPac Assembler V2.0.
*
*	!!  This program is public domain  !! 
*
*
*



	incdir   	"vd0:include/"		; include some files

	include	intuition/intuition.i
	include	intuition/intuition_lib.i
        	include 	exec/memory.i
	include	exec/exec_lib.i
	include	graphics/graphics_lib.i
	include	graphics/text.i
	include	libraries/dos_lib.i
	include	libraries/dos.i
	include	math/mathFFP_lib.i

BOX	macro			; macro to create a border structure
	dc.w	0,0
	dc.b	\5,0,RP_JAM1,5
	dc.l	box_\@,\6
box_\@	dc.w	\1,\2,\3,\2,\3,\4,\1,\4,\1,\2
	endm

TEXT	macro			; macro to create a text structure	
	dc.b	\4,0,RP_JAM1,0
	dc.w	\1,\2
	dc.l	0,\3,0
	endm


	include	misc/easystart.i	; include startup code

OpenThem	move.w	#$ff,Mode
	lea	dosname(pc),a1		; open libs
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_DOSBase	
	lea	intname(pc),a1                 
	moveq.l	#0,d0		
	CALLEXEC 	OpenLibrary
	move.l	d0,_IntuitionBase  
	lea	grafname(pc),a1
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_GfxBase
	lea	Mathname(pc),a1
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_MathBase
	beq	.exit
	jmp	start

.exit	moveq.l	#0,d0
	rts
_DOSBase	dc.l	0
dosname	DOSNAME
_GfxBase	dc.l	0
grafname	GRAFNAME
_IntuitionBase	dc.l	0
intname	INTNAME
_MathBase	dc.l	0
Mathname	FFPNAME

start	clr.b	Mode		; set to normal mode
	move.b	#$ff,OldMode

	move.l	_IntuitionBase,a6	; get pointer to first screen
	lea.l	NewWindow1,a0
	move.l	ib_FirstScreen(a6),nw_Screen(a0)


	CALLINT	OpenWindow		; open the window
	tst.l	d0
	beq	NoWindow
	move.l	d0,Window1_Ptr


	jsr	GetDepth
	jsr	SaveForUndo		; save color for undo function

IN3	jsr	SaveC12		; get colors 0 &1

	lea.l	FONT,a0            	; use Topaz-80 
	CALLGRAF	OpenFont           	
	move.l	d0,Font
	move.l	d0,a0
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	SetFont




	
IN1	move.l	Window1_Ptr,a1		; draw border
	move.l	wd_RPort(a1),a0
	lea.l	CG_Box(pc),a1
	move.w	#10,d0
	move.w	#15,d1
	CALLINT	DrawBorder

	

IN0	lea.l	Gadget0,a0		; refresh gadgets
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets

	clr.l	COLOR		; current color = 0	
	jsr	GetDepth		; get depth
	jsr	MakeCG		; create color gadget
IN2	jsr	NewColor		; edit new color
	jsr	RGB		; write R,G,B


	move.l	Window1_Ptr,a1		; Clear Window
	move.l	wd_RPort(a1),a1
	move.l	a1,a3
	moveq.l	#0,d0
	CALLGRAF	SetAPen
	move.l	a3,a1
	move.l	#182,d0
	move.l	#13,d1
	move.l	#182+40,d2
	move.l	#128,d3
	CALLGRAF	RectFill
	move.l	a3,a1
	moveq.l	#5,d0
	move.l	#105,d1
	move.l	#227,d2
	move.l	#128,d3
	CALLGRAF	RectFill


wait	lea.l	Gadget0,a0		; refresh gadgets again
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets

	moveq.l	#0,d0
	move.b	Mode,d0
	cmp.b	OldMode,d0
	beq	wait2
	move.b	d0,OldMode
	mulu	#25,d0
	add.l	#Title0,d0
	move.l	d0,a1
	sub.l	a2,a2
	move.l	Window1_Ptr,a0
	CALLINT	SetWindowTitles

wait2	move.l	Window1_Ptr,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.l	im_IAddress(a1),a4
	move.w	im_MouseX(a1),d6
	move.w	im_MouseY(a1),d7
	CALLEXEC 	ReplyMsg		; reply message		
	cmp.l	#CLOSEWINDOW,d4
	beq	QUIT
	cmp.l	#MENUPICK,d4
	beq	Size
	
	cmp.l	#GADGETDOWN,d4
	beq	G1_HandlerA
	
	cmp.l	#VANILLAKEY,d4
	beq	Key
	moveq.l	#0,d0		
	move.w	gg_GadgetID(a4),d0	
	tst.w	d0
	beq	G0_Handler
	cmp.w	#3,d0
	bls	G1_HandlerB
	cmp.w	#7,d0	
	beq	UNDO
	cmp.w	#9,d0	
	beq	SUB
	cmp.w	#8,d0	
	beq	ADD
	cmp.w	#4,d0
	beq	ChangeScreen
	cmp.b	#10,d0
	beq	Black_White
	cmp.b	#5,d0
	beq	UNDO_ALL
	cmp.b	#11,d0
	beq	ANTIK
	cmp.b	#12,d0
	beq	COPY
	cmp.b	#13,d0
	beq	EXCHANGE
	cmp.b	#14,d0
	beq	SPREAD
	

	bra	wait	

Key	cmp.b	#" ",d5
	beq	XColors
	cmp.b	#27,d5
	beq	XColorsBack
	bra	wait	



QUIT	move.l	Window1_Ptr,a0		; the exit
	CALLINT	CloseWindow
	moveq.l	#0,d0

	move.l	_MathBase,a1
	CALLEXEC	CloseLibrary
	moveq.l	#0,d0
	rts





MakeCG	move.l	Depth,d0		; get size of block
	subq	#1,d0		; from table and draw
	lsl.l	#2,d0		; the blocks
	lea.l	SizeTable,a0
	move.w	(a0,d0),d6
	move.w	2(a0,d0),d7
	moveq.l	#0,d5
	move.w	#0,-(sp)


MCG0	moveq.l	#0,d4

MCG1	move.w	(sp),d0
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a3
	CALLGRAF	SetAPen
	move.l	a3,a1
	move.w	d4,d0
	move.w	d5,d1
	add.w	d6,d4
	move.w	d4,d2
	move.w	d5,d3
	add.w	d7,d3
	add	#10,d0
	add	#15,d1
	add	#10,d2
	add	#15,d3
	CALLGRAF	RectFill
	add.w	#1,(sp)
	cmp.w	#159,d4
	bls	MCG1
MCG2	add.w	d7,d5
	cmp.w	#39,d5
	bls	MCG0
	lea.l	2(sp),sp
	rts	
	
Depth	dc.l	0
SizeTable	dc.w	160/2,40/1	; 2   colors
	dc.w	160/2,40/2	; 4   colors
	dc.w	160/4,40/2	; 8   colors
	dc.w	160/4,40/4	; 16  colors
	dc.w	160/8,40/4	; 32  colors
	dc.w	160/16,40/4	; 64  colors	
	
	
Gadget0	dc.l	Gadget1
	dc.w	10,15,160,40
	dc.w	GADGHNONE
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	0,0
	dc.l	0
	dc.l	0,0
	dc.w	0
	dc.l	0


Gadget1	dc.l	Gadget2
	dc.w	40+10,50+15,114,11
	dc.w	GADGHCOMP
	dc.w	RELVERIFY!GADGIMMEDIATE
	dc.w	PROPGADGET
	dc.l	G1_image,0
	dc.l	0
	 dc.l	0,G1_info
	dc.w	1
	dc.l	0
G1_image	ds.w	4
G1_info	dc.w	FREEHORIZ!AUTOKNOB
	dc.w	0
	dc.w	0
	dc.w	$ffff/15
	ds.w	7
Gadget2	dc.l	Gadget3
	dc.w	40+10,50+30,114,11
	dc.w	GADGHCOMP
	dc.w	RELVERIFY!GADGIMMEDIATE
	dc.w	PROPGADGET
	dc.l	G2_image,0
	dc.l	0
	dc.l	0,G2_info
	dc.w	2
	dc.l	0
G2_image	ds.w	4
G2_info	dc.w	FREEHORIZ!AUTOKNOB
	dc.w	0
	dc.w	0
	dc.w	$ffff/15
	ds.w	7
Gadget3	dc.l	Gadget4
	dc.w	40+10,50+45,114,11
	dc.w	GADGHCOMP
	dc.w	RELVERIFY!GADGIMMEDIATE
	dc.w	PROPGADGET
	dc.l	G3_image,0
	dc.l	0
	dc.l	0,G3_info
	dc.w	3
	dc.l	0

G3_image	ds.w	4
G3_info	dc.w	FREEHORIZ!AUTOKNOB
	dc.w	0
	dc.w	0
	dc.w	$ffff/15
	ds.w	7


Gadget4	dc.l	Gadget5
	dc.w	5,115,10*8,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G4_box,0
	dc.l	G4_text
	dc.l	0,0
	dc.w	4
	dc.l	0
G4_box	BOX	-1,-1,80,10,1,0
G4_text	TEXT	16,1,G4_string,1
G4_string	dc.b	"SCREEN",0
Gadget5	dc.l	Gadget7
	dc.w	95,115,10*8,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G5_box,0
	dc.l	G5_text
	dc.l	0,0
	dc.w	5
	dc.l	0
G5_box	BOX	-1,-1,80,10,1,0
G5_text	TEXT	8,1,G5_string,1
G5_string	dc.b	"Undo All",0

Gadget7	dc.l	Gadget8
	dc.w	11,66,20,30
	dc.w	GADGHBOX
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	C_Box,0
	dc.l	0
	dc.l	0,0
	dc.w	7
	dc.l	0
Gadget8	dc.l	Gadget9
	dc.w	185,15,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G8_text
	dc.l	0,0
	dc.w	8
	dc.l	0
G8_box	BOX	-1,-1,36,10,1,0
G8_text	TEXT	6,1,G8_string,1
G8_string	dc.b	"ADD",0
Gadget9	dc.l	Gadget10
	dc.w	185,30,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G9_text
	dc.l	0,0
	dc.w	9
	dc.l	0
G9_text	TEXT	6,1,G9_string,1
G9_string	dc.b	"SUB",0

Gadget10	dc.l	Gadget11
	dc.w	185,50,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G10_text
	dc.l	0,0
	dc.w	10
	dc.l	0
G10_text	TEXT	6,1,G10_string,1
G10_string	dc.b	"B&W",0
Gadget11	dc.l	Gadget12
	dc.w	185,65,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G11_text
	dc.l	0,0
	dc.w	11
	dc.l	0
G11_text	TEXT	4,1,G11_string,1
G11_string	dc.b	"ANT.",0
Gadget12	dc.l	Gadget13
	dc.w	185,85,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G12_text
	dc.l	0,0
	dc.w	12
	dc.l	0
G12_text	TEXT	1,1,G12_string,1
G12_string	dc.b	"COPY",0
Gadget13	dc.l	Gadget14
	dc.w	185,100,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G13_text
	dc.l	0,0
	dc.w	13
	dc.l	0
G13_text	TEXT	8,1,G13_string,1
G13_string	dc.b	"EX.",0
Gadget14	dc.l	0	Gadget14
	dc.w	185,115,36,10
	dc.w	GADGHCOMP
	dc.w	RELVERIFY
	dc.w	BOOLGADGET
	dc.l	G8_box,0
	dc.l	G14_text
	dc.l	0,0
	dc.w	14
	dc.l	0
G14_text	TEXT	4,1,G14_string,1
G14_string	dc.b	"SPR.",0




Font	dc.l	0
FONT	dc.l	fontname
	dc.w	TOPAZ_EIGHTY
	dc.b	FS_NORMAL
	dc.b	FPF_ROMFONT
	even
fontname	dc.b	"topaz.font",0 



NewWindow1	dc.w	10,10
	dc.w	232,130
	dc.b	-1,-1
	dc.l	CLOSEWINDOW!GADGETUP!GADGETDOWN!MENUPICK!VANILLAKEY
	dc.l	WINDOWCLOSE!WINDOWDRAG!WINDOWDEPTH!ACTIVATE!SMART_REFRESH
	dc.l	Gadget0
	dc.l	0
	dc.l	0
CScreen	dc.l	0
	dc.l	0
	dc.w	140,100
	dc.w	140,100
	dc.w	CUSTOMSCREEN

Window1_Ptr	dc.l	0		; window pointer
Undo	dc.w	0		; undo
CG_Box	BOX	-1,-1,161,41,1,0
C_Box	BOX	-1,-1,20,30,1,0
 
COLOR	dc.l	0		; current color
MoverSize	dc.w	0		; size of mover ($ffff/15 or $ffff/7)

NewColor	move.w	#$ffff/15,d0		; edit a new color 
	cmp.l	#$1f,COLOR		; color is halfbrite ( >$1f) => mover is smaller !
	bls	NC0
	move.w	#$ffff/7,d0
NC0	move.w	d0,MoverSize
	

	move.l	Window1_Ptr,a1		; write color number
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#12,d0
	move.w	#50+15+6+1+15+15+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen2,a0
	move.l	COLOR,d0
	add.l	d0,d0
	add.l	d0,a0
	moveq.l	#2,d0
	CALLGRAF	Text


	move.l	Window1_Ptr,a0		; get RGB and save for undo !
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	COLOR,d0
	bsr	GET
	move.w	d0,Undo
	move.w	d0,-(sp)		; remake prop-gadgets
	lsr.w	#8,d0
	and.w	#$f,d0
	moveq.l	#0,d1
	move.b	d0,d1
	mulu.w	MoverSize,d1
	lea.l	Gadget1,a0
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	moveq.l	#0,d2
	move.w	MoverSize,d3
	moveq.l	#0,d4
	move.w	#FREEHORIZ!AUTOKNOB,d0
	CALLINT	ModifyProp
	
	moveq.l	#0,d1
	move.w	(sp),d1
	lsr.w	#4,d1
	and.w	#$f,d1
	mulu.w	MoverSize,d1
	lea.l	Gadget2,a0
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	moveq.l	#0,d2
	move.w	MoverSize,d3
	moveq.l	#0,d4
	move.w	#FREEHORIZ!AUTOKNOB,d0
	CALLINT	ModifyProp

	moveq.l	#0,d1
	move.w	(sp)+,d1
	and.w	#$f,d1
	mulu.w	MoverSize,d1
	lea.l	Gadget3,a0
	move.l	Window1_Ptr,a1
	sub.l	a2,a2
	moveq.l	#0,d2
	move.w	MoverSize,d3
	moveq.l	#0,d4
	move.w	#FREEHORIZ!AUTOKNOB,d0
	CALLINT	ModifyProp

	move.l	Window1_Ptr,a1		; draw block for undo gadget
	move.l	wd_RPort(a1),a1
	move.l	COLOR,d0
	CALLGRAF	SetAPen
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.w	#10+1,d0	
	move.w	#15+50+1,d1
	move.w	#10+19+1,d2	
	move.w	#15+50+29+1,d3
	CALLGRAF	RectFill
	jmp	HEX		; write mover positions as numbers

G0_Handler	cmp.b	#1,Mode		
	beq	Copy_Color
	cmp.b	#2,Mode
	beq	Exchange_Color
	cmp.b	#3,Mode
	beq	Spread_Color


	move.w	d6,d0		; color gadget was selected but 
	move.w	d7,d1		; which color ?
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel
	move.l	d0,COLOR
	jsr	NewColor		
	bra	wait

Zahlen	dc.b	"0123456789ABCDEF"
Zahlen2	dc.b	"000102030405060708090a0b0c0d0e0f"
	dc.b	"101112131415161718191a1b1c1d1e1f"
	dc.b	"202122232425262728292a2b2c2d2e2f"
	dc.b	"303132333435363738393a3b3c3d3e3f"
	
HEX	move.l	Window1_Ptr,a0		; write mover position as hex number
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	COLOR,d0
	bsr	GET
	move.w	d0,d5

	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#114+50+4,d0
	move.w	#50+15+6+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen,a0
	move.l	d5,d0
	lsr.l	#8,d0
	and.l	#$f,d0	
	add.l	d0,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#114+50+4,d0
	move.w	#50+15+6+15+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen,a0
	move.l	d5,d0
	lsr.l	#4,d0
	and.l	#$f,d0
	add.l	d0,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#114+50+4,d0
	move.w	#50+15+6+15+15+2,d1
	CALLGRAF	Move	

	lea.l	Zahlen,a0
	and.l	#$f,d5	
	add.l	d5,a0
	moveq.l	#1,d0
	CALLGRAF	Text
	rts
G1_HandlerA	jsr	ReadColor		; prop gadget was selected
	jsr	HEX
	move.l	Window1_Ptr,a0		
	move.l	wd_UserPort(a0),a0
	CALLEXEC	GetMsg
	tst.l	d0
	beq	G1_HandlerA
	move.l	d0,a1	
	CALLEXEC 	ReplyMsg
G1_HandlerB	jsr	ReadColor		;  " (but left button was released afterwards)
	jsr	HEX
	bra	wait

ReadColor	move.l	Window1_Ptr,a0		; get mover position and set RGB 
	CALLINT	ViewPortAddress
	move.l	d0,a0
	moveq.l	#0,d1
	moveq.l	#0,d2
	moveq.l	#0,d3
	
	move.w	G1_info+2,d1
	divu	G1_info+6,d1
	move.w	G2_info+2,d2
	divu	G2_info+6,d2
	move.w	G3_info+2,d3
	divu	G3_info+6,d3
	move.l	COLOR,d0
	bsr	SET
	rts
UNDO	move.l	Window1_Ptr,a0		; undo color
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	COLOR,d0
	move.w	Undo,d1
	move.w	d1,d2
	move.w	d2,d3
	lsr.w	#8,d1
	and.l	#$f,d1
	lsr.w	#4,d2
	and.l	#$f,d2
	and.l	#$f,d3		
	bsr	SET
	jsr	NewColor
	bra	wait
GetDepth	move.l	CScreen,a0		; get depth (HAM=4!)
	lea.l	sc_BitMap(a0),a0
	moveq.l	#0,d0
	move.b	bm_Depth(a0),d0
	move.l	CScreen,a0
	lea.l	sc_ViewPort(a0),a0
	move.w	vp_Modes(a0),d1
	and.w	#$800,d1
	beq	GD1
	moveq.l	#4,d0
GD1	move.l	d0,Depth
	rts
SUB	CALLEXEC	Forbid		; sub Bitplane
	move.l	CScreen,a0
	lea.l	sc_ViewPort(a0),a1		
	move.w	vp_Modes(a1),d0
	and.l	#$800,d0		; HAM ?
	bne	SUB1
	
	lea.l	sc_BitMap(a0),a0
	moveq.l	#0,d2
	move.b	bm_Depth(a0),d2			
	cmp.b	#1,d2		; depth =1 ?
	bls	SUB1
	sub.b	#1,bm_Depth(a0)
	subq	#1,d2
	lsl.l	#2,d2
	add.l	#bm_Planes,d2
	moveq.l	#0,d0
	moveq.l	#0,d1
	move.w	bm_BytesPerRow(a0),d0
	lsl.l	#3,d0
	move.w	bm_Rows(a0),d1
	move.l	(a0,d2),a0
	CALLGRAF	FreeRaster		; free memory
	

	move.l	CScreen,a0
	lea.l	sc_ViewPort(a0),a1
	lea.l	sc_BitMap(a0),a0
	cmp.b	#5,bm_Depth(a0)
	bne	SUB2
	and.w	#$ffff-$80,vp_Modes(a1)	; clear EHB flag !

SUB2	CALLINT	RemakeDisplay
	CALLEXEC	Permit
	bra	IN0
SUB1	CALLEXEC	Permit		; cannot sub bitplane
	sub.l	a0,a0
	CALLINT	DisplayBeep
	bra	wait
	

ADD	CALLEXEC	Forbid		; add bitplane
	move.l	CScreen,a0
	lea.l	sc_BitMap(a0),a0
	moveq.l	#0,d2
	move.b	bm_Depth(a0),d2			
	cmp.b	#5,d2		; depth = 6 ?
	bhi	ADD1
	move.l	CScreen,a1
	lea.l	sc_ViewPort(a1),a1
	move.w	vp_Modes(a1),d0
	and.l	#$8000,d0		; HIRES (x=640) ?
	beq	ADD2
	move.b	bm_Depth(a0),d2			
	cmp.b	#3,d2
	bhi	ADD1
ADD2	moveq.l	#0,d0
	moveq.l	#0,d1
	move.w	bm_BytesPerRow(a0),d0
	lsl.l	#3,d0
	move.w	bm_Rows(a0),d1
	
	CALLGRAF	AllocRaster		; get memory ?
	tst.l	d0
	beq	ADD1
	move.l	CScreen,a0
	lea.l	sc_BitMap(a0),a0
	moveq.l	#0,d2
	move.b	bm_Depth(a0),d2
	add.b	#1,bm_Depth(a0)
	lsl.l	#2,d2
	add.l	#bm_Planes,d2
	move.l	d0,(a0,d2)

	move.l	d0,a1
	move.l	(a0),d0
	swap	d0
	moveq.l	#3,d1
	CALLGRAF	BltClear		; clear bitplane

	move.l	CScreen,a0
	lea.l	sc_BitMap(a0),a0
	cmp.b	#6,bm_Depth(a0)
	bne	ADD3
	move.l	CScreen,a1
	lea.l	sc_ViewPort(a1),a1
	or.w	#$80,vp_Modes(a1)	; depth =6 => set EHB flag !

ADD3	CALLINT	RemakeDisplay
	CALLEXEC	Permit
	bra	IN0
ADD1	CALLEXEC	Permit		; cannot add bitplane
	sub.l	a0,a0
	CALLINT	DisplayBeep
	bra	wait

Size	CALLEXEC	Forbid		
	move.l	Window1_Ptr,a0
	cmp.w	#20,wd_Height(a0)
	bls	MakeBig		;  stretch window to normal size  
	moveq.l	#0,d0
	move.w	#10,d1
	sub.w	wd_Height(a0),d1
	CALLINT	SizeWindow
	CALLEXEC	Permit
	moveq.l	#0,d0
	move.b	#0,Mode
	move.b	Mode,d0
	cmp.b	OldMode,d0
	beq	wait2
	move.b	d0,OldMode
	mulu	#25,d0
	add.l	#Title0,d0
	move.l	d0,a1
	sub.l	a2,a2
	move.l	Window1_Ptr,a0
	CALLINT	SetWindowTitles
	bra	wait
MakeBig	move.l	Window1_Ptr,a0		;crunch window to min. size
	moveq.l	#0,d0
	move.w	wd_TopEdge(a0),d1
	neg.w	d1
	CALLINT	MoveWindow		; move window to top (to have enough room)
	move.l	Window1_Ptr,a0
	moveq.l	#0,d0
	move.w	#120,d1
	CALLINT	SizeWindow		; resize window
	CALLEXEC	Permit
	move.l	#10,d1		; Intuition needs time to understand it 
	CALLDOS	Delay		; (don't ask me why !)
	move.l	Window1_Ptr,a0
	CALLINT	WindowToFront

	bra	IN1

ChangeScreen	move.l	Window1_Ptr,a0		; re-open window on first screen
	CALLINT	CloseWindow
	jmp	start

SET	cmp.b	#$1f,d0		; set RGB (special routine for EHB-mode!)
	bls	SET1
	and.w	#$1f,d0
	lsl	#1,d1
	lsl	#1,d2
	lsl	#1,d3
SET1	CALLGRAF	SetRGB4
	rts

GET	move.l	d0,-(sp)		; get RGB (special routine for EHB-mode!)
	and.l	#$1f,d0
	CALLGRAF	GetRGB4
 	cmp.l	#$1f,(sp)
	bls	GET1
	lsr.w	#1,d0
	and.l	#$777,d0
GET1	tst.l	(sp)+
	rts

R	dc.b	"R"
G	dc.b	"G"
B	dc.b	"B"


RGB	move.l	Window1_Ptr,a1		; print out R, G , B
	move.l	wd_RPort(a1),a1
	move.l	a1,a2
	moveq.l	#1,d0
	CALLGRAF	SetAPen

	move.l	a2,a1	
	move.w	#40,d0
	move.w	#50+15+6+2,d1
	CALLGRAF	Move	

	lea.l	R,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#40,d0
	move.w	#50+15+6+15+2,d1
	CALLGRAF	Move	

	lea.l	G,a0
	moveq.l	#1,d0
	CALLGRAF	Text

	move.l	a2,a1	
	move.w	#40,d0
	move.w	#50+15+6+15+15+2,d1
	CALLGRAF	Move	

	lea.l	B,a0
	moveq.l	#1,d0
	CALLGRAF	Text
	rts	

C1	dc.w	0
C2	dc.w	0

SaveC12	move.l	Window1_Ptr,a0		; save color 0 &1
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	moveq.l	#0,d0
	bsr	GET
	move.w	d0,C1
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	moveq.l	#1,d0
	bsr	GET
	move.w	d0,C2
	rts

XColors	move.l	Window1_Ptr,a0		; set color 0 & 1 to black and white
	CALLINT	ViewPortAddress
	move.l	d0,a0
	moveq.l	#0,d0	
	moveq.l	#0,d1
	moveq.l	#0,d2	
	moveq.l	#0,d3
	bsr	SET
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	moveq.l	#1,d0	
	move.b	#$e,d1
	move.b	#$e,d2	
	move.b	#$e,d3
	bsr	SET
	jsr	NewColor
	bra	wait

XColorsBack	move.l	Window1_Ptr,a0		; use old colors 
	CALLINT	ViewPortAddress
	move.l	d0,a0
	moveq.l	#0,d0	
	move.w	C1,d1
	move.w	d1,d2
	move.w	d2,d3
	and.w	#$f,d3
	lsr.w	#4,d2
	and.w	#$f,d2
	lsr.w	#8,d1
	and.w	#$f,d1
	bsr	SET
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	moveq.l	#1,d0	
	move.w	C2,d1
	move.w	d1,d2
	move.w	d2,d3
	and.w	#$f,d3
	lsr.w	#4,d2
	and.w	#$f,d2
	lsr.w	#8,d1
	and.w	#$f,d1
	bsr	SET
	jsr	NewColor
	bra	wait


NoWindow	move.l	#RECOVERY_ALERT,d0	; error message
	move.l	#30,d1
	lea.l	ErrorText,a0
	CALLINT	DisplayAlert
	move.l	_MathBase,a1
	CALLEXEC	CloseLibrary
	moveq.l	#0,d0
	rts

ErrorText	dc.w	99
	dc.b	17
	dc.b	"XColor   :   ERROR !?!    I cannot open the window !"
	dc.b 	0,0 

	even
ColorBuffer	ds.w	64
UNDOBuffer	ds.w	64
UNDODepth	dc.l	0

* How to make B&W
*
* C=r+g+b    ; add all three parts red, green, blue
* r=g=b=C/3  ; new parts

Black_White	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	Depth,d0		; copy colors to buffer
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	ColorBuffer(pc),a1
.X	move.w	(a0)+,(a1)+
	dbra	d1,.X
	
	move.l	Depth,d0		; convert colors to b&W
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	lea.l	ColorBuffer(pc),a1
.Y	moveq.l	#0,d0
	add.b	(a1),d0
	moveq.l	#0,d2
	move.b	1(a1),d2
	move.b	d2,d3
	and.b	#$f,d3
	lsr.b	#4,d2
	add.b	d2,d0
	add.b	d3,d0
	and.l	#$ff,d0
	divu	#3,d0
	move.b	d0,(a1)+
	move.b	d0,d2
	lsl.b	#4,d0
	or.b	d2,d0
	move.b	d0,(a1)+
	dbra	d1,.Y
	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	lea.l	ColorBuffer(pc),a1
	move.l	Depth,d1		
	moveq.l	#0,d0
	bset	d1,d0
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	bra	IN2

* How to make ANTIK
*
* C=r+g+b    ; add all three parts red, green, blue
* r=C/3      ; new parts
* g=C/4
* b=C/5

ANTIK	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	Depth,d0		; copy colors to buffer
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	ColorBuffer(pc),a1
.X	move.w	(a0)+,(a1)+
	dbra	d1,.X
	
	move.l	Depth,d0		; convert colors to b&W
	moveq.l	#0,d1
	bset	d0,d1
	subq	#1,d1
	lea.l	ColorBuffer(pc),a1
.Y	moveq.l	#0,d0
	add.b	(a1),d0
	moveq.l	#0,d2
	move.b	1(a1),d2
	move.b	d2,d3
	and.b	#$f,d3
	lsr.b	#4,d2
	add.b	d2,d0
	add.b	d3,d0
	and.l	#$ff,d0
	move.l	d0,d5
	divu	#3,d0
	move.b	d0,(a1)+
	move.l	d5,d0
	lsl.w	#2,d0
	and.w	#$f0,d0
	divu	#5,d5
	or.b	d5,d0
	move.b	d0,(a1)+
	dbra	d1,.Y
	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	lea.l	ColorBuffer(pc),a1
	move.l	Depth,d1		
	moveq.l	#0,d0
	bset	d1,d0
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	bra	IN2


SaveForUndo	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	Depth,d0		
	moveq.l	#0,d1
	bset	d0,d1
	cmp.b	#32,d1
	bls	.Z
	move.l	#32,d1
.Z	move.l	d1,UNDODepth
	subq	#1,d1

	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a0
	lea.l	UNDOBuffer(pc),a1
.X	move.w	(a0)+,(a1)+
	dbra	d1,.X		
	rts

UNDO_ALL	move.l	Window1_Ptr,a0		; restor save color map		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	lea.l	UNDOBuffer(pc),a1
	move.l	UNDODepth,d0		
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay
	bra	IN3


Mode	dc.b	0
OldMode	dc.b	$ff
	even


Title0	dc.b	"XColor 1.2 by RF        ",0
Title1	dc.b	"Copy to :               ",0
Title2	dc.b	"Exchange to :           ",0
Title3	dc.b	"Spread to :             ",0 

COPY	cmp.b	#1,Mode
	beq	.Label1
	move.b	#1,Mode
	bra	wait
.Label1	clr.b	Mode
	bra	wait

Copy_Color	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a3	; pointer color map

	move.w	d6,d0		; get color		
	move.w	d7,d1		
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel
	
	move.l	d0,d3		; You can only change colours between 0 and $1f or between $20 and $3f
	move.l	COLOR,d4
	and.b	#$20,d3
	and.b	#$20,d4
	cmp.b	d3,d4
	bne	Error


	and.w	#$1f,d0		; change colours ( EHB colour-> normal colour) 
	add.w	d0,d0
	move.l	COLOR,d1
	and.w	#$1f,d1
	add.w	d1,d1



	move.w	(a3,d1),(a3,d0)	; copy it !
	clr.b	Mode

	CALLINT	RemakeDisplay		
	bra	IN2

EXCHANGE	cmp.b	#2,Mode
	beq	.Label1
	move.b	#2,Mode
	bra	wait
.Label1	clr.b	Mode
	bra	wait

Exchange_Color	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a3	; pointer color map

	move.w	d6,d0		; get color		
	move.w	d7,d1		
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel

	move.l	d0,d3		; You can only change colours between 0 and $1f or between $20 and $3f
	move.l	COLOR,d4
	and.b	#$20,d3
	and.b	#$20,d4
	cmp.b	d3,d4
	bne	Error

	and.l	#$1f,d0

	add.w	d0,d0
	move.l	COLOR,d1
	and.l	#$1f,d1
	add.w	d1,d1

	move.w	(a3,d1),d2		; exchange it !
	move.w	(a3,d0),(a3,d1)
	move.w	d2,(a3,d0)
	
	clr.b	Mode

	CALLINT	RemakeDisplay		
	bra	IN2

SPREAD	cmp.b	#3,Mode
	beq	.Label1
	move.b	#3,Mode
	bra	wait
.Label1	clr.b	Mode
	bra	wait

Spread_Color	move.l	Window1_Ptr,a0		
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	vp_ColorMap(a0),a0
	move.l	cm_ColorTable(a0),a3	; pointer color map

	move.w	d6,d0		; get color		
	move.w	d7,d1		
	move.l	Window1_Ptr,a1
	move.l	wd_RPort(a1),a1
	CALLGRAF	ReadPixel
	move.l	COLOR,d1

P	cmp.w	d1,d0
	bls	.L1
	move.l	d0,d2
	move.l	d1,d0	
	move.l	d2,d1
.L1	move.l	d1,d2
	sub.l	d0,d2
	cmp.l	#1,d2
	bls	.exit

	move.l	d0,d3	; You can only change colours between 0 and $1f or between $20 and $3f
	move.l	d1,d4
	and.b	#$20,d3
	and.b	#$20,d4
	cmp.b	d3,d4
	bne	Error

	and.l	#$1f,d0
	and.l	#$1f,d1

	move.l	d0,FirstColor
	move.l	d1,LastColor
	move.l	d2,DiffColor
	move.l	d2,d6
	subq	#2,d6

	move.l	FirstColor,d0		;get RGB of first colour 
	bsr	ReadColorII
	movem.l	d0-d2,FirstR
	
	move.l	LastColor,d0		;get RGB of last colour 
	bsr	ReadColorII
	sub.l	FirstR,d0
	move.l	d0,DiffR		; What's the difference ?
	sub.l	FirstG,d1
	move.l	d1,DiffG
	sub.l	FirstB,d2
	move.l	d2,DiffB
	


	moveq.l	#6,d4		; convert integer to FFP
	lea.l	FirstR(pc),a3
.L2	move.l	(a3),d0
	CALLFFP	SPFlt
	move.l	d0,(a3)+
	dbra	d4,.L2

	lea.l	DiffR,a3
	lea.l	FaktorR,a4

	move.l	(a3)+,d0		; How much R (,G,B)  has to be added each colour ?
	move.l	DiffColor,d1
	CALLFFP	SPDiv
	move.l	d0,(a4)+
	move.l	(a3)+,d0
	move.l	DiffColor,d1
	CALLFFP	SPDiv
	move.l	d0,(a4)+
	move.l	(a3)+,d0
	move.l	DiffColor,d1
	CALLFFP	SPDiv
	move.l	d0,(a4)+

.L3	lea.l	FirstR,a3
	lea.l	FaktorR,a4
	lea.l	R_Wert,a5
	
	move.l	(a3),d0		; add to R,G,B
	move.l	(a4)+,d1
	CALLFFP	SPAdd
	move.l	d0,(a3)+
	CALLFFP	SPFix		; convert result to integer
	move.l	d0,(a5)+
	move.l	(a3),d0
	move.l	(a4)+,d1
	CALLFFP	SPAdd
	move.l	d0,(a3)+
	CALLFFP	SPFix
	move.l	d0,(a5)+
	move.l	(a3),d0
	move.l	(a4)+,d1
	CALLFFP	SPAdd
	move.l	d0,(a3)+
	CALLFFP	SPFix
	move.l	d0,(a5)+
	
	add.l	#1,FirstColor		; set colour
	move.l	FirstColor,d0
	movem.l	R_Wert,d1-d3
	bsr	WriteColor
	dbra	d6,.L3

	
	
.exit	clr.b	Mode
	CALLINT	RemakeDisplay		; remake copper list
	bra	IN2


FirstR	dc.l	0
FirstG	dc.l	0
FirstB	dc.l	0
DiffR	dc.l	0
DiffG	dc.l	0
DiffB	dc.l	0
DiffColor	dc.l	0	
FaktorR	dc.l	0
FaktorG	dc.l	0
FaktorB	dc.l	0
FirstColor	dc.l	0
LastColor	dc.l	0
ColorNumber	dc.l	0

R_Wert	dc.l	0
G_Wert	dc.l	0
B_Wert	dc.l	0
	


ReadColorII	move.l	d0,-(sp)
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	(sp)+,d0
	move.l	vp_ColorMap(a0),a0
	and.l	#$1f,d0
	CALLGRAF	GetRGB4
	move.b	d0,d1
	move.b	d0,d2
	and.l	#$f,d2
	lsr.b	#4,d1
	and.l	#$f,d1	
	lsr.w	#8,d0
	and.l	#$f,d0
	rts

WriteColor	and.w	#$1f,d0
	move.l	d0,-(sp)
	move.l	Window1_Ptr,a0
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	(sp)+,d0
	CALLGRAF	SetRGB4
	rts
Error	move.l	CScreen,a0	; User tried to copy, spread or exchange a normal colour and a EHB colour!
	CALLINT	DisplayBeep
	clr.b	Mode
	bra	wait
	

		


		

	
	




		

