0%

RGB888、RGB555、RGB565之间转换

三种RGB格式表示方式

1
2
3
RGB555: R-5bit,G-5bit,B-5bit
RGB565: R-5bit,G-6bit,B-5bit
RGB888: R-8bit,G-8bit,B-8bit

RGB888转RGB555

RGB888 : R7 R6 R5 R4 R3 R2 R1 R0 G7 G6 G5 G4 G3 G2 G1 G0 B7 B6 B5 B4 B3 B2 B1 B0

RGB555 : 0 R7 R6 R5 R4 R3 G7 G6 G5 G4 G3 B7 B6 B5 B4 B3

1
2
3
4
5
6
7
8
9
10
11
func rgb888ToRgb555(color uint32) uint16 {
r := (color >> 16) & 0xff
g := (color >> 8) & 0xff
b := color & 0xff

r >>= 3
g >>= 3
b >>= 3

return uint16(r<<(10) | g<<5 | b)
}

RGB555转RGB888

RGB555 : 0 R4 R3 R2 R1 R0 G4 G3 G2 G1 G0 B4 B3 B2 B1 B0

RGB888 : R4 R3 R2 R1 R0 0 0 0 G4 G3 G2 G1 G0 0 0 0 B4 B3 B2 B1 B0 0 0 0

1
2
3
4
5
6
7
8
9
10
11
func rgb555ToRgb888(color uint16) uint32 {
r := (color >> 10) & 0x1f
g := (color >> 5) & 0x1f
b := color & 0x1f

R := uint32(r) << 3
G := uint32(g) << 3
B := uint32(b) << 3

return 0xff000000 | R<<16 | G<<8 | B
}

RGB888转RGB565

RGB888 : R7 R6 R5 R4 R3 R2 R1 R0 G7 G6 G5 G4 G3 G2 G1 G0 B7 B6 B5 B4 B3 B2 B1 B0

RGB5G5 : R7 R6 R5 R4 R3 G7 G6 G5 G4 G3 G2 B7 B6 B5 B4 B3

1
2
3
4
5
6
7
8
9
10
11
func rgb888ToRgb565(color uint32) uint16 {
r := (color >> 16) & 0xff
g := (color >> 8) & 0xff
b := color & 0xff

r >>= 3
g >>= 2
b >>= 3

return uint16(r<<(11) | g<<5 | b)
}

RGB565转RGB888

RGB565 : R4 R3 R2 R1 R0 G5 G4 G3 G2 G1 G0 B4 B3 B2 B1 B0

RGB888 : R4 R3 R2 R1 R0 0 0 0 G5 G4 G3 G2 G1 G0 0 0 0 B4 B3 B2 B1 B0 0 0 0

1
2
3
4
5
6
7
8
9
10
11
func rgb565ToRgb888(color uint16) uint32 {
r := (color >> 11) & 0x1f
g := (color >> 5) & 0x3f
b := color & 0x1f

R := uint32(r) << 3
G := uint32(g) << 2
B := uint32(b) << 3

return 0xff000000 | R<<16 | G<<8 | B
}