将 RGB 颜色元组转换为六位代码 [英] Converting a RGB color tuple to a six digit code

查看:180
本文介绍了将 RGB 颜色元组转换为六位代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 (0, 128, 64) 转换为类似这样的 #008040".我不知道如何称呼后者,使搜索变得困难.

解决方案

使用格式运算符 %:

<预><代码>>>>'#%02x%02x%02x' % (0, 128, 64)'#008040'

请注意,它不会检查边界...

<预><代码>>>>'#%02x%02x%02x' % (0, -1, 9999)'#00-1270f'

I need to convert (0, 128, 64) to something like this "#008040". I'm not sure what to call the latter, making searching difficult.

解决方案

Use the format operator %:

>>> '#%02x%02x%02x' % (0, 128, 64)
'#008040'

Note that it won't check bounds...

>>> '#%02x%02x%02x' % (0, -1, 9999)
'#00-1270f'

这篇关于将 RGB 颜色元组转换为六位代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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