Always outputs rounded down and rounded up variants
This commit is contained in:
parent
939a9019aa
commit
cc344d9e90
|
@ -1,13 +1,13 @@
|
|||
# rgb555conv - converts 24 bit color to 15 bit color
|
||||
I got tired of manually converting 24 bit hex colors (like `0xRRGGBB`) into 15
|
||||
bit RGB555 colors (like `0b0BBBBBGGGGGRRRRR`) so I slapped this together to do
|
||||
bit BGR555 colors (like `0b0BBBBBGGGGGRRRRR`) so I slapped this together to do
|
||||
it for me. Be warned that the 4 character color it outputs is in BGR format
|
||||
instead of RGB for use on the Game Boy Color and Game Boy Advance. It also
|
||||
for use on the Game Boy Color and the Game Boy Advance instead of RGB. It also
|
||||
outputs a 24 bit representation of the 15 bit color (still in `0xRRGGBB`) for
|
||||
use in things like your sprite software.
|
||||
|
||||
If it outputs a rounded and unrounded variant, it's up to you to look at it and
|
||||
decide which you like, because they may differ significantly.
|
||||
It outputs a rounded down and rounded up variant, it's up to you to look at it
|
||||
and decide which you like, because they may differ significantly.
|
||||
|
||||
## compiling:
|
||||
|
||||
|
|
27
main.c
27
main.c
|
@ -21,28 +21,23 @@ int main (int argc, char *argv[])
|
|||
printf(" Input, 24 bit color, 6 char hex: #%06X\n\n", in_888);
|
||||
|
||||
rgb888 split;
|
||||
split.r = (in_888 & 0x00FF0000) >> 16;
|
||||
split.g = (in_888 & 0x0000FF00) >> 8;
|
||||
split.b = in_888 & 0x000000FF;
|
||||
split.r = ((in_888 & 0x00FF0000) >> 16) & 0xF0;
|
||||
split.g = ((in_888 & 0x0000FF00) >> 8) & 0xF0;
|
||||
split.b = (in_888 & 0x000000FF) & 0xF0;
|
||||
|
||||
// Basic rounding
|
||||
rgb888 rounded = split;
|
||||
if (split.r & 4) rounded.r |= 8;
|
||||
if (split.g & 4) rounded.g |= 8;
|
||||
if (split.b & 4) rounded.b |= 8;
|
||||
rgb888 rounded;
|
||||
rounded.r = split.r | 8;
|
||||
rounded.g = split.g | 8;
|
||||
rounded.b = split.b | 8;
|
||||
|
||||
int16_t rgb555 = ((split.b & 0xF8) << 7) | ((split.g & 0xF8) << 2) | (split.r >> 3);
|
||||
int16_t rgb555_rounded = ((rounded.b & 0xF8) << 7) | ((rounded.g & 0xF8) << 2) | (rounded.r >> 3);
|
||||
|
||||
if (rgb555 != rgb555_rounded) {
|
||||
printf("Output, 15 bit color, 6 char hex, unrounded: #%06X\n", (uint32_t) ((split.r & 0xF8) << 16) | ((split.g & 0xF8) << 8) | (split.b & 0xF8));
|
||||
printf("Output, 15 bit color, 6 char hex, rounded: #%06X\n", (uint32_t) ((rounded.r & 0xF8) << 16) | ((rounded.g & 0xF8) << 8) | (rounded.b & 0xF8));
|
||||
printf("Output, 15 bit color, 6 char hex, rounded down: #%06X\n", (uint32_t) ((split.r & 0xF8) << 16) | ((split.g & 0xF8) << 8) | (split.b & 0xF8));
|
||||
printf("Output, 15 bit color, 6 char hex, rounded up: #%06X\n", (uint32_t) ((rounded.r & 0xF8) << 16) | ((rounded.g & 0xF8) << 8) | (rounded.b & 0xF8));
|
||||
|
||||
printf("Output, 15 bit color, 4 char hex, unrounded: 0x%04X\n", rgb555);
|
||||
printf("Output, 15 bit color, 4 char hex, rounded: 0x%04X\n", rgb555_rounded);
|
||||
} else {
|
||||
printf("Output, 15 bit color, 6 char hex: #%06X\n", (uint32_t) ((split.r & 0xF8) << 16) | ((split.g & 0xF8) << 8) | (split.b & 0xF8));
|
||||
printf("Output, 15 bit color, 4 char hex: 0x%04X\n", rgb555);
|
||||
}
|
||||
printf("Output, 15 bit color, 4 char hex, rounded down: 0x%04X\n", rgb555);
|
||||
printf("Output, 15 bit color, 4 char hex, rounded up: 0x%04X\n", rgb555_rounded);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue