diff --git a/tools/gbcpal.c b/tools/gbcpal.c index b5cdeea8b00..e5e68284d17 100644 --- a/tools/gbcpal.c +++ b/tools/gbcpal.c @@ -29,6 +29,9 @@ struct Color { uint8_t r, g, b; }; +const struct Color BLACK = {0, 0, 0}; +const struct Color WHITE = {31, 31, 31}; + uint16_t pack_color(struct Color color) { return (color.b << 10) | (color.g << 5) | color.r; } @@ -48,7 +51,8 @@ double luminance(struct Color color) { int compare_colors(const void *color1, const void *color2) { double lum1 = luminance(*(const struct Color *)color1); double lum2 = luminance(*(const struct Color *)color2); - return (lum1 < lum2) - (lum1 > lum2); // sort lightest to darkest + // sort lightest to darkest, or darkest to lightest if reversed + return reverse ? (lum1 > lum2) - (lum1 < lum2) : (lum1 < lum2) - (lum1 > lum2); } void read_gbcpal(const char *filename, struct Color **colors, size_t *num_colors) { @@ -76,18 +80,19 @@ void filter_colors(struct Color **colors, size_t *num_colors) { size_t num_filtered = 0; struct Color *filtered = xmalloc((sizeof **colors) * *num_colors); + // filter out black, white, and duplicate colors for (size_t i = 0; i < *num_colors; i++) { struct Color color = (*colors)[i]; - if (color.r == 0 && color.g == 0 && color.b == 0) { - continue; // filter out black + if (color.r == BLACK.r && color.g == BLACK.g && color.b == BLACK.b) { + continue; } - if (color.r == 31 && color.g == 31 && color.b == 31) { - continue; // filter out white + if (color.r == WHITE.r && color.g == WHITE.g && color.b == WHITE.b) { + continue; } if (num_filtered > 0) { struct Color last = filtered[num_filtered - 1]; if (color.r == last.r && color.g == last.g && color.b == last.b) { - continue; // filter out duplicates + continue; } } filtered[num_filtered++] = color; @@ -118,34 +123,22 @@ int main(int argc, char *argv[]) { qsort(colors, num_colors, sizeof(*colors), compare_colors); filter_colors(&colors, &num_colors); - uint16_t first, second; - if (num_colors == 0) { - // colors are white and black - first = 0x7fff; - second = 0x0000; - } else if (num_colors == 1) { - // colors are both the same one - first = pack_color(colors[0]); - second = first; - } else if (num_colors == 2) { - // colors are light and dark - first = pack_color(colors[0]); - second = pack_color(colors[1]); - if (reverse) { - // darker color is first - uint16_t tmp = first; - first = second; - second = tmp; - } - } else { + if (num_colors > 2) { error_exit("%s: more than 2 colors besides black and white (%zu)\n", out_filename, num_colors); } + uint16_t packed_colors[4] = { + pack_color(WHITE), + pack_color(num_colors > 0 ? colors[0] : WHITE), + pack_color(num_colors > 1 ? colors[1] : num_colors > 0 ? colors[0] : BLACK), + pack_color(BLACK), + }; + uint8_t bytes[8] = { - 0xff, 0x7f, // white - first & 0xff, first >> 8, - second & 0xff, second >> 8, - 0x00, 0x00, // black + packed_colors[0] & 0xff, packed_colors[0] >> 8, + packed_colors[1] & 0xff, packed_colors[1] >> 8, + packed_colors[2] & 0xff, packed_colors[2] >> 8, + packed_colors[3] & 0xff, packed_colors[3] >> 8, }; write_u8(out_filename, bytes, COUNTOF(bytes));