Skip to content

Commit 223b3d1

Browse files
authored
More accurate 8-bit <=> 5-bit RGB color conversion (#1827)
1 parent 65d408e commit 223b3d1

20 files changed

+31
-15
lines changed

include/gfx/rgba.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ struct Rgba {
1818
: red(rgba >> 24), green(rgba >> 16), blue(rgba >> 8), alpha(rgba) {}
1919

2020
static constexpr Rgba fromCGBColor(uint16_t color) {
21-
constexpr auto _5to8 = [](uint8_t channel) -> uint8_t {
22-
channel &= 0b11111; // For caller's convenience
23-
return channel << 3 | channel >> 2;
24-
};
21+
constexpr auto _5to8 = [](uint8_t c) -> uint8_t { return ((c & 0b11111) * 255 + 15) / 31; };
2522
return {
2623
_5to8(color),
2724
_5to8(color >> 5),

man/rgbasm-old.5

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,24 @@ Previously we had
461461
.Pp
462462
Instead, now we have
463463
.Ql p ** q ** r == p ** (q ** r) .
464+
.Ss 8-bit and 5-bit color conversion
465+
Changed in 1.0.0.
466+
.Pp
467+
RGBGFX takes 8-bit RGB colors as its PNG input, and outputs 5-bit GBC colors.
468+
Its
469+
.Ql -r/--reverse
470+
mode does the opposite 5-bit to 8-bit conversion.
471+
Instead of the previous inaccurate conversions, we now do accurate rounding to the nearest equivalent.
472+
.Pp
473+
Previously to convert an 8-bit color channel to 5-bit, we truncated it as
474+
.Ql c >> 3 ;
475+
and to reverse a 5-bit color channel to 8-bit, we extended it as
476+
.Ql (c << 3) | (c >> 2) .
477+
.Pp
478+
Instead, now we round 8-bit to 5-bit as
479+
.Ql (c * 31 + 127) / 255 ,
480+
and round 5-bit to 8-bit as
481+
.Ql (c * 255 + 15) / 31 .
464482
.Sh BUGS
465483
These are misfeatures that may have been possible by mistake.
466484
They do not get deprecated, just fixed.

src/gfx/rgba.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ uint16_t Rgba::cgbColor() const {
5050
g = reverse_curve[g];
5151
b = reverse_curve[b];
5252
} else {
53-
r >>= 3;
54-
g >>= 3;
55-
b >>= 3;
53+
constexpr auto _8to5 = [](uint8_t c) -> uint8_t { return (c * 31 + 127) / 255; };
54+
r = _8to5(r);
55+
g = _8to5(g);
56+
b = _8to5(b);
5657
}
5758
return r | g << 5 | b << 10;
5859
}

test/gfx/alpha_embedded.err

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Failed to fit tile colors [$1527, $15cc, $1ab4] in specified palettes
1+
error: Failed to fit tile colors [$1527, $15cc, $1a93] in specified palettes
22
note: The following palette was specified:
3-
[$1ab4, $15cc]
3+
[$1a93, $15cc]
44
Conversion aborted after 1 error

test/gfx/at-file-ref.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Failed to fit tile colors [$6c8a, $7f55, $7fff] in specified palettes
1+
error: Failed to fit tile colors [$688b, $7f55, $7fff] in specified palettes
22
note: The following palettes were specified:
33
[$5f77, $213d, $41a6, $40ee]
44
[$3f65, $36b3, $262a, $50b0]

test/gfx/bad_manual_pals.err

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error: Failed to fit tile colors [$6c8a, $7f55, $7fff] in specified palettes
1+
error: Failed to fit tile colors [$688b, $7f55, $7fff] in specified palettes
22
note: The following palettes were specified:
33
[$7fff, $7f55]
4-
[$7fff, $6c8a]
4+
[$7fff, $688b]
55
Conversion aborted after 1 error

test/gfx/base_ids.out.pal

0 Bytes
Binary file not shown.

test/gfx/bg_fuse.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
warning: Fusing colors #a9b9c9ff and #aabbccff into Game Boy color $66f5 [first seen at x: 1, y: 1]
1+
warning: Fusing colors #a9bacbff and #aabbccff into Game Boy color $66f5 [first seen at x: 1, y: 1]
22
FATAL: Tile (0, 0) contains the background color (#aabbccff)!
33
Conversion aborted after 1 error

test/gfx/bg_fuse.png

-3 Bytes
Loading

test/gfx/empty_lines.out.pal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
lU���
1+
hU���

0 commit comments

Comments
 (0)