当前位置:网站首页>颜色字符串转换
颜色字符串转换
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
边栏推荐
- C语言结构体指定初始化
- Shell脚本 &&和||的使用
- undefined reference to `Nabo::NearestNeighbourSearch
- 往String原型上封装一个时间戳转日期的方法
- C语言中volatile的使用
- [UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
- [stepping on the pit] MELD in win11 wsl2 cannot be used normally. Problem repair
- 查漏补缺(四)
- [UDS unified diagnostic service] II. Network layer protocol (1) - overview and functions of network layer
- 查漏补缺(九)---程序篇
猜你喜欢
![[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit](/img/22/c501c79176a93345dc72ff150c53c3.png)
[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit

卷积神经网络实现CIFAR100数据集分类
![[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview](/img/c5/4b3092daeabf0f4889d93fef327c88.png)
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview

cv_bridge 与opencv 版本不匹配的解决

基于TensorFlow的线性回归实例

VHDL finite state machine (FSM) code example

Set up a personal blog of jpress

HDU-Tunnel Warfare

死区时间的分析与设置

FOC single resistance sampling position loop control servo motor
随机推荐
服务器常见错误代码 总结
微信小程序之改变数组中某值,对象中某值的方法
说说ts的心里话
解析psd文件,并映射成组件
Qt 给应用程序加图标
查漏补缺(七)
JS中的this指向
查漏补缺(六)
算数表达式
[UDS] unified diagnostic service (UDS)
v-for下定时给图片添加动画
realsense 选型大对比D455 D435i D415 T265 3D硬件对比
导入文件时候 new FormData()
The difference between single quotation mark, double quotation mark and back quotation mark in shell script
Krypton binary
ROS包nmea_navsat_driver读取GPS、北斗定位信息笔记
JS实现私有属性
TP download folder, compress folder and download
软件工程中的十三种文档
基于TensorFlow的线性回归实例