【每日一题20220418】颜色转换

:mage:‍ 今天的任务是,给定3个数字表示RGB的色值,请编写一个函数,返回该颜色对应十六进制色值的字符串,字母均为大写。RGB的数字范围是0~255。

【示例】
输入:(148, 0, 211)
输出:9400D3
解释:十进制数字148、0、211对应的十六进制分别为0x94、0x0、0xd3, 所以最终表示为9400D3

题目难度:中等
题目来源:CodeWars-RGB To Hex Conversion

def solution(r: int, g: int, b: int)-> str:
    # your code here

assert solution(1, 2, 3) == "010203"
assert solution(255, 255, 255) == "FFFFFF"
assert solution(148, 0, 211) == "9400D3"
1 个赞
def solution(r: int, g: int, b: int)-> str:
    # your code here
    arg = r,g,b
    RGB = []
    for item in arg:
        new = str(hex(item)).split("x")[-1].upper()
        if len(new) == 1:
            RGB.append("0"+new)
            continue
        RGB.append(new)
    return "".join(RGB)


assert solution(1, 2, 3) == "010203"
assert solution(255, 255, 255) == "FFFFFF"
assert solution(148, 0, 211) == "9400D3"
def solution(r: int, g: int, b: int)-> str:
    list_1,b=[r,g,b],''
    for i in list_1:
        if len(hex(i))<=3:
            b=b+''.join(hex(i).split('x'))
        else:
            b=b+''.join(hex(i).split('0x'))
    return b.upper()

def solution(r: int, g: int, b: int) -> str:
    return "".join(map(lambda x: hex(x)[2:].upper().rjust(2, '0'), (r, g, b)))


assert solution(1, 2, 3) == "010203"
assert solution(255, 255, 255) == "FFFFFF"
assert solution(148, 0, 211) == "9400D3"

借助上述大佬的灵感,改用zfill进行填充
def solution(r: int, g: int, b: int) -> str:
    return "".join(map(lambda x: hex(x)[2:].upper().zfill(2), (r, g, b)))
def solution(r: int, g: int, b: int) -> str:
    # your code here
    return ''.join('%02X' % max(0, min(a, 255)) for a in (r, g, b))
1 个赞
def solution(r: int, g: int, b: int) -> str:
    return ('%02x%02x%02x' % (r, g, b)).upper()

def rgb_solution(r, g, b):
rgb = [r, g, b]
rgb_new =
for i in rgb:
rgb_hex = hex(i).split(‘0x’)
if len(rgb_hex[1]) < 2:
rgb_hex[1] = ‘0’ + rgb_hex[1]
rgb_new.append(rgb_hex[1].upper())
else:
rgb_new.append(rgb_hex[1].upper())
rgb_new = ‘’.join(rgb_new)
return rgb_new

直接用X可解

def solution(r: int, g: int, b: int) -> str:
    return "%02X%02X%02X" % (r, g, b)
1 个赞

是把x换成X是吧,直接转成大写的16进制,%02X%02X%02X 好的 谢谢哈 :+1:

def solution(r: int, g: int, b: int)-> str:
    # your code here
    rgb=(r,g,b)
    newstr=""
    for n in rgb:
        strn=str(hex(n).upper()[2:])
        if len(strn)==1:
            strn="0"+strn
            newstr+=strn
            continue
        newstr+=strn

    return newstr
assert solution(1, 2, 3) == "010203"

assert solution(255, 255, 255) == "FFFFFF"
 assert solution(148, 0, 211) == "9400D3"
def solution(r: int, g: int, b: int)-> str:
    # your code here
    dict1 = {0: '0', 1: '1', 2:'2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
    list1 = ["0" + dict1[i]  if i < 16 else dict1[i // 16] + dict1[i % 16] for i in (r, g, b)]
    return "".join(list1)


# assert solution(1, 2, 3) == "010203"
# assert solution(255, 255, 255) == "FFFFFF"
# assert solution(148, 0, 211) == "9400D3"
def solution(r: int, g: int, b: int) -> str:
    result = ""
    for i in [r, g, b]:
        result = result + ('0' + (hex(i)[2:]))[-2:].upper()
    return result
def solution(r: int, g: int, b: int)-> str:
    vals=[]
    for j in (r, g, b):
        if j<0:
            vals.append(0)
        elif 0<= j<=255:
            vals.append(j)
        if j>255:
            vals.append(255)
    return ''.join([hex(i).replace('0x','').zfill(2) for i in vals]).upper()
def solution(r: int, g: int, b: int)-> str:
    def to_hex(x):
        return f'{x:0>2X}'.upper()
    return ''.join([to_hex(r),to_hex(g),to_hex(b)])

assert solution(1, 2, 3) == "010203"
assert solution(255, 255, 255) == "FFFFFF"
assert solution(148, 0, 211) == "9400D3"
def solution(r: int, g: int, b: int) -> str:
    rgb = r, g, b
    result = []
    for i in rgb:
        rgb_hex = hex(i).upper()[2:]
        if len(rgb_hex) == 2:
            result.append(rgb_hex)
        else:
            result.append('0' + rgb_hex)
    return ''.join(result)


assert solution(1, 2, 3) == "010203"
assert solution(255, 255, 255) == "FFFFFF"
assert solution(148, 0, 211) == "9400D3"
def solution(r: int, g: int, b: int) -> str:
    return "%02X%02X%02X" % (r, g, b)
def solution(r: int, g: int, b: int)-> str:
	if r>=0&r<=255 & g>=0&g<=255 & b>=0&b<=255:
		RGB = map(lambda x : hex(x)[2::].upper(),(r,g,b))
		print("".join(RGB))
		
	else:
		print("r,g,b中有不在0-255之间的值")

solution(20,240,118)

def solution(r: int, g: int, b: int)-> str:
    rgb=[hex(r),hex(g),hex(b)]
    res=""
    for i in range(3):
        if len(rgb[i]) >3:
            res=res+re.sub("0x", "", rgb[i])
        elif len(rgb[i]) <=3:
           res=res+re.sub("x", "", rgb[i])
    return res.upper()
def solution(r: int, g: int, b: int)-> str:
    return ''.join(['0'*(4-len(hex(i)))+hex(i)[2:] for i in (r,g,b)]).upper()

assert solution(1, 2, 3) == "010203"
assert solution(255, 255, 255) == "FFFFFF"
assert solution(148, 0, 211) == "9400D3"