Search code examples
pythoncolorsrgba

How to convert RGBA to RGB in Python, or how to get the right color code


Thanks in advance! Below is the html, and I use driver.find_element_by_xpath("//h3[@class='title-part form_title']").value_of_css_property('color') to catch the color, but the result is rgba(63,64,64,1), but my expect result is #3f3f40.

Question1: is there any way to get the correct color code by python directly?
Question2: if not, is there any way to convert the rgba(63,64,64,1) to rgb code? so that I can user'#%02x%02x%02x' % (rgb) to get the color code? Help! Help! Help!


Solution

  • You want to convert the rgba tuple into a hex value, it's pretty straight-forward and there's some code here that shows you how to achieve it: https://pythonjunkie.wordpress.com/2012/07/19/convert-hex-color-values-to-rgb-in-python/

    def rgb_to_hex(rgb): ... return '%02x%02x%02x' % rgb