codewars(python)练习笔记十二:十进制颜色转十六进制

codewars(python)练习笔记十二:十进制颜色转十六进制

题目

The rgb() method is incomplete. Complete the method so that passing in RGB decimal values will result in a hexadecimal representation being returned. The valid decimal values for RGB are 0 - 255. Any (r,g,b) argument values that fall out of that range should be rounded to the closest valid value.

The following are examples of expected output values:

rgb(255, 255, 255) # returns FFFFFF
rgb(255, 255, 300) # returns FFFFFF
rgb(0,0,0) # returns 000000
rgb(148, 0, 211) # returns 9400D3

题目大意:计算颜色值,将0~255 转 16进制。超出范围的,应该四舍四入到最接近的有效值。

我的解法:

#!/usr/bin/python

def rgb(r, g, b):
    temp = ''
    for item in [r,g,b]:
        if item < 0:
            item = 0
        elif item > 255:
            item = 255
        temp += str(hex(item)).replace('0x','').upper().zfill(2)
    return temp

几种不同的进制转换,这一块的知识点我比较弱,就弄最简单的思路来写。

其他解法一:

def rgb(r, g, b):
    round = lambda x: min(255, max(x, 0))
    return ("{:02X}" * 3).format(round(r), round(g), round(b))

其他解法二:

def limit(num):
    if num < 0:
        return 0
    if num > 255:
        return 255
    return num


def rgb(r, g, b):
    return "{:02X}{:02X}{:02X}".format(limit(r), limit(g), limit(b))

其他解法三:

def rgb(r, g, b):
    clamp = lambda x: max(0, min(x, 255))
    return "%02X%02X%02X" % (clamp(r), clamp(g), clamp(b))

其他解法四:

def rgb(*args):
  return ''.join(map(lambda x: '{:02X}'.format(min(max(0, x), 255)), args));

其他解法五:

def rgb(r, g, b):
  clamp = lambda x: max(0, min(x, 255))
  return "{:02X}{:02X}{:02X}".format(clamp(r), clamp(g), clamp(b))

其他解法六:

def rgb(r, g, b): 
    return ''.join(['%02X' % max(0, min(x, 255)) for x in [r, g, b]])
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,974评论 0 10
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,330评论 0 38
  • 继续这份…… 在此刻已经是晚上22:10左右,一点小酒下去,感觉整个世界才刚刚开始耶!人家问我,你这是喝了多少呀!...
    林子2016阅读 288评论 0 0
  • 当悲伤太多的时候,一个人已经无法承受,我就把投注在一个人身上的所有煎熬分别来接受。
    陈鸿宇o阅读 139评论 0 0
  • 千万不要被标题吓倒?因为这就是我对它无比镌刻的原因! 2017年8月得知怀孕了,带着欣喜和如梦般渡过了孕早期,舒适...
    Twinkle_L阅读 311评论 1 2

友情链接更多精彩内容