;%11LLLLL0 10CCCCC0 (+inc) to light+hue LC1=%11ccLLL1, hue %11RGCld0
;%11RGCld0 11RGCld0=twohues12,twohues34 to attr and attrcode=%1aaa1111
;%11ccLLL1 1aaaLLL1 (left,right) twochunks to gfx

chmap_cls
chmap_cls_color
;de=clear data
	ld (clsp),sp
	ld hl,chmap_color+(chmwid*2)
	ld b,chmhgt
cl0
	ld sp,hl	
	dup chmwid
	push de
	edup
	inc h
	djnz cl0
clsp=$+1
	ld sp,0
	ret



c2p
c2p_color
;190000..196000 (64x32)
	ld (prchsp),sp

;convert color and light (chmap) to light+hue (chmapto) and twohues (chmapattr)
	ld a,chmap_color/256
prech0:
	ld h,a
	ld l,chmap_color&255
	ld sp,hl
	ex de,hl ;chmapto=chmap
	ld b,d
	res 0,b
	and 1
	add a,chmwid+(chmapattr&255)
	ld c,a
;sp=chmap
;de=chmapto
;bc=chmapattr+chmwid+1 or 0 (odd lines)

	dup chmwid/2
	pop hl ;l=color,h=light
	ldi ;light+hue LC1=%11ccLLL1
 ;NB: skip one byte in (bc)!
	ld a,(hl) ;hue
	pop hl ;l=color,h=light
	ldi ;light+hue LC2=%11ccLLL1
	or (hl) ;hue
	ld (bc),a ;twohues %11RGCld0
	edup
;cc=
;00 - W (with 0-Ñ1 gives "wrong 0-C1", or else "0-W")
;01 - R
;10 - C/G
;11 - C1-C2 (don't mind the hue)
;l means lighter 1/3 range of light is used
;d means dark 1/3 range of light is used
;if hues are different (except for exactly one "incompatible" hue with double upper or double dark range), then 0-W
;if lighter and dark range are both marked, then 0-W
;if lighter range is marked, then C2-W
;if dark range is marked, then 0-C1
;if no ranges are marked, then C1-C2 (with C1-C2 marked in cc too)

	ld a,d
	inc a
	cp chmhgt+(chmap_color/256)
	jp nz,prech0

;draw chunks on screen
	ld hl,attrtop;#5880
	ld (prchattr),hl
	ld a,chmapattr/256 ;todo remove

prch0:
;attrs (sp=chmapattr, de=attr, bc=chmapto+chmwid-1)
	ld h,a
	ex af,af'
	ld l,chmapattr&255
	ld sp,hl
	ld b,h
	ld c,chmwid+(chmapto&255)-1
prchattr=$+1
	ld de,0

	dup chmwid/4
	pop hl ;twohues12,twohues34=%11RGCld0
	ldi ;attr
 ;NB: skip HSB chunk in (bc)!
	ld a,(bc)
	and (hl) ;attrcode=%1aaa1111
	ld (bc),a ;correct top LSB chunk
	inc b
	ld a,(bc)
	and (hl) ;attrcode=%1aaa1111
	ld (bc),a ;correct bottom LSB chunk
	dec c
	pop hl ;twohues12,twohues34=%11RGCld0
	ldi ;attr
 ;NB: skip HSB chunk in (bc)!
	ld a,(bc)
	and (hl) ;attrcode=%1aaa1111
	ld (bc),a ;correct bottom LSB chunk
	dec b
	ld a,(bc)
	and (hl) ;attrcode=%1aaa1111
	ld (bc),a ;correct top LSB chunk
	dec c
	edup
	org $-1
	
;de=end of attr line+1
;bc=start of chmapto line
	ld (prchattr),de ;if chmwid=64, it's a next line
	dec de ;de=end of attr line

;bottom chunks
	ld h,b,l,c
	ld (prchtopsp),hl
	inc h
	ld sp,hl
	ld a,d ;%010110xx
	add a,a
	add a,a
	add a,a ;%110xx000
	sub #7f
	ld d,a ;%010xx001
	ld (prchtopde),de
	set 2,d
	ld b,d,c,e
	inc b
	inc bc

	dup chmwid/4
	pop hl ;twochunks
	ld a,(hl)
	ld (de),a
	dec d
	ldd
	ld a,(hl)
	ld (bc),a
	inc b
	ld (bc),a
	pop hl ;twochunks
	ld a,(hl)
	ld (de),a
	inc d
	ldd
	ld a,(hl)
	ld (bc),a
	dec b
	ld (bc),a
	edup

;top chunks
prchtopsp=$+1
	ld sp,0
prchtopde=$+1
	ld de,0
	ld b,d,c,e
	inc b
	inc bc

	dup chmwid/4
	pop hl ;twochunks
	ld a,(hl)
	ld (de),a
	dec d
	ldd
	ld a,(hl)
	ld (bc),a
	inc b
	ld (bc),a
	pop hl ;twochunks
	ld a,(hl)
	ld (de),a
	inc d
	ldd
	ld a,(hl)
	ld (bc),a
	dec b
	ld (bc),a
	edup

	ex af,af'
	add a,2
	cp chmhgt+(chmapattr/256)
	jp nz,prch0
prchsp=$+1
	ld sp,0
	ret
