gtk3 +和python rgba转换为十六进制 [英] gtk3+ and python rgba convert to hex

查看:149
本文介绍了gtk3 +和python rgba转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i使用gtk3我发现它使用rgba表示颜色,但是(红色,绿色,蓝色,alpha)不是 0-255 之间的整数,而是浮动的点号在 0-1.0 之间,所以我不知道如何从rgba转换为十六进制,反之亦然

i using gtk3 i found that it use rgba for representing color, but the (red,green,blue,alpha) are not integer between 0-255 but floating point number between 0-1.0, so i don't know how to convert from rgba to hex and vice-versa

我已经尝试过此代码,但是它似乎不起作用:

i have tried this code but its seem to not work :

def convert_to_hex(rgba_color) :
red = str(hex(int(rgba_color.red*255)))[2:].capitalize()
green = str(hex(int(rgba_color.green*255)))[2:].capitalize()
blue = str(hex(int(rgba_color.blue*255)))[2:].capitalize()

return '0x' + red + green + blue

推荐答案

假设问题在于,数字只有1位数时,应该有前导零.这是一个解决方案.

Assuming the problem is that the number should have leading zeros when they are only 1 digit. Here is a solution for that.

def convert_to_hex(rgba_color) :
    red = int(rgba_color.red*255)
    green = int(rgba_color.green*255)
    blue = int(rgba_color.blue*255)
    return '0x{r:02x}{g:02x}{b:02x}'.format(r=red,g=green,b=blue)

这篇关于gtk3 +和python rgba转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆