;
; colours.s
;
; Colour handling for Sculptrix
;
;  1995-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's Sculptrix.
;
; Sculptrix is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Sculptrix is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Sculptrix.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

;----- External dependencies ------------------------------------------------

		GET	sh.vString

;----- Main code ------------------------------------------------------------

		AREA	|Module$$Code|,CODE,READONLY

; --- colours_read ---
;
; On entry:	R1 == pointer to icon block
;
; On exit:	R0 == foreground in bits 0-3, background in bits 4-7
;		CS if colours in validation string, and
;		  R14 == address of colours in there, or 0
;
; Use:		Works out colours of an icon.

		EXPORT	colours_read
colours_read	ROUT

		ORR	R14,R14,#C_flag		;Set C flag for exit
		STMFD	R13!,{R1,R2,R14}	;Save some registers
		LDR	R14,[R1,#16]		;Load the icon flags
		TST	R14,#&40		;Is the icon antialiased?
		BNE	%50colours_read		;Yes -- skip on then

		MOV	R0,R14,LSR #24		;Get the colours
		LDMFD	R13!,{R1,R2,R14}	;Restore registers
		BICS	PC,R14,#C_flag		;Colours not in valid string

		; --- Extract colours from string ---

50colours_read	MOV	R0,#'f'			;Find the validation command
		MOV	R2,#0			;Start at the beginning
		BL	vString_find		;Locate the string
		MOVCC	R14,#0			;If not found, clear ptr
		MOVCC	R0,#&07			;Return black-on-white
		BCC	%90colours_read		;And skip on

		ADD	R14,R2,#1		;Point at the characters
		LDRB	R0,[R14,#1]		;Load the foreground colour
		SUB	R0,R0,#'0'		;Convert to digit
		CMP	R0,#10			;Too big?
		SUBCS	R0,R0,#7		;Yes -- must be a letter
		CMP	R0,#16			;Still too big?
		SUBCS	R0,R0,#&20		;Yes -- lowercase then

		LDRB	R1,[R14,#0]		;Load the background colour
		SUB	R1,R1,#'0'		;Convert to digit
		CMP	R1,#10			;Too big?
		SUBCS	R1,R1,#7		;Yes -- must be a letter
		CMP	R1,#16			;Still too big?
		SUBCS	R1,R1,#&20		;Yes -- lowercase then
		ORR	R0,R0,R1,LSL #4		;Merge the two together
90colours_read	LDMFD	R13!,{R1,R2,PC}^	;And return to caller

		LTORG

; --- colours_set ---
;
; On entry:	R1 == pointer to icon /state/ block
;		R2 == new background colour
;
; On exit:	R2 == old background colour
;
; Use:		Changes the background colour of an icon.

		EXPORT	colours_set
colours_set	ROUT

		STMFD	R13!,{R0-R3,R14}	;Save some registers
		ADD	R1,R1,#8		;Point to the main icon data
		BL	colours_read		;Read the old colours
		MOV	R0,R0,LSR #4		;Shift down the colour
		STR	R0,[R13,#8]		;Return this in R2 later
		BCS	%50colours_set		;Handle anti-aliasing

		MOV	R2,R2,LSL #28		;Put colours in right place
		MOV	R3,#&F0000000		;Just change background colr
		B	%70colours_set		;And change the colour

		; --- Handle anti-aliasing ---

50colours_set	CMP	R14,#0			;Can we do this?
		BEQ	%90colours_set		;No -- don't then
		ADD	R2,R2,#'0'		;Convert to decimal
		CMP	R2,#'9'+1		;Is it in range?
		ADDCS	R2,R2,#7		;No -- make it a letter
		STRB	R2,[R14,#0]		;Store background colour
		MOV	R2,#0			;Don't change flags
		MOV	R3,#0			;No, really, don't

70colours_set	LDMDB	R1,{R0,R1}		;Load window and icon handle
		STMFD	R13!,{R0-R3}		;Save them registers
		MOV	R1,R13			;Point to the block
		SWI	XWimp_SetIconState	;Set the icon state
		ADD	R13,R13,#16		;Restore the stack
90colours_set	LDMFD	R13!,{R0-R3,PC}^	;And return to caller

		LTORG

;----- That's all, folks ----------------------------------------------------

		END
