Introduction to using Python to implement a color-color-value conversion gadget

Source: Internet
Author: User
Tags color representation
   Requirements Description
The company's UI design brother, has been diverted to Zeplin for a long time. Zeplin's design presentation The color value of the page is expressed in decimal RGB, and in most cases the color representation in Android requires a hexadecimal RGB representation. My math is not good to directly see the decimal can be in arithmetic to get hexadecimal results, so I need a tool, input decimal RGB, get hex color value, preferably can be easily copied.

Example of Zeplin color value display
   Original Treatment Method
Because I would python (terminal input python and then as a calculator, or hex () function to convert decimal to hexadecimal), so I am of course using the Python hex () function to do the conversion, and then the manual result input to the Android studi o Medium.

Convert color values manually using the hex function
   Motive
People are always lazy, want to write this gadget has been a long time, I have also played the idea is:
Input: RGB-like decimal values (110, 122 138), separated by a space or comma.
Output: A hexadecimal RGB color value (#6e7a8a).
But it has never been done, has been fastidious. How lazy!
   Open Dry
   1. First I need to enter a function
I opened the folder where I learned Python before, and there was a 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 forgotten, Google, get results, by the way change the input prompt, print out the input content:
Python code

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 (), the default is not passed in parameters can be separated by whitespace characters. Originally also said with a comma (,) as a delimiter, now seems to be saved, directly separated by a space, no matter how many spaces can be automatically segmented. Then add the code:
Python code

Rgbcolorarray = Input.split () print (Rgbcolorarray)


  3. Need to iterate through an array
The simple traversal of the array is how to get it forgotten, the same search:
Python code

For x in Rgbcolorarray:print (x)


  4. Convert characters 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, 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

Results of the first version execution
Writing this basic version of the basic can get the results I want, the disadvantage is that I need to manually income, use the brain memory hex color value and then input. Hopefully, you can copy the final result directly.
and further
Although the results have 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 display, such as the result of the 11 display is 0xB
2.0x more realistic results
3. The displayed results 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 less than 16 in front of 0. Outputs the result continuously together.
For loop traversal array
In front of the use of the For loop, is from the sample found, but many lines do not know how to get. Java is mostly written in {} curly braces.
Continue to check the information, and then know is probably the following usage.
Python code

#!/usr/bin/python #-*-Coding:utf-8-*-  for num in range (10,20): # iterations between 10 and 20 for the number for  I in range (2,num): # based on Sub-Iteration   if num%i = = 0:  # Determines the first factor    j=num/i   # Calculates the second factor    print '%d equals%d *%d '% (num,i,j) break   # jump out of the current Ring  Else:     # The else part of the Loop   print num, ' is a prime number '


Variable declaration
Because you need not wrap, you need a character connection, not a direct print.
Declaring a variable has another problem. According to the previous variable usage, looking for some Python code to see, probably know not to declare what type, directly with the 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
The extra 0x two bits need to be removed.
Use the string clipping and still look for examples.
Python code

#!/usr/bin/python  var1 = ' Hello world! ' var2 = "python runoob"  print "var1[0]:", var1[0] print "var2[1:5]:", V Ar2[1:5]


The results of this sample execution:
Python code

Var1[0]: H Var2[1:5]: Ytho


By the way, I was also learning about Python's colleague, who told me that the following index could be omitted, representing the crop directly to the end.
For example, if print "var2[1:]", var2[1:] The result should be Ython Runoob
So there's the code:
Python code

output = "#" for X in Rgbcolorarray:  intx = int (x)  output = output + Hex (intx) [2:] Print (output)


It can also be written from a backward-forward number, such as the above example. For example, if print "var2[-1:]", var2[-1:] The result should be that the OB is the post 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 behind me, and I ended up explaining what the bug was.
If Else judgment
Then make a judgment, give one's 0
Python code

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


This will have the Python file:
Python code

#!/usr/bin/python #coding =utf-8 input = raw_input ("\ n 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)


And one last step: add Coloru to the environment variable
This time I can get my demerit, but it's not very convenient, I need to go to the directory where this Python file is written
Python code

Python coloru.py


or write full coloru.py this path. is a very hemp thing, so I need to add Coloru to the environment variable. I'm using zsh, so find the configuration file for the environment variable: ~/.ZSHRC, plus the configuration at the end:
Python code

Alias coloru= "Python ~/documents/development/pythonstudy/coloru.py"


This is the last viable version after a colleague's guidance, and my first thought was to set the coloru.py file as an executable and add it to Path. As a result I added the address of the coloru.py file to the path, and the path in the world should be a directory. It's easier to add aliases in this way.
That is to say, if I install the client, I can use the script I wrote. But it's okay. I learned Python and wrote my own first really useful Python code.
2. A bug: I found this bug when I wrote this article, like 0x33 such a string from the back of the clip to write [-2:], of course, no problem, but write 0xf such a string will be problematic. Program Input 5 5 5 The result is #0x50x50x5. modified to [2:] The clipping is OK.

A bug caused by backward cropping a string
You can also continue the upgrade experience:
A. The result #e7ea7b can be obtained by inputting Coloru 231 234 123 directly into the terminal;
B. With Alfred, after exhaling The Alfred window, enter the color value, get the result, enter the direct copy hex to the pasteboard.
C. Save the previously converted color values, easy to reuse the color, directly copy the hexadecimal color.

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.