当前位置:   article > 正文

RGB888颜色码与十六位(RGB565)颜色码的转换

rgb565

    最近弄了个ARDUINO开发板玩,发现RGB565的颜色代码转换很不习惯,查了不少资料,发现在我们的计算机中图像是以RGB888格式显示图像的,24位图每个像素保存了32bit的数据,即RGB888+Alpha,Alpha就是半透明填充字节。

  但是在很多单板机里面是使用RGB565显示图像的,网上很多调色板工具和程序都是用RGB888格式,很少有RGB565的。

  因此,自己弄了个简单的RGB565和RGB888颜色代码转换的工具。

基本原理:

一.RGB888->RGB565

方法只要提取相应单色高位即可(R5 G6 B5),但会导致低位的缺失,影响精度,而且无法恢复。

二.RGB565->RGB888

方法只要补充相应单色低位即可(R3 G2 B3)。在图像转换中会使用一些算法来补充,这里不涉及图像转换,仅颜色码的转换。

RGB888用unsigned int 32位字节存储

  0  0  0  0  0  0  0  0R7R6R5R4R3R2R1R0G7G6G5G4G3G2G1G0B7B6B5B4B3B2B1B0

RGB565用unsigned short 16位字节存储
R7R6R5R4R3G7G6G5G4G3G2B7B6B5B4B3

程序实现:

基本的tkinter使用,python 3.7

核心程序就是通过位移取得RGB565的R,G,B三色值。简单的使用Label显示色块,三个颜色的滑块可以自由拉动。

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/468891
推荐阅读
相关标签
  

闽ICP备14008679号