更多颜色
回忆上次内容
- 上次 了解了 高亮颜色
- 91-97 是 高亮 前景色
- 101-107是 高亮 背景色
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_高亮](https://file.cfanz.cn/uploads/png/2023/02/08/2/29696G0Lcd.png)
- 颜色种类
- 在原来基础上 增加了一些
- 但也非常有限
- 还想要 更精细的颜色
- 有可能吗??🤔
更多颜色
- 继续深挖
- 关于 逃逸字符的文档
- 可以用 索引颜色
- 那就 是 38;5;索引数字m
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_02](https://file.cfanz.cn/uploads/png/2023/02/09/11/JW3eGZ13FE.png)
- 上图的196、213、160
- 来自于 什么 呢?
索引颜色表
- 索引数字 来自于 索引表
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_03](https://file.cfanz.cn/uploads/png/2023/02/09/11/aA0744c9Ua.png)
- 这颜色 好多啊
- 记不住 怎么办?
- 或者 可以研究一下
- 这些颜色的 规律?
规则原理
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_高亮_04](https://file.cfanz.cn/uploads/png/2023/02/09/11/P13Xa74b44.png)
- 0-7 是 标准色
- 原来的30-37
- 8-15 是 高亮色
- 原来的90-97
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_05](https://file.cfanz.cn/uploads/png/2023/02/09/11/aA0744c9Ua.png)
- 16-231 是 混合色
- r、g、b 都是从 {0,1,2,3,4,5} 的阶梯范围
- 具体的颜色数值
- 来自于
16 + 36*r + 6*g+ b
- 得到数值后 可以显示颜色
- 232-255 是 24级 灰阶色
lolcat
- 回想lolcat这个命令
- 应该是 利用了这些颜色设置
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_06](https://file.cfanz.cn/uploads/png/2022/12/31/2/71723PMC62.png)
- 有了这个
- 可以制作一个 海蓝色的海龟么 ?
海蓝色的大海龟
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_07](https://file.cfanz.cn/uploads/png/2023/02/09/11/313RP7cX95.png)
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_08](https://file.cfanz.cn/uploads/png/2023/02/09/11/2102295617.png)
- 我想让 海龟变色
- 怎么办?
循环范围
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_高亮_09](https://file.cfanz.cn/uploads/png/2023/02/09/11/aA0744c9Ua.png)
- 索引色值 递增1
- 从 16 到 231
- 现在需要 用python
- 做一个循环
- 遍历 所有的数字
修改
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_高亮_10](https://file.cfanz.cn/uploads/png/2023/02/09/11/aA0744c9Ua.png)
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_11](https://file.cfanz.cn/uploads/png/2023/02/09/11/85E51F3COF.png)
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_高亮_12](https://file.cfanz.cn/uploads/png/2023/02/09/11/2P3YL5L679.png)
- 索引数字 循环出来了
- 如何 根据索引颜色 设置颜色呢?
设置前景 为 索引颜色
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_13](https://file.cfanz.cn/uploads/png/2023/02/09/11/4I15695Rf7.png)
- "\33[38;2;xxxm"
- 设置前景使用xxx号索引色
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_14](https://file.cfanz.cn/uploads/png/2023/02/09/11/9bYFB93Qb9.png)
- 那如何设置背景色呢?
设置背景 为 索引颜色
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_15](https://file.cfanz.cn/uploads/png/2023/02/09/11/cY2Q87L409.png)
- 把原来的38改成48
- 就是设置 背景索引色 了
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_16](https://file.cfanz.cn/uploads/png/2023/02/09/11/FOVOSaAV1A.png)
- 能否 让这些字 一个个出来 呢?
- 逐个出现 肯定要用到 延迟
- 不要翻页
- 自己尝试一下
一个个出来
- 逐个出现
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_17](https://file.cfanz.cn/uploads/png/2023/02/09/11/BQE1c54cFE.png)
- 前景背景颜色有点混同
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_18](https://file.cfanz.cn/uploads/png/2023/02/09/11/A5d487E90T.png)
- 可以让 前景色和背景色
- 互为补色 吗?
设置
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_19](https://file.cfanz.cn/uploads/png/2023/02/09/11/aA0744c9Ua.png)
- 利用互补色 有 一定的规律
- 16 + 231 = 247
- 17 + 230 = 247
- 18 + 229 = 247
- 两者之和 为 247
利用规律
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_20](https://file.cfanz.cn/uploads/png/2023/02/09/11/fJCV2134M1.png)
- 运行结果
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_背景色_21](https://file.cfanz.cn/uploads/png/2023/02/09/11/L0Mc76SW1J.png)
- 这下颜色多了很多!
- 不过索引颜色
- 还是 不够细腻
- 能直接 设置 RGB 么?🤔
总结
- 这次 首先了解了 索引颜色
- \33[38;5;XXXm 设置 前景为索引色
- \33[48;5;XXXm 设置 背景为索引色
- RGB每种颜色
- 可选0-5
- 总共 6 级
![图片描述 [oeasy]python0078_设置索引颜色_index_color_ansi_控制终端颜色_python_22](https://file.cfanz.cn/uploads/png/2023/02/09/11/75aeH9UbIL.png)
- 想用 精确RGB值
- 真实地 大红色画个 大红桃心 ♥️
- 有可能吗??🤔
- 我们下次再说!👋
- 蓝桥->https://www.lanqiao.cn/courses/3584
- github->https://github.com/overmind1980/oeasy-python-tutorial
- gitee->https://gitee.com/overmind1980/oeasypython
- 视频->https://www.bilibili.com/video/BV1CU4y1Z7gQ 作者:oeasy










