udoprog/c10t

[patch] Text renders in "random" colors

Closed this issue · 0 comments

Text was rendering in really strange colors. I tracked it down to a bug in text.cpp, where a 0-1 float was being directly assigned from a 0-255 unit8:

uint8_t* buffer = bitmap->buffer;
.......
    for (pos_t y = 0; y < s_bitmap_rows && y < image->get_height() + pen_y; y++) {
      for (pos_t x = 0; x < s_bitmap_width && x < image->get_width() + pen_x; x++) {
        color c(base);
        c.a = buffer[x + y * bitmap->width]; //THIS LINE
        image->safe_blend_pixel(pen_x + x, pen_y + y, c);

Converting the value using the color_i_to_f array resolves the issue:

        c.a = color_i_to_f[buffer[x + y * bitmap->width]];

DIFF:

diff --git a/src/text.cpp b/src/text.cpp
index 77fbc7f..42f6773 100644
--- a/src/text.cpp
+++ b/src/text.cpp
@@ -60,7 +60,7 @@ namespace text {
     for (pos_t y = 0; y < s_bitmap_rows && y < image->get_height() + pen_y; y++) {
       for (pos_t x = 0; x < s_bitmap_width && x < image->get_width() + pen_x; x++) {
         color c(base);
-        c.a = buffer[x + y * bitmap->width];
+        c.a = color_i_to_f[buffer[x + y * bitmap->width]];
         image->safe_blend_pixel(pen_x + x, pen_y + y, c);
       }
     }