• RGB888转换为RGB565原理及工具


    1.原理
    首先RGB的范围是0-255,包括RGB三个值。

    第一步将R,G,B三个值分别转化为八位二进制数,这里以GREEN为例子。
    GREEN R:0 G:255 B:0
    二进制:R00000000 11111111 00000000

    565的意思就是RGB的位数,R取5位,G取六位,B取5位。其中要求取高位,舍低位。

    00000 000 11111111 00000 00

    其中非粗体的 0和1 舍去

    得到0000011111100000
    再转化为十六进制数0x7e0

    2.工具

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
     
    from tkinter import *
    import re
     
    window = Tk()
    window.title("RGB888 转换 RGB565")
    window.geometry('450x300+300+250')
     
    def scalecommand(color):
        
        #print(s1.get(),s2.get(),s3.get())
        b=s3.get()
        g=s2.get()
        r=s1.get()
     
        R = r & 0xF8
        G = g & 0xFc
        B = b & 0xF8
     
        rgb565 = (R << 8) | (G << 3) | (B >> 3)
     
        
        rgb888 = (r << 16) | (g << 8) | b
        R = R >> 3
        G = G >> 2
        B = B >> 3
        rgb888_text = 'RGB888: '+ "#%06x"%rgb888
        rgb888_Label.configure(text=rgb888_text)
        
        rgb565_text = '565: '+ "%d " %R + "%d " %G + "%d" %B
        rgb565_Label.configure(text=rgb565_text)
        
        #print("%#08X"%rgb)
        info_Label.configure(bg="#%06x" %rgb888)
        
        
     
    def HexToDec(value):
      try:
        return int(value, 16)
      except ValueError:
        return "Invalid Hexadecimal Value"
     
     
    def buttonClick1():
        # RGB888 转 RGB565
        
        try:
            c888 = int(rgb1_Entry.get(),16)         #字符转16进制整数
        except ValueError:
            print( "Invalid Hexadecimal Value")
            return
        
        rgb2_Entry.delete(0,END)
        
        if c888 == None :
            return
        else:
            
            b = (c888 & 0xFF)                 #转换RB 取得rgb颜色B
            g = int((c888 & 0xFF00) >> 8)         #转换G 取得rgb颜色G
            r = int((c888 & 0xFF0000) >>16)     #转换R 取得rgb颜色R
            R = r & 0xF8                        #取得RGB565的5位R
            G = g & 0xFc                        #取得RGB565的6位G
            B = b & 0xF8                        #取得RGB565的5位B
     
            rgb565 = (R << 8) | (G << 3) | (B >> 3)                               
            #print("%#06x" %rgb565)               
            #设置滑块位置
            s1.set(r)               
            s2.set(g)
            s3.set(b)
    
            #显示RGB888和RGB565颜色码
            info_Label.configure(bg="#%06x" %c888)      
            rgb888_text = 'RGB888: '+ "#%06x"%c888
            rgb888_Label.configure(text=rgb888_text)
            rgb565_text = 'RGB565: '+ "%#06x" %rgb565
            rgb565_Label.configure(text=rgb565_text)
     
     
     
    def buttonClick2():
        # RGB565 转 RGB888
        rgb1_Entry.delete(0,END)
        
        try:
            c565 = int(rgb2_Entry.get(),16)
        except ValueError:
            print( "Invalid Hexadecimal Value")
            return
        
        if c565 == None :
            return
        else:
            b = (c565 & 0x001F)                 #转换R 
            g = int((c565 & 0x07E0))           #转换G 
            r = int((c565 & 0xF800))          #转换B
            R = r >> 8
            G = g >> 3
            B = b << 3
     
            rgb888 = (R << 16) | (G << 8) | B        
     
            #print("%#06x" %rgb888)
            
            s1.set(R)
            s2.set(G)
            s3.set(B)
            info_Label.configure(bg="#%06x" %rgb888)
            rgb888_text = 'RGB888: '+ "#%06x"%rgb888
            rgb888_Label.configure(text=rgb888_text)
            rgb565_text = 'RGB565: '+ "%#06x" %c565
            rgb565_Label.configure(text=rgb565_text)
        
     
    Rgb1_Label = Label(window, text="RGB888 代码:",height = 2,fg='#191970')
    Rgb1_Label.place( x =20, y = 25 , anchor=NW)
     
    R1_Label = Label(window, text="#",height = 2,fg='#191970')
    R1_Label.place( x =20, y = 50 , anchor=NW)
    rgb1_Entry = Entry(window,width=10)
    rgb1_Entry.place( x =40, y = 60 , anchor=NW)
     
    Rgb2_Label = Label(window, text="RGB565 代码:",height = 2,fg='#191970')
    Rgb2_Label.place( x =20, y = 85 , anchor=NW)
     
    R2_Label = Label(window, text="0x",height = 2,fg='#191970')
    R2_Label.place( x =20, y = 110 , anchor=NW)
    rgb2_Entry = Entry(window,width=10)
    rgb2_Entry.place( x =40, y = 120 , anchor=NW)
     
     
    button1 = Button(window,text="转换", bg='#8FBC8F',command=buttonClick1)    #转换按键
    button1.place( x =160, y = 40 , anchor=NW)
     
    button2 = Button(window,text="转换", bg='#8FBC8F',command=buttonClick2)    #转换按键
    button2.place( x =160, y = 110 , anchor=NW)
     
    info_Label = Label(window, text="",height = 10,width=20)        #色块
    info_Label.configure(bg='#FFFFFF')
    info_Label.place( x =280, y = 20 , anchor=NW)
     
    R_Label = Label(window, text="R",height = 1,width=1)       
    R_Label.place( x =20, y = 170 , anchor=NW)
    s1 = Scale(window,  from_=0, to=255, orient=HORIZONTAL,
                 length=200, showvalue=1, tickinterval=0, resolution=1, command=scalecommand)     #滑块R
    s1.place( x =40, y = 150 , anchor=NW)
     
    G_Label = Label(window, text="G",height = 1,width=1)       
    G_Label.place( x =20, y = 210 , anchor=NW)
    s2 = Scale(window,  from_=0, to=255, orient=HORIZONTAL,
                 length=200, showvalue=1, tickinterval=0, resolution=1, command=scalecommand)   #滑块G
    s2.place( x =40, y = 190 , anchor=NW)
     
    B_Label = Label(window, text="B",height = 1,width=1)        
    B_Label.place( x =20, y = 250 , anchor=NW)
    s3 =  Scale(window, from_=0, to=255, orient=HORIZONTAL,
                 length=200, showvalue=1, tickinterval=0, resolution=1, command=scalecommand)   #滑块B
    s3.place( x =40, y = 230 , anchor=NW)
     
    rgb888_Label = Label(window, text="RGB888:",height = 1,width=18 , fg = 'blue',anchor="w")
    rgb888_Label.place( x =280, y = 210 )
     
    rgb565_Label = Label(window, text="565:",height = 1,width=18 , fg = 'blue',anchor="w")  
    rgb565_Label.place( x =280, y = 250 ) 
     
     
    window.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171

    转自 https://blog.csdn.net/wild_lee/article/details/122009234
    修改了一下将RGB565的R G B 分别用十进制打出

    一些常用的颜色
    RGB888和RGB565颜色对照表

    RGB颜色值与十六进制颜色码互转

    RGB在线颜色生成调色板

    色彩中间色/颜色中间值计算

  • 相关阅读:
    UNIX环境高级编程_文件IO_文件描述符
    第四站:数组
    计算机网络TCP篇之流量控制
    【如何使用vscode用户代码】
    THM学习笔记—Simple CTF
    【lwip】09-IPv4协议&超全源码实现分析
    FreeRTOS的学习(五)—— 信号量之计数信号量
    Linux中修改环境变量的几种方法比较分析
    记一次 .NET 某设备监控系统 死锁分析
    探索 VisionOS 辅助功能:VoiceOver 手势
  • 原文地址:https://blog.csdn.net/qq_44710568/article/details/126118594