46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
""" Pixel converter utilities
|
|
|
|
This file expose many 32 bits RGBA into various pixel format
|
|
"""
|
|
|
|
__all__ = [
|
|
'rgb1conv',
|
|
'rgb8conv',
|
|
'rgba8conv',
|
|
'rgb16conv',
|
|
'rgba16conv'
|
|
]
|
|
|
|
def rgb24to16(rgb):
|
|
_r = (rgb[0] & 0xff) >> 3
|
|
_g = (rgb[1] & 0xff) >> 2
|
|
_b = (rgb[2] & 0xff) >> 3
|
|
return (_r << 11) | (_g << 5) | _b
|
|
|
|
|
|
|
|
def rgb1conv(pixel):
|
|
return pixel == (0, 0, 0)
|
|
|
|
def rgb8conv(pixel):
|
|
return int((pixel[0] * 7) / 255) << 5 \
|
|
| int((pixel[1] * 4) / 255) << 3 \
|
|
| int((pixel[2] * 7) / 255) << 0
|
|
|
|
def rgba8conv(pixel):
|
|
return int((pixel[0] * 4) / 256) << 6 \
|
|
| int((pixel[1] * 8) / 256) << 3 \
|
|
| int((pixel[2] * 4) / 256) << 1 \
|
|
| (len(pixel) >= 4 and pixel[3] == 0)
|
|
|
|
def rgb16conv(pixel):
|
|
return int((pixel[0] * 31) / 255) << 11 \
|
|
| int((pixel[1] * 63) / 255) << 5 \
|
|
| int((pixel[2] * 31) / 255) << 0
|
|
|
|
def rgba16conv(pixel):
|
|
return int((pixel[0] * 31) / 255) << 11 \
|
|
| int((pixel[1] * 63) / 255) << 6 \
|
|
| int((pixel[2] * 31) / 255) << 1 \
|
|
| (pixel[3] != 0)
|