当前位置:网站首页>Color string conversion
Color string conversion
2022-04-23 06:50:00 【KK should continue to work hard】
Color string conversion
subject :
take rgb The color string is converted to hexadecimal form , Such as rgb(255, 255, 255) To #ffffff
- rgb Each of them , The number of spaces after is not fixed
- Hexadecimal expressions use six lowercase letters
- If the input does not match rgb Format , Return to original input
answer :
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() Method
Match the lookup string , Returns the part that matches the content of the regular expression , The return value is an array type , If not found, return null.
// If the regular expression is not used g
console.log('123ssss 123 sss f123fff'.match(/fff/)); //['fff', index: 20, input: '123ssss 123 sss f123fff', groups: undefined]
// If regular expressions use g, There is no following content
console.log('xxxssss sss ffff'.match(/xxx/g)); //['xxx']
toString() Method
Convert to string type , The parameter is hexadecimal , If every write parameter defaults to 10. for example :2,8,16,…
Knowledge point : Regular expressions 、toString(16) Convert base
版权声明
本文为[KK should continue to work hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230551363990.html
边栏推荐
猜你喜欢
随机推荐
freeCodeCamp----time_calculator练习
金额输入框,用于充值提现
特效案例收集:鼠标星球小尾巴
ASP.NET CORE JWT认证
若依如何input改成文本
Detailed explanation and application of PN junction and diode principle
微信小程序之改变数组中某值,对象中某值的方法
Sdoi2009-hh Necklace
查漏补缺(九)---程序篇
几款笔记软件的优缺点
Database programming of node
1-4 NodeJS的安装之配置可执行脚本
Modify registry values
Promise(一)
锚点定位——如何设置锚点居页面顶部距离,锚点定位并距离顶部一定偏移
出入库与库存系统的模型问题
千呼万唤始出来
excel里有文字的数字怎么从小到大排序,而不是首数字排序
FOC single resistance sampling position loop control servo motor
提交本地仓库并同步码云仓库