Python 转换RGB颜色值的示例代码
作者:那个男人_ 时间:2022-06-26 22:02:33
题目:转换RBG颜色值
我们知道在网页中的颜色值设置都是用16进制的RGB来表示的,比如#FFFFFF,表示R:255,G:255,B:255的白色。
现在请设计一个函数可以转换RGB的16进制至10进制,或者转换10进制至16进制输出格式。
例:
print( color("#FFFFFF"))
>>>(255, 255, 255)
print( color((255,255,255))
>>> #FFFFFF
Python源码:
def color(value):
digit = list(map(str, range(10))) + list("ABCDEF")
if isinstance(value, tuple):
string = '#'
for i in value:
a1 = i // 16
a2 = i % 16
string += digit[a1] + digit[a2]
return string
elif isinstance(value, str):
a1 = digit.index(value[1]) * 16 + digit.index(value[2])
a2 = digit.index(value[3]) * 16 + digit.index(value[4])
a3 = digit.index(value[5]) * 16 + digit.index(value[6])
return (a1, a2, a3)
来源:https://blog.csdn.net/hcmdghv587/article/details/79934244
标签:Python,RGB颜色值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python人工智能学习PyTorch实现WGAN示例详解
2022-10-20 18:49:32
![](https://img.aspxhome.com/file/2023/0/95790_0s.png)
Python字体反爬实战案例分享
2021-06-18 01:00:46
![](https://img.aspxhome.com/file/2023/9/75929_0s.png)
mac下给python3安装requests库和scrapy库的实例
2023-11-06 09:44:52
PHP使用自定义key实现对数据加密解密的方法
2023-08-21 12:48:37
Keras框架中的epoch、bacth、batch size、iteration使用介绍
2023-10-13 13:15:13
asp如何从数据库中删除废旧的电子信箱地址?
2009-11-15 20:04:00
Python的进程,线程和协程实例详解
2021-05-05 04:35:59
![](https://img.aspxhome.com/file/2023/9/95559_0s.png)
游戏的用户体验营销小札
2009-08-30 15:13:00
盘点十个超级好用的高级Python脚本
2023-12-04 23:41:27
![](https://img.aspxhome.com/file/2023/0/69630_0s.png)
Python爬取知乎图片代码实现解析
2023-02-22 08:14:14
![](https://img.aspxhome.com/file/2023/7/81777_0s.jpg)
Python对列表中的各项进行关联详解
2023-10-26 20:19:34
![](https://img.aspxhome.com/file/2023/0/97250_0s.png)
PHP中Too few arguments to function的问题及解决
2023-06-04 21:15:55
![](https://img.aspxhome.com/file/2023/3/55303_0s.png)
谈谈XHTML中CDATA
2007-09-17 12:45:00
asp 延时 页面延迟的三种方法
2011-03-31 11:04:00
使用python将多个excel文件合并到同一个文件的方法
2023-01-14 18:53:42
![](https://img.aspxhome.com/file/2023/9/81819_0s.jpg)
PS中执行N遍选定动作的脚本
2008-02-22 21:33:00
![](https://img.aspxhome.com/file/UploadPic/20082/22/2008222213917983s.gif)
PHP5中使用DOM控制XML实现代码
2023-09-30 09:00:07
PHP 二维数组根据某个字段排序的具体实现
2023-11-20 05:21:13
段正淳的css笔记(3)标题右侧“更多”的实现
2007-11-01 21:55:00
![](https://img.aspxhome.com/file/UploadPic/200711/1/200711122344717s.gif)
ASP程序中使用断开的数据记录集的代码
2012-12-04 20:20:28