A shell script for converting percentage opacity to hexadecimal
A nest of chickens 2021-08-05 17:00:52

water ~

Let's have a script for the first Nuggets ~

The main function of this script is to convert the percentage into 16 Base number .

reason : The design leader of our company submits to lanhu The color of your design is controlled by percentage opacity , and Android It uses 16 Base number ARGB. So there's this script ~

Create a .sh file

for example :tran_calc.sh. Then copy and paste the following :

#!/bin/bash
# Calculation 0%~100%
tran_table(){
tran=0
while((tran <= 100))
do
tran_to_hex $tran;let "tran++"
done
}
# Calculate and format the output hex
tran_to_hex(){
# for example :tran_to_hex FF here $1 refer to FF . It also corresponds to the following 【 Parameters 2】
tran=$1
temp=$((255*$tran/100))
# Calculation hex value , And supplement the preamble 0
hexStr=$(echo "obase=16;$temp"|bc|awk '{ len = (2 - length % 2) % 2; printf "%.*s%s\n", len, "00000000", $0}')
# Format output 
printf " transparency %3s percentage %3s%% Hex %s \n" $temp $tran $hexStr
}
# When the parameter is greater than two perform then Internal code 
if test $# -ge 2
then
# The first parameter is zero hex When it comes to execution then Internal code 
if test $1 = "hex"
then
# call function , And pass in 【 Parameters 2】
tran_to_hex $2
else
tran_table
fi
else
# There are less than two parameters , Output cross reference table 
tran_table
fi
 Copy code 

Add executable rights

chmod a+x tran_calc.sh
 Copy code 

Create soft link

The terminal executes the following instructions

ln -s `pwd`/tran_calc.sh /usr/local/bin/tran_calc
 Copy code 

Use

Now you can execute... At the terminal

  • Output 0%~100% Transparency comparison table
tran_calc
 Copy code 

perhaps

  • Convert values to 16 Base number
tran_calc hex [ The number ]
 Copy code 

for example tran_calc hex 50 That is, calculate 50% Diaphaneity hex value

Expanding knowledge

Shell Pass parameters . Look at the comparison table below , come from Novice tutorial -—— Pass parameter section .

How to write it meaning
$# The number of parameters passed to the script
$* Display all the parameters passed to the script in a single string . Such as " " use 「 " 」 In a nutshell 、 With " *" use 「"」 In a nutshell 、 With " 1 2 2 … n" Output all parameters in the form of .
$$ The current process the script runs ID Number
$! Of the last process running in the background ID Number
[email protected] And identical , But use quotation marks , And return each parameter in quotation marks . Such as " * identical , But use quotation marks , And return each parameter in quotation marks . Such as " @" use 「"」 In a nutshell 、 With " 1 " " 1" " 2" … "$n" Output all parameters in the form of .
$- Show Shell Current options used , And set command Function the same .
$? Display the exit status of the last command .0 No mistakes , Any other value indicates an error .

I wanted to see if I had a chance to participate in 8 The month's Gengwen activity . The number of words is too small to participate in 、 A large piece of code can't participate in 、 Those who have a lot of references cannot participate in . Ha ha ha . I took it all . Come on come on ~

Make do with . Welcome to correct ~~

Please bring the original link to reprint ,thank
Similar articles