Introduction to a small tool for color value conversion using Python

Source: Internet
Author: User
Tags color representation
This article describes how to use Python to implement a small tool for color value conversion. 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.
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
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.

-->

The preceding section describes how to use Python to convert color and color values. For more information, see other related articles in the first PHP community!

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.