Hexadecimal Color Inverter


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

By Matt Lagrandeur


Copy this code and paste it in your HTML
  1. function invertHex(hexnum){
  2. if(hexnum.length != 6) {
  3. alert("Hex color must be six hex numbers in length.");
  4. return false;
  5. }
  6.  
  7. hexnum = hexnum.toUpperCase();
  8. var splitnum = hexnum.split("");
  9. var resultnum = "";
  10. var simplenum = "FEDCBA9876".split("");
  11. var complexnum = new Array();
  12. complexnum.A = "5";
  13. complexnum.B = "4";
  14. complexnum.C = "3";
  15. complexnum.D = "2";
  16. complexnum.E = "1";
  17. complexnum.F = "0";
  18.  
  19. for(i=0; i<6; i++){
  20. if(!isNaN(splitnum[i])) {
  21. resultnum += simplenum[splitnum[i]];
  22. } else if(complexnum[splitnum[i]]){
  23. resultnum += complexnum[splitnum[i]];
  24. } else {
  25. alert("Hex colors must only include hex numbers 0-9, and A-F");
  26. return false;
  27. }
  28. }
  29.  
  30. return resultnum;
  31. }

URL: http://www.mattlag.com/scripting/hexcolorinverter.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.