Python implements a small tool for color-color value conversion _python

Source: Internet
Author: User

  Requirements Description

The company's UI design younger brother, has been diverted to Zeplin for a long time. Zeplin's design shows that the color value of the page is expressed in decimal RGB, and that the colors in Android are most often required in hexadecimal RGB. My math is not good to directly see the decimal can be in mental arithmetic to get hexadecimal results, so I need a tool, input decimal RGB, get hexadecimal color value, preferably easy to copy.

Zeplin Color-color Value Display Example

  Original processing mode

Because I'm going to Python (only for the terminal input Python and then as a calculator, or to use the hex () function to convert the decimal to hexadecimal), I am certainly using the Python hex () function to do the conversion, and then manually enter the results into the Android studi O in.

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:

Python code

#!/usr/bin/python 

#coding =utf-8 

 

raw_input ("\ n etc input") 

After the terminal executes the 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:

Python code

input = raw_input ("\ n input color such as 144 60:\n") 

  2. Need to split characters

Query to the Python character split function split (), the default without passing parameters can be separated by whitespace. 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:

Python 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:

Python code

 
 

  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 converts a string to an int type, and Hex takes a numeric argument and returns a string. A string that starts at 0x.

So there's a version of it.

So there's the first version.

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 is more than 0x

3. The results of the display are best linked together for easy copying, rather than one line per color.

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

In front of the For 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.

Python code

#!/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 not wrap, you need a character connection, not a direct 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:

Python 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.

Python code

#!/usr/bin/python 
 
var1 = ' Hello world! ' 
VAR2 = "Python runoob" 
 
print "var1[0]:", var1[0] 
print "Var2[1:5": ", Var2[1:5] 

The results of this paradigm:

Python code

Var1[0]: H 
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.

For example, the above example if print "var2[1:]", var2[1:] The result should be Ython Runoob

So there's code:

Python 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, the above example if print "Var2[-1:]", var2[-1:] The result should be OB, which is the back two bits of the string.

So here we can write Hex (intx) [-2:] (because the output string is similar to 0x23, and so on) that's why I wrote a bug later, and I also end up explaining what this bug is.

If Else judgment

Then you have to make a judgment and give a 0.

Python code

If intx < output 
 = output + ' 0 ' + hex (intx) [-2:] 
else: 
 output = output + Hex (intx) [-2:] 

So you have the Python file:

Python code

#!/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.

Python code

 
 

or write all coloru.py this path. It's all so numb, so I need to add Coloru to the environment variable. I used zsh, so I found the configuration file for the environment variable: ~/.ZSHRC, end plus configuration:

Python code

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 set the coloru.py file as an executable file and add it to the Path. As a result, I added the address of the coloru.py file to path, and the path in the world should be a directory. This is a more convenient way to add aliases.

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.

2. A bug: I was writing this article when I found this bug, similar to 0x33 such strings from the back of the time when the cut to write [-2:], of course, no problem, but write 0xf such a string will have a problem. 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 result #e7ea7b can be obtained by inputting Coloru 231 234 123 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.