Convert RGB to Hexadecimal using JavaScript


/ Published in: JavaScript
Save to your folder(s)

You have a RGB value and want to convert it into Hexadecimal mode, here’s how to do it using JavaScript.


Copy this code and paste it in your HTML
  1. function Rgb() {
  2. var red = returnHex(200);
  3. var green = returnHex(255);
  4. var blue = returnHex(155);
  5. var hex = "#" + red + green + blue;
  6.  
  7. alert(hex);
  8. }
  9.  
  10. // Convert to Hex
  11. function returnHex(num) {
  12. // Hex can store 16 different values in 1 character
  13. if (num == null) return "00";
  14. num = num.length < 2 ? "0" + num : num
  15. return num.toString(16);
  16. }

URL: http://www.devcurry.com/2010/08/convert-rgb-to-hexadecimal-using.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.