javascript - javascript中的十六进制到rgb转换器
问题描述
我正在尝试制作一个简单的 rgb 到十六进制转换器,但我一直被 Javascript 卡住,我做错了什么?在 html 部分,我制作了一个表单,在提交时调用 convert() 函数。
function convert() {
r = parseInt(document.getElementById('r').value);
g = parseInt(document.getElementById('g').value);
b = parseInt(document.getElementById('b').value);
rgb(r, g, b);
function rgb(r, g, b){
res = ColorToHex(r) + ColorToHex(g) + ColorToHex(b);
function ColorToHex(color) {
if (color > 255) return "FF";
else if (color < 0) return "00";
else color.toString(16).padStart(2, "0").toUpperCase();
}
}
document.getElementById('result').innerHTML = res;
return false;
}
解决方案
这从 RGB 返回十六进制
console.log(convert('255','18', '50'));
function convert(r, g, b) {
r = parseInt(r); g = parseInt(g); b = parseInt(b);
res = r.toString(16) + g.toString(16) + b.toString(16);
res = res.toUpperCase();
return res;
}
推荐阅读
- python-3.x - python3模块v:“module.classObj”
- python - 图像分类问题中 Raspberry Pi 0 上的 Tensorflow Lite 量化
- amazon-web-services - Correct Root CA for aswIoT mqtt server
- swift - 通用函数:表达式类型不明确,没有更多上下文
- amazon-web-services - 使用实例 ID 访问在 AWS 实例中运行的 WebService
- android - 使用 Jetpacks 导航组件时的奇怪片段生命周期
- python - PyMC3 和 Arviz:使用 arviz plot_hpd 可视化多种条件下的最高后验密度
- azure-functions - 有没有办法获取等待订阅的消息计数?
- javascript - 用于动态生成内容的自适应 vanilla JS 页面浏览器
- html - 悬停时的叠加层比图像宽