Skip to content

Instantly share code, notes, and snippets.

@JamesHagerman
Forked from ajfisher/colour_converter.py
Created June 13, 2018 08:51
Show Gist options
  • Save JamesHagerman/ad641b1c8bcf2493dc39811492239f20 to your computer and use it in GitHub Desktop.
Save JamesHagerman/ad641b1c8bcf2493dc39811492239f20 to your computer and use it in GitHub Desktop.
Convert an 888 RGB value to 565 hex
# Author: Andrew Fisher
# Version: 0.1
# Date: 30/05/2012
# Licence: BSD
# This python converts an rgb value expressed in 8 bit values (eg 255, 128, 0) and returns a hex value that
# has been converted to 565 format. This was highly useful when I was playing with some Arduino
# stuff requiring 16 bit LCD designs and couldn't remember the hex layout.
def rgb_hex565(red, green, blue):
# take in the red, green and blue values (0-255) as 8 bit values and then combine
# and shift them to make them a 16 bit hex value in 565 format.
print ("0x%0.4X" % ((int(red / 255 * 31) << 11) | (int(green / 255 * 63) << 5) | (int(blue / 255 * 31))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment