Home  >  Article  >  Backend Development  >  Introduction to a small tool for color value conversion using Python

Introduction to a small tool for color value conversion using Python

高洛峰
高洛峰Original
2017-03-23 17:09:041666browse

 Requirement Description
The company’s UI design guy has switched to Zeplin for a long time. The color values ​​of Zeplin's design draft display page are represented by decimal RGB. In most cases, color representation in Android requires hexadecimal RGB representation. My math skills are not so good that I can mentally calculate the hexadecimal result by directly seeing the decimal system, so I need a tool to input the decimal RGB and get the hexadecimal color value. It is best to be able to easily copy.
Introduction to a small tool for color value conversion using Python
Zeplin's color value display example
 Original processing method
Because I know Python (limited to python input in the terminal and then use it as a calculator to calculate, or use The hex() function converts decimal to hexadecimal), so when I encountered such a problem, of course I used python's hex() function to do the conversion, and then manually input the results into Android Studio.
Introduction to a small tool for color value conversion using Python
Using hex function to manually convert color values
 Motivation
People are always too lazy. I have wanted to write this small tool for a long time. I have also tried it. The idea is:
Input: RGB-like decimal value (110, 122 138), separated by spaces or commas.
Output: A hexadecimal RGB color value (#6e7a8a).
But I haven’t done anything yet, I’ve been paying attention. So lazy!
 Let’s get started
 1. First I need to input the function
I opened the folder where I learned Python before, and there happened to be an example of raw_input in it:
Python code

#!/usr/bin/python 
#coding=utf-8 
 
raw_input("\n\n等输入")


After executing python input.py in the terminal, you can enter text.
I need to receive information entered by the user. I forgot how to receive it, Google it, get the result, change the input prompt and print out the input content:
Python code

input = raw_input("\n输入颜色 比如50 144 60:\n") 
print(input)


 2. Need to separate characters
Query the Python character splitting function split(). By default, you can split by whitespace without passing in parameters. It was originally said to use an English comma (,) as the separator, but now it seems that it can be omitted. It can be separated directly with spaces. No matter how many spaces there are, it can be automatically separated. So I added the code:
Python code

rgbColorArray = input.split() 
print(rgbColorArray)


 3. Need to traverse the array
I have forgotten how to do a simple traversal of the array. Search the same:
Python code

for x in rgbColorArray: print(x)


 4. Convert characters to hexadecimal
At this time, we get the string and turn it into a hexadecimal string . At this time, two functions are needed, int() and hex(). The int function can convert the string into the int type, while hex accepts numeric parameters and returns a string. A string starting with 0x.
So there is a version.
So there was the first version.
The first version
Introduction to a small tool for color value conversion using Python
The first version
Introduction to a small tool for color value conversion using Python
The first version execution result
After writing such a basic version, I can basically get what I want I got the desired result, but the disadvantage is that I still need to input it manually, use my brain to memorize the hexadecimal color value and then enter it. I hope I can directly copy the final result.
Going further
Although the results have come out, I still hope to make some progress. There are several questions:
1. When the number to be converted is less than 16, there is only one digit. Not displayed, for example, the displayed result of 11 is 0xB
 2. The actual result is 0x
 3. It is best to display the results together for easy copying, rather than one line for each color.
Then you need to traverse the color value array, remove the 0x string, and add 0 in front if it is judged to be less than 16. Output the results together continuously.
 for loop traverses the array
I used the for loop before, which is from the example found, but there are too many lines and I don’t know how to do it. If you write too much in Java, it is usually enclosed in { } curly brackets.
Continue to check the information, and then I know that it is roughly used like the following.
Python code

#!/usr/bin/python 
# -*- coding: UTF-8 -*- 
 
for num in range(10,20): # 迭代 10 到 20 之间的数字 
 for i in range(2,num): # 根据因子迭代 
  if num%i == 0:  # 确定第一个因子 
   j=num/i   # 计算第二个因子 
   print '%d 等于 %d * %d' % (num,i,j) 
   break   # 跳出当前循环 
 else:     # 循环的 else 部分 
  print num, '是一个质数'


  变量声明
  由于需要不换行,所以就需要字符连接,而不是直接 print。
  声明变量又遇到问题了。根据前面的变量使用情况,找了些 python 代码看了看,大概知道不用声明什么类型,直接用就好了。于是有了代码:
Python代码

output = "#" 
for x in rgbColorArray: 
 intx = int(x) 
 output = output + hex(intx) 
print(output)


  字符串裁剪和拼接
  需要把多余的0x 两位去掉。
  用到字符串裁剪,依然寻找范例。
Python代码

#!/usr/bin/python 
 
var1 = 'Hello World!' 
var2 = "Python Runoob" 
 
print "var1[0]: ", var1[0] 
print "var2[1:5]: ", var2[1:5]


  这个范例的执行结果:
Python代码

var1[0]: H 
var2[1:5]: ytho


  顺便问了旁边也在学习 python 的同事,他告诉我后面的索引可以省略,代表直接裁剪到结尾。
  比如上面的例子如果 print "var2[1:]", var2[1:] 得到的结果应该是 ython Runoob
  所以有代码:
Python代码

output = "#" 
for x in rgbColorArray: 
 intx = int(x) 
 output = output + hex(intx)[2:] 
print(output)


  也可以从后往前数,比如还是上面的范例可以写成。比如上面的例子如果 print "var2[-1:]", var2[-1:] 得到的结果应该是 ob 也就是字符串的后两位。
  于是我们这里可以写成hex(intx)[-2:] (因为输出字符串类似是0x23, 这样的)就是这个导致我后面写了个 bug,我也文章最后说明这个 bug 是什么。
  if else 判断
  接着要做一个判断,给一位的补上0
Python代码

if intx <p style="text-align: left;"><br>  这样就有了python 文件:<br>Python代码</p><pre class="brush:php;toolbar:false">#!/usr/bin/python 
#coding=utf-8 
input = raw_input("\n输入颜色 比如50 144 60:\n") 
#print(input) 
 rgbColorArray = input.split() 
print(rgbColorArray) 
output = "#" 
for x in rgbColorArray: 
 intx = int(x) 
 if intx <p style="text-align: left;"><br>  还有最后一步:把 ColorU 加入到环境变量中<br>  这个时候我可以得到我要的记过了,但是有点不太方便,我需要到这个 python 文件所在的目录下写<br>Python代码</p><pre class="brush:php;toolbar:false">python colorU.py


  或者写全 colorU.py 这个路径。都是很麻的事情,所以我需要把 colorU 加入环境变量中。我用的是 zsh,所以找到环境变量的配置文件:~/.zshrc,末尾加上配置:
Python代码

alias colorU="python ~/Documents/Development/PythonStudy/colorU.py"


  这个是经过另外以为同事指导后的最后可行版本,我最初的思路是把 colorU.py 文件设置成可执行文件,然后加入到 Path 当中。结果我把 colorU.py 这个文件的地址加入到了 Path 中,世界上PATH 应该是一个目录。这样添加别名的方式更方便。
也就是说如果我装客户端就可以不用我写的这个脚本了。但没关系我学习了 python,写了我自己的第一个真正有用的 python 代码。
  2. 一个bug:我是在写这篇文章的时候才发现这个 bug 的,类似0x33这样的字符串从后往前裁剪的时候写[-2:],当然没有问题,但是写0xf 这样的字符串就会有问题了。程序输入 5 5 5得到的结果是 #0x50x50x5。修改成[2:] 的裁剪就可以了。
Introduction to a small tool for color value conversion using Python
倒向裁剪字符串引起的bug
  还可以继续升级体验:
  a. 直接在终端中输入 colorU 231 234 123 就可以得到结果 #e7ea7b;
  b. 配合Alfred, 呼出 Alfred 窗口后,输入色值,得到结果,回车直接复制十六进制到粘贴板。
  c. 保存之前已经转换过的色值,方便重复使用的颜色,直接复制十六进制颜色。

The above is the detailed content of Introduction to a small tool for color value conversion using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn