当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
New type of dark energy could solve Universe expansion mystery
JS中的this指向
微信小程序之 js 时间戳/1000 转换 秒,六个小时后,一天后,本周五 选项计算时间
C# webpai 路由详解
1-3 NodeJS的安装之清单配置与跑项目环境
Router对象、Route对象、声明式导航、编程式导航
1-4 NodeJS的安装之配置可执行脚本
Arm common assembly instructions
FOC single resistance sampling position loop control servo motor
C# Dapper 基本使用 增删改查事务等
Node accesses server-side static resources
ECMAScript历史
Makefile foundation, common functions and general makefile
Use of C language and
几款笔记软件的优缺点
特效案例收集:鼠标星球小尾巴
ASP.NET CORE配置选项(下篇)
C# Task.Delay和Thread.Sleep的区别
查漏补缺(七)
关于软件的空间占用,安装目录