Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Created June 19, 2012 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tcelestino/2954305 to your computer and use it in GitHub Desktop.
Save tcelestino/2954305 to your computer and use it in GitHub Desktop.
Hexdecimal Contraction
//authors: Diego Fleury and Diego Nunes
function hexContraction(color) {
var regexp = /#?([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3/i;
return '#'+ color.replace(regexp, '$1$2$3') || '#'+ color.replace('#', '');
}
hexContraction('aabbcc'); //#abc
hexContraction('#AD8634'); //'#AD8634
hexContraction('aaddc4'); //#aaddc4
@dnunes
Copy link

dnunes commented Jun 19, 2012

O return do final poderia ser simplificado com...

return '#'+ (color.replace(regexp, '$1$2$3') || color.replace('#', ''));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment