列表

详情


FED30. 颜色字符串转换

描述

将 rgb 颜色字符串转换为十六进制的形式,如 rgb(255, 255, 255) 转为 #ffffff
1. rgb 中每个 , 后面的空格数量不固定
2. 十六进制表达式使用六位小写字母
3. 如果输入不符合 rgb 格式,返回原始输入

示例1

输入:

'rgb(255, 255, 255)'

输出:

#ffffff

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77832KB, 提交时间: 2020-12-26

{"css":"","js":"function rgb2hex(sRGB) {\n    var regexp=/rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)/;\n    var ret=sRGB.match(regexp);\n    if(!ret){\n        return sRGB;\n    }else{\n        var str='#';\n        for(var i=1;i<=3;i++){\n            var m=parseInt(ret[i]);\n            if(m<=255&&m>=0){\n                str+=(m<16?'0'+m.toString(16):m.toString(16));\n            }else{\n                return sRGB;\n            }\n        }\n        return str;\n    }\n}\n","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 874ms, 内存消耗: 77840KB, 提交时间: 2020-12-26

{"css":"","js":"function rgb2hex(sRGB) {\n    let reg=/^(rgb|RGB)\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)$/;\n    if(!reg.test(sRGB)) return sRGB;\n    let temp = sRGB.replace(reg,'$2,$3,$4');\n    return '#'+temp.split(',').map(item=>{\n        if(+item>16) return (+item).toString(16);\n        return '0'+(+item).toString(16);\n    }).join('');    \n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 877ms, 内存消耗: 77824KB, 提交时间: 2020-11-10

{"css":"","js":"function rgb2hex(sRGB) {\n    var reg=/^(rgb|RGB)/;\n    if(reg.test(sRGB)){\n        var strHex=\"#\";\n        var colorArr = sRGB.replace(/(?:\\(|\\)|rgb|RGB)*/g,\"\").split(\",\")\n        for(var i=0;i<colorArr.length;i++){\n            var hex = Number(colorArr[i]).toString(16);\n            if(hex===\"0\"){\n                hex=hex+hex\n            }\n            strHex=strHex+hex\n        }\n        return strHex\n    }else{\n        return String(sRGB)\n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 880ms, 内存消耗: 77780KB, 提交时间: 2020-11-23

{"css":"","js":"function rgb2hex(sRGB) {\n    var reg=/^(rgb|RGB)/;\n    if(reg.test(sRGB)){\n        var strHex=\"#\";\n        var colorArr = sRGB.replace(/(?:\\(|\\)|rgb|RGB)*/g,\"\").split(\",\")\n        for(var i=0;i<colorArr.length;i++){\n            var hex = Number(colorArr[i]).toString(16);\n            if(hex===\"0\"){\n                hex=hex+hex\n            }\n            strHex=strHex+hex\n        }\n        return strHex\n    }else{\n        return String(sRGB)\n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 881ms, 内存消耗: 77856KB, 提交时间: 2020-12-29

{"css":"","js":"function rgb2hex(sRGB) {\n    const regex = /^rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)$/;\n    if(!regex.test(sRGB)){\n        console.log(1);\n        return sRGB;\n    }\n    sRGB = sRGB.slice(4, sRGB.length-1).split(\",\");\n    var to16 = (string) => {\n        if(Number(string).toString(16).length<=1){\n            return `0${Number(string).toString(16)}`;\n        }else{\n            return Number(string).toString(16);\n        }\n    }\n    return `#${to16(sRGB[0])}${to16(sRGB[1])}${to16(sRGB[2])}`;\n}","html":"","libs":[]}

上一题