Skip to content

Commit 9e7835a

Browse files
committed
Optimized _Darken and _Lighten
1 parent 6643e90 commit 9e7835a

File tree

1 file changed

+69
-78
lines changed

1 file changed

+69
-78
lines changed

CEdev/lib/src/graphics/graphx/graphics_lib.asm

+69-78
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,29 @@ _Lighten:
174174
; arg1 : 8 bit change amount
175175
; Returns:
176176
; 16 bit color value
177-
pop de
178-
pop bc
179-
pop hl
180-
push hl
177+
178+
; Read params
179+
pop de ;; de = return vector
180+
pop bc ;; bc = color
181+
ex (sp),hl ;; l = amt
181182
push bc
182183
push de
184+
; Strategy: lighten(color, amt) = ~darken(~color, amt)
185+
; Darken the inverted color
186+
ld a,c
187+
cpl
188+
ld c,a
183189
ld a,b
184190
cpl
185-
ld b,a
186-
ld a,c
187-
cpl ; invert 16 bit color input
188-
call _Darken_ASM \.r
191+
ld b,a ;; bc = ~color
192+
call _Darken_ASM \.r ;; hl = darken(~color, amt)
193+
; Invert the darken result for the lighten result
189194
ld a,l
190195
cpl
191196
ld l,a
192197
ld a,h
193198
cpl
194-
ld h,a ; invert output
199+
ld h,a ;; hl = ~darken(~color, amt) = lighten(color, amt)
195200
ret
196201
197202
;-------------------------------------------------------------------------------
@@ -202,79 +207,64 @@ _Darken:
202207
; arg1 : 8 bit change amount
203208
; Returns:
204209
; 16 bit color value
205-
pop de
206-
pop bc
207-
pop hl
208-
push hl
210+
211+
; Read params
212+
pop de ;; de = return vector
213+
pop bc ;; bc = color
214+
ex (sp),hl ;; l = amt
209215
push bc
210216
push de
211-
ld a,c
217+
; Comments assume 1555 RGB color
212218
_Darken_ASM:
213-
ld c,l
214-
ld iyl,a
215-
ld de,128
216-
and a,31
217-
ld l,a
218-
ld h,c
219-
mlt hl
220-
add hl,de
221-
push hl ; h = blue color
222-
ld a,b
223-
rrca
224-
rrca
225-
and a,31
226-
ld l,a
227-
ld h,c
228-
mlt hl
229-
add hl,de
230-
push hl ; h = red color
231-
ld a,iyl
232-
rrca
233-
rrca
234-
rrca
235-
rrca
236-
and a,14
237-
ld l,a
238-
ld a,b
239-
and a,3
240-
rlca
241-
rlca
242-
rlca
243-
rlca
244-
or a,l
245-
ld l,a
246-
ld a,h
247-
rlca
248-
and a,1
249-
or a,l
250-
ld l,a
251-
ld h,c
252-
mlt hl
253-
add hl,de ; h = green color
254-
ld a,h
255-
and a,1
256-
rrca
257-
ld d,a
219+
; Calculate the output blue value
220+
push bc
221+
ld a,c ;; a = color & $FF
222+
ld c,l ;; c = amt
223+
and a,%00011111
224+
ld h,a ;; h = blue
225+
mlt hl ;; hl = blue * amt
226+
ld de,128 ;; de = 128
227+
add hl,de ;; hl = blue * amt + 128
228+
ld l,h
229+
ld h,d ;; hl = (blue * amt + 128) / 256 = blue_out
230+
ex (sp),hl ;; hl = color, tmp1 = blue_out
231+
; Isolate the input red value
232+
ld a,h ;; a = color >> 8
233+
rra ;; a = color >> 9
234+
and a,%00111110
235+
ld b,a ;; b = red << 1
236+
; Calculate the output green value
237+
add.s hl,hl
238+
rla ;; a & 1 = green & 1
239+
add hl,hl
240+
add hl,hl ;; hl = color << 3
241+
rra
258242
ld a,h
259-
rrca
260-
rrca
261-
rrca
262-
rrca
263-
ld l,a
264-
and a,3
265-
or a,d
266-
ld d,a
267-
ld a,l
268-
and a,224
269-
ld e,a ; green store complete
270-
pop af ; a = red color
271-
rlca
272-
rlca
273-
or a,d
274-
ld h,a ; high byte complete
275-
pop af ; a = blue color
276-
or a,e
277-
ld l,a ; hl complete
243+
rla
244+
and a,%00111111
245+
ld h,a ;; h = green
246+
ld l,c ;; l = amt
247+
mlt hl ;; hl = green * amt
248+
add hl,de ;; hl = green * amt + 128
249+
ld l,h ;; l = (green * amt + 128) / 256 = green_out
250+
; Calculate the output red value
251+
mlt bc ;; bc = red * amt << 1
252+
inc b ;; b = (red * amt + 128 << 1) / 256
253+
srl b ;; b = (red * amt + 128) / 256 = red_out
254+
; Position the output red and green bits
255+
add hl,hl
256+
add hl,hl ;; l = green_out << 2
257+
ld h,b ;; h = red_out
258+
add hl,hl
259+
add hl,hl ;; hl = (red_out << 10) | (green_out << 4)
260+
bit 4,l
261+
jr z,_
262+
set 7,h
263+
res 4,l
264+
_ ;; hl = (green_out & 1 << 15) | (red_out << 10) | (green_out >> 1 << 5)
265+
; Add the output blue value (no positioning necessary) for the final output color
266+
pop bc ;; bc = blue_out
267+
add hl,bc ;; hl = color_out
278268
ret
279269
280270
;-------------------------------------------------------------------------------
@@ -4547,3 +4537,4 @@ tmpCharData:
45474537
.db 0,0,0,0,0,0,0,0
45484538
45494539
.endLibrary
4540+

0 commit comments

Comments
 (0)