程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python RGB and hex color string conversion

編輯:Python

Python RGB And hexadecimal color string conversion

Title Description

take #FFFFFF and rgb(255, 255, 255) The two forms of strings are converted to each other .

Input

Input multiple lines , Each line looks like a string in the title .
Ensure that the string is shaped like #[num1][num2][num3] perhaps rgb([num1], [num2], [num3]).

Output

For the corresponding input , Output a string in another format .

The sample input

#FFFFFF
rgb(255, 255, 255)

Sample output

rgb(255, 255, 255)
#FFFFFF

Run code

while(1):
try:
inputstr=input()
if(inputstr[0]=="#"):
str1=inputstr[1:3]
str2=inputstr[3:5]
str3=inputstr[5:7]
num1=int('0x'+str1,16)
num2=int('0x'+str2,16)
num3=int('0x'+str3,16)
print("rgb(%s, %s, %s)" % (str(num1),str(num2),str(num3)))
else:
list=inputstr.split(',')
str1=list[0][4:]
str2=list[1][1:]
str3=list[2][1:-1]
# hex Returned letters A~F All lowercase letters a~f, Output according to the sample , use upper The function converts all letters to uppercase 
hex1=hex(int(str1))[2:].upper()
hex2=hex(int(str2))[2:].upper()
hex3=hex(int(str3))[2:].upper()
# Decimal rotation 16 There will be a default zero when the base is zero , use rjust Function to fill the left side of the string 0
# Empathy ljust Function to fill in the right side of a string 0
hex1 = hex1.rjust(2, '0')
hex2 = hex2.rjust(2, '0')
hex3 = hex3.rjust(2, '0')
outputstr="#"
outputstr=outputstr+hex1+hex2+hex3
print(outputstr)
except:
break

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved