Mostrando las entradas con la etiqueta rgb. Mostrar todas las entradas
Mostrando las entradas con la etiqueta rgb. Mostrar todas las entradas

jueves, 11 de abril de 2013

Convertir de RGB a Hexadecimal en Javascript

Convertir de RGB a Hexadecimal en Javascript

Copiamos el siguiente Script en un archivo llamado color.js y lo incluyen dentro de la etiqueta head del HTML principal:

color.js

#Developer

 1 _rgb = function(rgb){
 2     var hexDigits = new Array ("0","1","2","3","4","5","6",
 3     "7","8","9","a","b","c","d","e","f");
 4 
 5  return {
 6    toHex: function(rgb){
 7     rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 8     return "#" + _rgb.hex(rgb[1]) + _rgb.hex(rgb[2]) + _rgb.
 9     hex(rgb[3]);
10    },
11    hex: function(x){
12     return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + 
13     hexDigits[x % 16];
14    },
15  };
16 }();

#Production

 1 _rgb=function(e){var t=new Array("0","1","2","3","4","5","6",
 2 "7","8","9","a","b","c","d","e","f");return{toHex:function(e)
 3 {e=e.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);return"#"+
 4 _rgb.hex(e[1])+_rgb.hex(e[2])+_rgb.hex(e[3])},hex:function(e)
 5 {return isNaN(e)?"00":t[(e-e%16)/16]+t[e%16]}}}();

index.html

 1 <script type="text/javascript">
 2  alert(_rgb.toHex('rgb(255,0,255)'))
 3 </script>

Y listo eso es todo!.