JavaScript implements web color value conversion

Source: Internet
Author: User

I've been busy with business requirements lately and haven't been blogging for a long time. A little bit of time today, looking at some of the recent project's front-end code, see the Web color conversion function, suddenly think that when we do some color settings/editing requirements, often involves a variety of color value format interchange. So I decided to record how I do this part of the function of the time is how to achieve, write down and share with you, I hope readers can express their views, a lot of communication.

First look at the problem

Problem one, when we are developing the Web front page, we often use Dom.style.backgroundColor = "#f00" to set the background color of a DOM element, and also by similar (why is it similar?). There are more situations where you can freely imagine the var BGC = Dom.style.backgroundColor code to get the background color of a DOM element. So here's the question, see:

If the contrast here is not obvious enough, let's continue looking down:

It is clear that the same color value should have been equal, but the result is not. This is not the case, the author in the Chrome development tools and Firefox console, the results are consistent. Issue two, front-end development work, often starting with a restore UI design. And in the coding process we often find that the design: a box background solid color (assuming: #f00), but with 75% opacity. Obviously, we can't set this situation simply by Dom.style.backgroundColor = "#f00" because it doesn't achieve the translucent effect. Changed, we know that there is an RGBA in CSS3, which means that we can set a translucent background color by Dom.style.backgroundColor = "Rgba (255, 0, 0, 0.75)" . So here's the problem: This conversion is easy to do in Photoshop, but how do we convert ("#f00", 75) to Rgba (255, 0, 0, 0.75) in JavaScript? Next, let's take a look at how I do it.

RGB (a) color value turns to hex color (hex)

is to do the development, I understand! It's better to go straight to the code, but here's the original one:

1 varRgbtohex =function(RGB) {2     varRrgb =/rgb\ ((\d{1,3}), (\d{1,3}), (\d{1,3}) \)/,3Rrgba =/rgba\ ((\d{1,3}), (\d{1,3}), (\d{1,3}), ([. \d]+) \)/,4R, G, B, a, rs = Rgb.replace (/\s+/g, ""). Match (RRGB),5RSA = Rgb.replace (/\s+/g, ""). Match (Rrgba);6     if(RS) {7r = (+rs[1]). toString (16);8r = R.length = = 1? "0" +r:r;9g = (+rs[2]). toString (16);TenG = G.length = = 1? "0" +g:g; Oneb = (+rs[3]). toString (16); Ab = B.length = = 1? "0" +b:b; -         return{hex: "#" + r + G + B, alpha:100}; -}Else if(RSA) { ther = (+rsa[1]). toString (16); -r = R.length = = 1? "0" +r:r; -g = (+rsa[2]). toString (16); -G = G.length = = 1? "0" +g:g; +b = (+rsa[3]). toString (16); -b = B.length = = 1? "0" +b:b; +A = (+rsa[4]) * 100 A         return{hex: "#" + r + G +B, Alpha:Math.ceil (A)}; at}Else { -         return{Hex:rgb, alpha:100}; -     } -};

Why is that the most primitive? Because when I review the code today, I find that there is room for evolution, and then compare the code after Evolution (optimization):

1 varRgbtohex =function(RGB) {2     varRrgba =/rgba?\ ((\d{1,3}), (\d{1,3}), (\d{1,3}) (, ([. \d]+))? \)/,3 R, G, B, A,4RSA = Rgb.replace (/\s+/g, ""). Match (Rrgba);5     if(RSA) {6r = (+rsa[1]). toString (16);7r = R.length = = 1? "0" +r:r;8g = (+rsa[2]). toString (16);9G = G.length = = 1? "0" +g:g;Tenb = (+rsa[3]). toString (16); Oneb = B.length = = 1? "0" +b:b; AA = (+ (rsa[5]? Rsa[5]: 1)) * 100 -         return{hex: "#" + r + G +B, Alpha:Math.ceil (A)}; -}Else { the         return{Hex:rgb, alpha:100}; -     } -};

And do not say that less an if branch, single from the code to see, it is very obvious! Next, let's see if the results of the transformation are as we wish, and for this I executed the lines of code shown in the console:

Judging from the results of the implementation, our approach seems to have been able to achieve our goal. However, careful friends should notice that there are two red arrows in the picture, is there any pit here? Not bad. We take a closer look at the color parameters passed in the first arrow RGB (255, 0, 0, 2) , in fact, here is not a valid color value, RGB format color value, there is no fourth (transparency) parameter; Look at the second arrow in rgba (255, 0, 0, 1.48) , where the format is no problem, but the transparency is 1.48, in fact, is not a legal transparency value. Both cases, our methods are normal execution, and return to the normal, indicating that our approach and further evolution of space, will be handed to everyone to play!

Hex Color (hex) turns into RGBA format

In daily development, the color value we use most often should be the color value in hexadecimal format (#ff0000, #f00等), what should we do if we need to convert to RGBA format when we use color values?

1 varHextorgba =function(Hex, AL) {2     varHexcolor =/^#/.test (hex)? Hex.slice (1): Hex,3ALP = Hex = = = ' Transparent '? 0: Math.ceil (AL),4 R, G, B;5Hexcolor =/^[0-9a-f]{3}| [0-9a-f] {6}$/i.test (hexcolor)? Hexcolor: ' Fffff ';6     if(Hexcolor.length = = 3) {7Hexcolor = Hexcolor.replace (/(\w) (\w) (\w)/gi, ' $1$1$2$2$3$3 ');8     }9r = Hexcolor.slice (0, 2);Teng = Hexcolor.slice (2, 4); Oneb = Hexcolor.slice (4, 6); AR = parseint (R, 16); -g = parseint (g, 16); -b = parseint (b, 16); the     return { -Hex: ' # ' +Hexcolor, - Alpha:alp, -RGBA: ' Rgba (' + R + ', ' + G + ', ' + B + ', ' + (alp/100). toFixed (2) + ') ' +     }; -};

Similarly, we also write a verification code to test that our conversion is normal:

Judging from the results of the implementation, our approach, no problem, can get the results we want to convert. But here is still left to everyone two red arrows, illegal transparency and illegal color values. This part of the evolutionary function is also left to everyone, haha ... Finally, the page color values between the conversion, in fact, is a cliché of the problem, I here is just a simple list of one, I believe there are more and better ways to use, welcome everyone to put forward, we exchange, common progress ~ ~

JavaScript implements web color value conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.