当前位置:网站首页>颜色字符串转换
颜色字符串转换
2022-04-23 05:53:00 【KK要继续努力】
颜色字符串转换
题目:
将 rgb 颜色字符串转换为十六进制的形式,如 rgb(255, 255, 255) 转为 #ffffff
- rgb 中每个 , 后面的空格数量不固定
- 十六进制表达式使用六位小写字母
- 如果输入不符合 rgb 格式,返回原始输入
解答:
function rgb2hex(sRGB) {
var reg = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/;
var arr = sRGB.match((reg));
if(!arr){
return sRGB;
}else{
var num1 = Number(arr[1])<16?'0'+Number(arr[1]):Number(arr[1]);
var num2 = Number(arr[2])<16?'0'+Number(arr[2]):Number(arr[2]);
var num3 = Number(arr[3])<16?'0'+Number(arr[3]):Number(arr[3]);
var num = "#" + num1.toString(16)+ num2.toString(16)+ num3.toString(16);
return num;
}
}
match()方法
匹配查找字符串,返回符合正则表达式内容的部分,返回值是数组类型,没有找到则返回null。
//如果正则表达式没有使用g
console.log('123ssss 123 sss f123fff'.match(/fff/)); //['fff', index: 20, input: '123ssss 123 sss f123fff', groups: undefined]
//如果正则表达式使用g,没有后面的内容
console.log('xxxssss sss ffff'.match(/xxx/g)); //['xxx']
toString()方法
转为字符串类型,参数为进制,如果每写参数默认为10。例如:2,8,16,…
知识点:正则表达式、toString(16)转换进制
版权声明
本文为[KK要继续努力]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45393020/article/details/117995252
边栏推荐
猜你喜欢
基于VGG对五种类别图片的迁移学习
Eigen 学习总结
QT add qserialport class to realize serial port operation
cuda工程更换环境(电脑)后遇到的一系列编译问题
Informatics one book pass - small ball
Error in created hook: “ReferenceError: “Promise”未定义“
谈谈v-if显示隐藏问题
Analysis and setting of dead time
Principle and characteristic analysis of triode
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
随机推荐
Detailed explanation and application principle of token
ES6面试题(参考文档)
[UDS unified diagnostic service] III. application layer protocol (1)
Log writing method (with time)
Declared as a global variable
CUDA环境安装
ROS包nmea_navsat_driver读取GPS、北斗定位信息笔记
Makefile基础、常用函数及通用Makefile
卷积神经网络实现CIFAR100数据集分类
基于SSD的物体检测案例实现
[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)
[UDS] unified diagnostic service (UDS)
监听除某元素之外点击事件
微信小程序之 js 时间戳/1000 转换 秒,六个小时后,一天后,本周五 选项计算时间
PN结、二极管原理详解与应用
Krypton zeal
useCenterHook
微信小程序之改变数组中某值,对象中某值的方法
C [document operation] PDF files and pictures are converted to each other
汇编基础代码示例