int dv;
.
.
.
clr = get_color(i);
dv = 0;
/* low color bits : 0000 0111 */
if (clr & 0x0007 ) dv ++;
/* mid color bits : 0011 1000 */
if (clr & 0x0028 ) dv = dv+ 8; /* iirc, this is faster than += in huc. try both */
/* hi color bits : 1 1100 0000 */
if (clr & 0x01c0 ) dv = dv + 64;
clr = clr - dv;
set_color(i, clr);
.
.
.
...............................................................................
that's off the top of my head, so double check the hex values
And everything else....
Assuming it works, the next step is to move that to asm; iirc, an int parameter will come in in the A and X registers, so color won't have to be loaded; the rest is a pretty straightfoward conversion.
( look up the asm code in the listing to see how it's done. That's how I learned 650x asm
For the low sets of bits, you can just check the low byte; for the high set, shift the color right (?) for the check (then you can and with 0xe0 ) But that's a bit of asm optimization you may not need (or want to do).
.................................................................................
Just out of curiosity, why are you ckecking for >0 anyway? clr can't be negative, an & won't change that. And != 0 is true in C, so you don't have to compare the result to anything....