美文网首页
python下rgb转hex

python下rgb转hex

作者: neao_anderson | 来源:发表于2018-03-21 20:44 被阅读0次
    
    def toHex(r, g, b):
    
        color = "#"
    
        color += str(hex(r)).replace('x','0')[-2:]
    
        color += str(hex(g)).replace('x','0')[-2:]
    
        color += str(hex(b)).replace('x','0')[-2:]
    
        return color
    
    

    注意replace('x','0')的用途,把int转成hex后,显示的0x*,0到15取值后2位后会成为x,所以把x替换成0后,取值为0,就没问题。

    相关文章

      网友评论

          本文标题:python下rgb转hex

          本文链接:https://www.haomeiwen.com/subject/cgynqftx.html