Skip to content

image converter does not do proper rgb_to_color565 conversion #17

@Troyhy

Description

@Troyhy

Hi, I was using this image converter and noticed that with test picture like this
test
color palette does not have pure white 0xFFFF but it has 0xFFF8. Question is that is this somehow intentional or a bug?
Here is the converter function and my proposed fix:

r=g=b=255
a = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b & 0xF8)
#fixed
b = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
print(f'{a:016b} != {b:016b}\n{a:2x} != {b:2x}')

# 1111111111111000 != 1111111111111111
# fff8 != ffff

def rgb_to_color565(r, g, b):
"""
Convert RGB color to the 16-bit color format (565).
Args:
r (int): Red component of the RGB color (0-255).
g (int): Green component of the RGB color (0-255).
b (int): Blue component of the RGB color (0-255).
Returns:
int: Converted color value in the 16-bit color format (565).
"""
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b & 0xF8)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions