Return to Snippet

Revision: 19888
at November 3, 2009 04:44 by joshuabaker


Initial Code
/**
 * jQuery.rgbToHex - Converts an RGB string to a HEX string (forces length 6)
 * @author Joshua Baker
 * @version 1.0.0
 */
;(function($){
  $.extend({
    rgbToHex: function(rgbString) {
      var parts = rgbString.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
      if (!parts)
      {
        if (rgbString.length < 6) {
          var parts = rgbString.split('');
          delete (parts[0]);
          for (var i = 1; i <= 3; ++i)
          {
            if (parts[i].length == 1) parts[i] = parts[i] + parts[i];
          }
        }
      }
      else
      {
        delete (parts[0]);
        for (var i = 1; i <= 3; ++i)
        {
          parts[i] = parseInt(parts[i]).toString(16);
          if (parts[i].length == 1) parts[i] = parts[i] + parts[i];
        }
      }
      return '#' + parts.join('');
    }
  });
})(jQuery);



/**
 * jQuery.rgbToHex - Converts an RGB string to a HEX string (forces length 6)
 * @author Joshua Baker
 * @version 1.0.0
 */
;(function(a){a.extend({rgbToHex:function(d){var c=d.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);if(!c){if(d.length<6){var c=d.split("");delete (c[0]);for(var b=1;b<=3;++b){if(c[b].length==1){c[b]=c[b]+c[b]}}}}else{delete (c[0]);for(var b=1;b<=3;++b){c[b]=parseInt(c[b]).toString(16);if(c[b].length==1){c[b]=c[b]+c[b]}}}return"#"+c.join("")}})})(jQuery);

Initial URL

                                

Initial Description

                                

Initial Title
jQuery.rgbToHex

Initial Tags
jquery, color

Initial Language
jQuery