26 users online:  Aja, AjaTheVampire,  Alex, AmazingChest,  Anorakun,  DeppySlide, E-man38, edgar, ElpoderosoBv, FrozenQuills, Green Jerry, JupiHornet, MANGOMILK, mish1, Phyll, RollingRigatonis,  RussianMan, Shining Hero, SJandCharlieTheCat,  Telinc1, The Snootster,  TickTockClock, Valentine, VinylHeart, WhiteYoshiEgg, Zavok - Guests: 116 - Bots: 119
Users: 54,920 (2,078 active)
Latest user: MarRuiz

The Fade Routine

Not sure if it's worth posting in the Advanced Documentation or not. And it's less digestable if I format it to merge with the disassembly. But thought I'd save this somewhere *searchable* rather than a filebin.

I understand how it all works now and hopefully commented it fully. Although I have no idea about the significance of the specific value configuration in the FadeTable. There's likely some math equation behind it.
It seems you could tweak the table though for different fade gradients. Also it seems likely that you could FadeToColor and put any color you wanted in $02 and the target color in A. Although I'd have to test that, and haven't time atm.

These are RTL versions of SMW's JSR routines, but other than that they're exactly the same. SMW ping pongs back and forth between the two palette tables to run this. Keeping one as a fade buffer. If you need additional usage hints you can lookup how SMW uses JSR CODE_00AFC0 but usage is fully explained here.
!fade_timer = $1495		; $20 steps (!fade_timer=$40) in a single fade cycle
; Fades may finish earlier or later. There are 32 steps and 32 intensities.
; But some steps may be skipped to do a relative fade.
; So you'll likely need to do a second cycle for large transitions in intensity.

GetFadeInfo:	; CODE_00AFA3 (run once per frame)
	LDA !fade_timer : TAY : INC #2 : STA !fade_timer	; dw INC
	REP #$30
	TYA : LSR #4 : AND #$0002 : STA $0C
	TYA : AND #$001E : TAY					; get as valid index
	; Note: 32 steps in fade but they're repeated twice a cycle, so $0E uses 16 entries
	LDA RGBFrameLimit,y : STA $0E

; FadeColor		Fade a single color over 32 steps
; 16-bit A, X-bit X/Y
; Inputs:
; A		original SNES RGB value	(Fade 00%, "target")
; $02	current SNES RGB value	(Fade XX%, "blackened")
; $0C	00 or 02, depending on which half of the timer you're in.
; $0E	Current Frame divided by 2
; Returns:
; $04	new SNES RGB value (store back into where $02 came from)
; $06	Base R component, divided by 2 (0rrr rr00)
; $08	Base G component, divided by 2 (0ggg gg00)
; $0A	Base B component, divided by 2 (0bbb bb00)
; Note: You need to keep the original color around in a buffer!
FadeColor:	; CODE_00AFC0
	; get raw components of color in 6/8/A
	STA $0A				; save temp
	AND #$001F : ASL #2 : STA $06			; R
	LDA $0A : AND #$03E0 : LSR #3 : STA $08	; G
	LDA $0B : AND #$007C : STA $0A			; B
	STZ $04				; get new color
	LDY #$0004
.loop:	; for each component
	PHY	; can we relative color fade this frame?
	LDA $0006,y : ORA $0C : TAY : LDA FadeTable,y : AND $0E		; See FadeTable
	BEQ .no_update		; fade allowed?
	BIT $1493 : BPL .update	; fade to black or color? (checking $1494)
.update: .no_update:
	CLC : ADC $02 : AND RGB_Mask,y : TSB $04	; pack rgb (clamp update to component)
	DEY #2
	BPL .loop

RGB:	; RGB component tables (in R,G,B order)
.Mask:	;$00AE65	SNES RGB component locations (-bbb bbgg gggr rrrr)
	dw $001F,$03E0,$7C00
.DEC:	;$00AE6B	Fade to black table. Decreases a component by 1.
	dw $FFFF,$FFE0,$FC00
.INC:	;$00AE71	Fade to color table. Increases a component by 1.
	dw $0001,$0020,$0400

RGBFrameLimit:	;$00AEF7	Table for the maximum values of a component to allow fading each frame. (AND Table)
	dw $8000,$4000,$2000,$1000,$0800,$0400,$0200,$0100
	dw $0080,$0040,$0020,$0010,$0008,$0004,$0002,$0001

FadeTable:	;$00AE77	Table of how much each component is allowed to fade relative to its brightness.
; each row is intensity of component doing lookup (32 intensities)
; left column is first half of timer	+($0C = 0)
; right column is second half of timer	+($0C = 2)
; Essentially we'll slide across all columns over 32 frames. Left to right. (AND $0E)
; If the bit is set █, then we're allowed to modify that intensity that frame
	dw $0000,$0000	;░░░░░░░░░░░░░░░░,░░░░░░░░░░░░░░░░
	dw $0001,$0000	;░░░░░░░░░░░░░░░█,░░░░░░░░░░░░░░░░
	dw $8000,$8000	;█░░░░░░░░░░░░░░░,█░░░░░░░░░░░░░░░
	dw $8020,$0400	;█░░░░░░░░░█░░░░░,░░░░░█░░░░░░░░░░
	dw $8080,$8080	;█░░░░░░░█░░░░░░░,█░░░░░░░█░░░░░░░
	dw $8208,$1040	;█░░░░░█░░░░░█░░░,░░░█░░░░░█░░░░░░
	dw $8420,$8420	;█░░░░█░░░░█░░░░░,█░░░░█░░░░█░░░░░
	dw $8844,$2210	;█░░░█░░░░█░░░█░░,░░█░░░█░░░░█░░░░
	dw $8888,$8888	;█░░░█░░░█░░░█░░░,█░░░█░░░█░░░█░░░
	dw $9122,$4488	;█░░█░░░█░░█░░░█░,░█░░░█░░█░░░█░░░
	dw $9248,$9248	;█░░█░░█░░█░░█░░░,█░░█░░█░░█░░█░░░
	dw $A492,$4924	;█░█░░█░░█░░█░░█░,░█░░█░░█░░█░░█░░
	dw $A4A4,$A4A4	;█░█░░█░░█░█░░█░░,█░█░░█░░█░█░░█░░
	dw $A949,$5294	;█░█░█░░█░█░░█░░█,░█░█░░█░█░░█░█░░
	dw $AAAA,$5294	;█░█░█░█░█░█░█░█░,░█░█░░█░█░░█░█░░
	dw $AAAA,$5554	;█░█░█░█░█░█░█░█░,░█░█░█░█░█░█░█░░
	dw $AAAA,$AAAA	;█░█░█░█░█░█░█░█░,█░█░█░█░█░█░█░█░
	dw $D5AA,$AAAA	;██░█░█░██░█░█░█░,█░█░█░█░█░█░█░█░
	dw $D5AA,$D5AA	;██░█░█░██░█░█░█░,██░█░█░██░█░█░█░
	dw $D6B5,$AD6A	;██░█░██░█░██░█░█,█░█░██░█░██░█░█░
	dw $DADA,$DADA	;██░██░█░██░██░█░,██░██░█░██░██░█░
	dw $DB6D,$B6DA	;██░██░██░██░██░█,█░██░██░██░██░█░
	dw $EDB6,$EDB6	;███░██░██░██░██░,███░██░██░██░██░
	dw $EEDD,$BB76	;███░███░██░███░█,█░███░██░███░██░
	dw $EEEE,$EEEE	;███░███░███░███░,███░███░███░███░
	dw $F7BB,$DDEE	;████░████░███░██,██░███░████░███░
	dw $FBDE,$FBDE	;█████░████░████░,█████░████░████░
	dw $FDF7,$EFBE	;██████░█████░███,███░█████░█████░
	dw $FEFE,$FEFE	;███████░███████░,███████░███████░
	dw $FFDF,$FBFE	;██████████░█████,█████░█████████░
	dw $FFFE,$FFFE	;███████████████░,███████████████░
	dw $FFFF,$FFFE	;████████████████,███████████████░