;	This is the code created by ColorCatch V1.0
;	by Preben Nielsen.
;	Assemble it as case-sensitive.
;	OPT O+
;	OPT O1+		;Tells when a branch could be optimised to short
;	OPT i+		;Tells when '#' is probably missing

	incdir	"INCLUDE:"
	include "exec/exec_lib.i"
	include "graphics/view.i"
	include "graphics/graphics_lib.i"
	include "intuition/intuition.i"
	include "intuition/intuition_lib.i"
	include "libraries/dos.i"
	include "libraries/dosextens.i"
	include "libraries/dos_lib.i"

LoadBase	MACRO
		IFNC		'\1','ExecBase'
		movea.l		\1(PC),A6
		ENDC
		IFC		'\1','ExecBase'
		movea.l		4.W,A6
		ENDC
		ENDM
CallLib		MACRO
		jsr		_LVO\1(A6)
		ENDM
Call		MACRO
		bsr		\1
		ENDM
Push		MACRO
		movem.l		\1,-(SP)
		ENDM
Pop		MACRO
		movem.l		(SP)+,\1
		ENDM

ColorExe	Push		D0-D7/A0-A6
		LoadBase	ExecBase
		suba.l		A1,A1
		CallLib		FindTask		;Find us
		movea.l		D0,A2
		tst.l		pr_CLI(A2)
		bne.S		WBAndCLIStartUp
		lea		pr_MsgPort(A2),A0
		CallLib		WaitPort		;wait for a message
		lea		pr_MsgPort(A2),A0
		CallLib		GetMsg			;then get it
		move.l		D0,WBenchMsg		;save it for later reply
WBAndCLIStartUp	lea		GfxName(PC),A1
		CallLib		OldOpenLibrary
		move.l		D0,GfxBase
		beq.S		Exit
		lea		IntName(PC),A1
		CallLib		OldOpenLibrary
		move.l		D0,IntBase
		beq.S		Exit

		move.l		D0,A0
		move.l		ib_FirstScreen(A0),A0	;IntuitionBase->FirstScreen
		lea		sc_ViewPort(A0),A0	;Screen.ViewPort
		move.w		Count(PC),D0
		lea		ColorTab(PC),A1
		LoadBase	GfxBase
		CallLib		LoadRGB4		;Set new colors

Exit		LoadBase	ExecBase
FreeInt		move.l		IntBase(PC),D0
		beq.S		FreeGfx
		movea.l		D0,A1
		CallLib		CloseLibrary
FreeGfx		move.l		GfxBase(PC),D0
		beq.S		ReplyWB
		movea.l		D0,A1
		CallLib		CloseLibrary
ReplyWB		move.l		WBenchMsg(PC),D0
		beq.S		AllDone
		CallLib		Forbid
		movea.l		D0,A1
		CallLib		ReplyMsg		;Reply WBenchMessage if we are started from WB
AllDone		Pop		D0-D7/A0-A6
		moveq		#0,D0
		rts
GfxName		dc.b		'graphics.library',0
IntName		dc.b		'intuition.library',0
		EVEN
Count		dc.w		0
;This is where ColorCatch stores the colors
ColorTab	dcb.w		32,0
GfxBase		dc.l		0
IntBase		dc.l		0
WBenchMsg	dc.l		0
		END

