#include "game_types.h"

/* BG_BlendChannel @ 0x08014304 (real 160B) -- two-mode palette index/blend lookup. Mirrors src/matched/BG_BlendChannel.c. */
/* Two-mode lookup: mode 4 indexes table 0x08547080 (clamped 0..59); otherwise
   blends two palette entries from 0x08547068 around table 0x08546FE4 with the
   ip factor (255 when clamped, else 0), plus 0x800 bias. */
int BG_BlendChannel(unsigned a, unsigned b, unsigned c)
{
    (void)c;
    if (a == 4) {
        unsigned i = 0;
        if (b > 20) {
            i = b - 21;
            if (i > 59)
                i = 59;
        }
        return ((unsigned char *)0x08547080)[i];
    }
    {
        unsigned ip = 0;
        unsigned i = 0;
        unsigned char *tb = (unsigned char *)0x08546FE4;
        short *pal = (short *)0x08547068;
        int t0, g, t1, g2, d;
        if (b > 35) {
            i = b - 36;
            if (i > 130) {
                i = 130;
                ip = 255;
            }
        }
        t0 = tb[i];
        g = pal[(t0 & 15) * 2] >> ((t0 >> 4) & 0xFF);
        t1 = tb[i + 1];
        g2 = pal[(t1 & 15) * 2] >> ((t1 >> 4) & 0xFF);
        d = g2 - g;
        return g + ((d * (int)ip) >> 8) + 0x800;
    }
}
