; Palette Changer V1.0
; 2 Mar '92

; Coded by Niels 'Nitro' Keurentjes
;	   Hartelstein 50
;	   5655 AL Eindhoven
;	   The Netherlands

; This program searches the current Copperlists in the GFX-library
; and changes them to your configuration.
; The command consists of only 2 basic modules:
;	- The command interpreter and copperchanger
;	- The ASCII-hex to binary converter

; Syntax : PALETTE 000111222333

;  The numbers state the changing color. There are three numbers
;  for every color: Red, Green and Blue. Example:
; PALETTE 262FFFF00AAA
;  This will give you an armygreen screen with white text, red window-
;  switch signs and middle grey cursor.

;  You may also enter rubbish as a color, example:
; PALETTE 262///F00AAA
;  This does exactly the same as above, except for NOT changing the
;  text color.

; But let's start the program...

cmp.b	#12,d0
blt.s	TooBad		; There aren't enough inputs!
move.l	a0,-(sp)	; Save the important register: A0
move.l	$4.w,a6		; Get execbase
lea	gn(pc),a1
jsr	-408(a6)	; Open GFX-library
move.l	d0,a5		; Backup at A5
move.l	(sp)+,a0

; Now starting the interpreter

Loop0:
cmp.b	#32,(a0)+	; Check for text.
beq.s	Loop0
subq	#1,a0

; A0 may be odd now, so be careful with word adressing...

move.l	38(a5),a1
move.l	50(a5),a2
moveq	#3,d7		; Initialize loop for 4 colors
addq	#4,a2

GiantLoop:
moveq	#0,d0		; clear D0 as a longword
move.b	(a0)+,d0
lsl.l	#8,d0		; keep on shifting
move.b	(a0)+,d0
lsl.l	#8,d0		; shift again
move.b	(a0)+,d0
bsr.s	Converter	; start the genial converter...
tst.w	d1
bmi.s	Next		; Oops...
cmp.w	#3,d7		; Working on the first colour?
bne.s	Normal		; Nah
move.w	d1,2(a1)	; Top part of screen

Normal:
move.w	d1,2(a2)	; The actual colors

Next:
addq	#4,a2
dbra	d7,GiantLoop

TooBad:
moveq	#0,d0		; Clear errorcode
rts

; ASCII to binary converter V1.0
; NK 2 Mar '92

; Needs : D0 Convertable
; Gives	: D1 Result

Converter:
move.l	d2,-(sp)	; save workregister
moveq	#0,d1
and.l	#$ffffff,d0	; forget the leftmost character (safety...)

Recheck:
move.b	d0,d2
tst.b	d2
beq.s	Quit		; Found it!
cmp.b	#$39,d2
bgt.s	Letter
cmp.b	#$30,d2
bge.s	Number

; Ascii codes lower than 48 are rubbish, so...

bra.s	QuitThisMess

Number:			; We've found a number!
sub.b	#$30,d2		; Now it's a realistic number
ror.l	#8,d0
or.b	d2,d1
ror.l	#4,d1
bra.s	Recheck

Letter:
bclr.l	#5,d2		; Make it upper case
sub.b	#55,d2
cmp.b	#10,d2
blt.s	QuitThisMess	; Lesser than 10? Drop dead.
cmp.b	#15,d2
bgt.s	QuitThisMess	; Bigger? Idem dito...
or.b	d2,d1
ror.l	#8,d0
ror.l	#4,d1
bra.s	Recheck

QuitThisMess:
moveq	#-1,d1		; Errorcode

Quit:
rol.l	#8,d1		; Real number, please...
rol.l	#4,d1
move.l	(sp)+,d2	; pull workregister from stack
rts

GN:	dc.b 'graphics.library',0
