First look at the Zeplin color value display sample
Original processing mode
Because I would python (only the terminal input Python and then use it as a calculator, or hex()
convert decimal to hexadecimal in a function), I would certainly use Python hex()
functions to do the conversion, and then manually enter the results into Android In Studio.
Manual conversion of color values using the hex function
Motivation
People are always lazy, want to write this gadget has been a long time, I have also played the idea is:
Input: A decimal value similar to RGB (110, 122 138), separated by a space or comma.
Output: A hexadecimal RGB color value (#6e7a8a).
But has not been the hands, has been fastidious. How lazy!
Open Dry
1. First I need to enter the function
I opened my previous study of Python's folder, which had exactly one raw_input example:
#!/usr/bin/python
#coding =utf-8
raw_input ("\ n etc input")
After the terminal is executed python input.py
, you can enter text.
I need to accept the information entered by the user. How to receive forget, Google, get results, and change the input prompt, print out the input:
input = raw_input ("\ n input color such as 144 60:\n")
print (input)
2. Need to split characters
Query to the Python character split function split()
, which can be separated by default without passing in the parameters. Originally also said in English comma (,) as a separator, now it seems to be able to save, directly with the space division, no matter how many spaces can be automatically segmented. So add the code:
Rgbcolorarray = Input.split ()
print (Rgbcolorarray)
3. Need to traverse array
Simple traversal of the array is how to get it also forgotten, the same search:
For x in Rgbcolorarray:print (x)
4. Convert character to hexadecimal
This time get the string, to become a hexadecimal string. This time requires two functions, int()
and hex()
the INT function can convert the string to an int type, while hex accepts numeric arguments and returns a string. A string that starts at 0x.
So there's the first version.
First version
First version Execution results
Writing such a basic version already basically gets the results I want, and the downside is that I have to manually pay for it, using the brain to memorize hexadecimal values and input. Hopefully you can copy the final result directly.
and further
Although the result has come out, but still hope to progress some, there are several problems:
1. When the number to be converted is less than 16, only one does not show, for example, 11 shows the result is 0xB
2, the reality of the result of more than 0x
3, the results of the display is best linked to facilitate replication, rather than each color line.
Then you need to traverse the array of color values, remove the 0x string, and judge the front of less than 16 to fill 0. Output the results together continuously.
For loop traversal array
The previous use for
of the loop, is a sample from the search, but many lines do not know how to get. Java is more often written in {} braces.
Continue to look for information, so know that is probably the following usage.
#!/usr/bin/python
#-*-coding:utf-8-*-
for num in range (10,20): # Number of iterations from 10 to 20 for
I in range (2,num): # Based on the factor iteration
If num%i = 0: # Determine the first factor
j=num/i # Calculate the second factor
print '%d equals%d *%d '% (num,i,j) break # Jump out
of the current loop else: # The else part of the loop
is print num, ' is a prime number '
Variable declaration
Because you need to do not wrap, you need a character connection, not a straight line print
.
Declaring a variable is having a problem again. Based on the previous variable usage, find some Python code to look, probably know not to declare what type, direct use is good.
So there's the code:
output = "#" for
x in Rgbcolorarray:
intx = int (x)
output = output + Hex (intx)
print (output)
String clipping and stitching.
You need to remove the extra 0x two bits.
Use string clipping, still looking for examples.
#!/usr/bin/python
var1 = ' Hello world! '
VAR2 = "Python runoob"
print "var1[0]:", var1[0]
print "Var2[1:5": ", Var2[1:5" the
execution result of this example:
var1[0]: h< C7/>var2[1:5]: Ytho
By the way, I was also studying Python's colleague, who told me that the following index could be omitted, representing the direct cropping to the end.
such as the above example, if print "var2[1:]", var2[1:]
the result should beython Runoob
So there's code:
output = "#" for
x in Rgbcolorarray:
intx = int (x)
output = output + Hex (intx) [2:]
print (output)
can also be from the back of the number, such as the above examples can be written. For example, if the print "var2[-1:]"
var2[-1:]
result should be OB is the two digits of the string.
So we can write this here hex(intx)[-2:]
(because the output string is similar to 0x23, and so on) that this leads me to post a bug, and I also end with the article to explain what the bug is.
If Else judgment
Then you have to make a judgment and give a 0.
If intx < output
= output + ' 0 ' + hex (intx) [-2:]
else:
output = output + Hex (intx) [-2:]
So you have the Python file:
#!/usr/bin/python
#coding =utf-8
input = raw_input ("Input color such as 144 60:\n")
#print (input)
Rgbcolorarray = Input.split ()
print (rgbcolorarray)
output = "#" for
x in Rgbcolorarray:
intx = Int (x
if Intx <: output
= output + ' 0 ' + hex (intx) [-2:]
else:
output = output + Hex (intx) [-2:]
#print (Hex (int (x))
print (output)
One last step: add Coloru to environment variables
I can get my demerit at this time, but it's a bit inconvenient and I need to write to the directory where this Python file is located.
or write colorU.py
the whole path. It's all so numb, so I need to put it colorU
into the environment variable. I used zsh, so I found the configuration file for the environment variable: ~/.ZSHRC, end plus configuration:
Alias coloru= "Python ~/documents/development/pythonstudy/coloru.py"
This is the last available version of my colleague's guidance, and my initial idea was to colorU.py
set the file as an executable file and add it to the Path. As a result colorU.py
, I added the address of this file to path, and the path in the world should be a directory. This is a more convenient way to add aliases.
Subsequent
It was later said that Zeplin clients could display hexadecimal color values by default, and I used the web version, so I didn't. That is, if I install the client, I don't have to write this script. But it's okay. I learned Python and wrote my own first really useful Python code.
A bug: I found this bug when I was writing this article, and a string like 0x33 was written [-2:] When it was cropped backwards, but there was a problem with writing 0xf. The result of the program input 5 5 5 is #0x50x50x5. Modify to [2:] The crop on it.
The bug caused by the backward cropping string
You can also continue the upgrade experience:
A. The colorU 231 234 123
result #e7ea7b can be obtained by inputting directly in the terminal;
B. With Alfred, exhale the Alfred window, enter the color value, get the result, and enter directly copy the hexadecimal to the pasteboard.
C. Save the color values that have been converted before, so that they are easy to reuse and copy the hexadecimal color directly.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.