Python implements a small tool for color value conversion, python

Source: Internet
Author: User

Python implements a small tool for color value conversion, python

  Requirements

The company's UI design elder brother has been switching to Zeplin for a long time. The Zeplin design draft displays the color values of the page in decimal RGB format. In Android, the color representation usually requires hexadecimal RGB format. I don't have enough mathematics to directly see the decimal to get the hexadecimal result. So I need a tool to input the decimal RGB to get the hexadecimal color value, it is best to facilitate replication.

Example of displaying the color value of Zeplin

  Original Processing Method

Because I will use Python (only input python on the terminal and use it as a calculator, or use the hex () function to convert decimal to hexadecimal ), in this case, I naturally use the python hex () function for conversion, and then manually input the result to Android Studio.

Use the hex function to manually convert the color value

  Motivation

People are always too lazy to write this tool for a long time. I have also thought about it:

Input: decimal value similar to RGB (110,122 138), separated by space or comma.

Output: A hexadecimal RGB color value (# 6e7a8a ).

But I have never been able to do it, and I have been paying attention to it. Really lazy!

  Start dry

  1. First, I need to enter the Function

I opened the folder where I learned Python. There is exactly an example of raw_input:

Python code

#! /Usr/bin/python # coding = UTF-8 raw_input ("\ n and other inputs ")

After python input. py is executed on the terminal, you can enter text.

I need to receive user input information. How can I receive the message "forgot"? Google: Get the result. By the way, modify the input prompt and print the input content:

Python code

Input = raw_input ("\ n input color, for example, 50 144 60: \ n") print (input)

  2. characters to be separated

The python character segmentation function split () is found. By default, blank spaces can be used for separation without passing in parameters. Originally, the English comma (,) is used as the separator. Now it seems that it can be saved and separated directly by space, no matter how many spaces can be automatically separated. Then add the Code:

Python code

rgbColorArray = input.split() print(rgbColorArray)

3. the array needs to be traversed.

I forgot how to retrieve the array in a simple way. I also searched:

Python code

for x in rgbColorArray: print(x) 

  4. Convert the character to hexadecimal

At this time, the string will be converted into a hexadecimal string. At this time, two functions are required: int () and hex (). The int function can convert the string to the int type, while hex accepts the number parameter and returns the string. A string starting with 0x.

So there is a version.

So we have the first version.

First version

First version

Execution result of the first version

Writing such a basic version can basically get the result I want. The disadvantage is that I still need to manually earn the result and use the brain to remember the hexadecimal color value and then input it. You can copy the final result directly.

Further

Although the results have come out, I still hope to make some progress. There are several problems:

1. When the number to be converted is smaller than 16, only one digit is not displayed. For example, the result of 11 is 0xB.

2. The actual result is 0x more.

3. It is best to connect the displayed results to facilitate copying, instead of one row of each color.

Then we need to traverse the color value array, remove the 0x string, and add 0 to the front of the judgment smaller than 16. Output results consecutively.

For Loop traversal Array

The for loop is used in the previous step, which is an example of the query. However, I don't know how to get it through multiple rows. Java writes are usually enclosed in braces.

Continue to query the information, so we know the following usage.

Python code

#! /Usr/bin/python #-*-coding: UTF-8-*-for num in range (): # iterations of numbers between 10 and 20 for I in range (2, num): # iteration Based on the factor 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: # print num of the loop else, 'is a prime number'

Variable Declaration

Because the line breaks are not required, character connections are required, rather than print directly.

Another problem occurs when declaring variables. Based on the usage of the preceding variables, I found some python code to check whether I need to declare any types. I just need to use them directly. So with the code:

Python code

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

String cropping and splicing

Remove the extra 0x two digits.

Use string pruning to find examples.

Python code

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

Execution result of this example:

Python code

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

By the way, I also asked my colleagues who are learning python. He told me that the index can be omitted, which means that the index can be cropped to the end.

For example, in the preceding example, if print "var2 [1:]" And var2 [1:], the result is ython Runoob.

So there is code:

Python code

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

It can also be written from the back to the front, for example, the above example. For example, in the preceding example, if print "var2 [-1:]" And var2 [-1:], the result is ob, which is the last two digits of the string.

So here we can write it as hex (intx) [-2:] (because the output string is similar to 0x23, this is the case). This causes me to write a bug later, I also explained the bug at the end of the article.

If else judgment

Next, let's make a judgment and add 0 to the first digit.

Python code

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

In this way, the python file is available:

Python code

#! /Usr/bin/python # coding = UTF-8 input = raw_input ("\ n input color, for example, 50 144 60: \ n") # print (input) rgbColorArray = input. split () print (rgbColorArray) output = "#" for x in rgbColorArray: intx = int (x) if intx <16: output = output + '0' + hex (intx) [-2:] else: output = output + hex (intx) [-2:] # print (hex (int (x) print (output)

The last step is to add ColorU to the environment variable.

At this time, I can remember what I want, but it is a little inconvenient. I need to write it in the directory where the python file is located.

Python code

python colorU.py 

Or write the full colorU. py path. So I need to add colorU to the environment variable. I used zsh, so I found the environment variable configuration file :~ /. Zshrc. Add the following configuration at the end:

Python code

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

This is the final feasible version after being instructed by another colleague. My initial thought was to set the colorU. py file to an executable file and then add it to the Path. As a result, the address of the colorU. py file is added to the Path. The PATH in the world should be a directory. This makes it easier to add aliases.

That is to say, if I install the client, I don't need to write this script. But it doesn't matter if I learned python and wrote my first truly useful python code.

2. A bug: I found this bug only when I was writing this article. I wrote [-2:] When cropping strings like 0x33 from the back. Of course there is no problem, however, writing strings such as 0xf may cause problems. Input 5 5 5. The result is #0x50x50x5. Change to [2.

A bug caused by backward cropping strings

You can continue to upgrade your experience:

A. Enter colorU 231 234 123 in the terminal to obtain the result # e7ea7b;

B. In combination with Alfred, after calling out the Alfred window, enter the color value and get the result. Press enter to copy the hexadecimal format to the clipboard.

C. Save the previously converted color values to make it easier to reuse the color and copy the hexadecimal color directly.

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.