RGB色值转换为整数值 | 豆包MarsCode AI刷题

40 阅读1分钟
def solution(rgb):
    rgb_values = rgb[4:-1].split(',')
    
    # Convert the string values to integers
    r, g, b = [int(v.strip()) for v in rgb_values]
    
    # Convert to the integer value by shifting bits
    return (r << 16) + (g << 8) + b

if __name__ == "__main__":
    #  You can add more test cases here
    print(solution("rgb(192, 192, 192)") == 12632256 )
    print(solution("rgb(100, 0, 252)") == 6553852)
    print(solution("rgb(33, 44, 55)") == 2174007)