@@ -47,8 +47,9 @@ tinycolor.prototype = {
4747//
4848// "red"
4949// "#f00" or "f00"
50+ // "#f00f" or "f00f"
5051// "#ff0000" or "ff0000"
51- // "#ff000000 " or "ff000000 "
52+ // "#ff0000ff " or "ff0000ff "
5253// "rgb 255 0 0" or "rgb (255, 0, 0)"
5354// "rgb 1.0 0 0" or "rgb (1, 0, 0)"
5455// "rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1"
@@ -389,6 +390,11 @@ function convertToPercentage(n) {
389390 return n ;
390391}
391392
393+ // Converts a hex value to a decimal
394+ function convertHexToDecimal ( h ) {
395+ return ( parseIntFromHex ( h ) / 255 ) ;
396+ }
397+
392398var matchers = ( function ( ) {
393399 // <http://www.w3.org/TR/css3-values/#integers>
394400 var CSS_INTEGER = "[-\\+]?\\d+%?" ;
@@ -413,7 +419,9 @@ var matchers = (function() {
413419 hsv : new RegExp ( "hsv" + PERMISSIVE_MATCH3 ) ,
414420 hsva : new RegExp ( "hsva" + PERMISSIVE_MATCH4 ) ,
415421 hex3 : / ^ ( [ 0 - 9 a - f A - F ] { 1 } ) ( [ 0 - 9 a - f A - F ] { 1 } ) ( [ 0 - 9 a - f A - F ] { 1 } ) $ / ,
422+ hex4 : / ^ ( [ 0 - 9 a - f A - F ] { 1 } ) ( [ 0 - 9 a - f A - F ] { 1 } ) ( [ 0 - 9 a - f A - F ] { 1 } ) ( [ 0 - 9 a - f A - F ] { 1 } ) $ / ,
416423 hex6 : / ^ ( [ 0 - 9 a - f A - F ] { 2 } ) ( [ 0 - 9 a - f A - F ] { 2 } ) ( [ 0 - 9 a - f A - F ] { 2 } ) $ / ,
424+ hex8 : / ^ ( [ 0 - 9 a - f A - F ] { 2 } ) ( [ 0 - 9 a - f A - F ] { 2 } ) ( [ 0 - 9 a - f A - F ] { 2 } ) ( [ 0 - 9 a - f A - F ] { 2 } ) $ / ,
417425 } ;
418426} ) ( ) ;
419427
@@ -454,6 +462,15 @@ function stringInputToObject(color) {
454462 if ( ( match = matchers . hsva . exec ( color ) ) ) {
455463 return { h : match [ 1 ] , s : match [ 2 ] , v : match [ 3 ] , a : match [ 4 ] } ;
456464 }
465+ if ( ( match = matchers . hex8 . exec ( color ) ) ) {
466+ return {
467+ r : parseIntFromHex ( match [ 1 ] ) ,
468+ g : parseIntFromHex ( match [ 2 ] ) ,
469+ b : parseIntFromHex ( match [ 3 ] ) ,
470+ a : convertHexToDecimal ( match [ 4 ] ) ,
471+ format : named ? "name" : "hex"
472+ } ;
473+ }
457474 if ( ( match = matchers . hex6 . exec ( color ) ) ) {
458475 return {
459476 r : parseIntFromHex ( match [ 1 ] ) ,
@@ -462,6 +479,15 @@ function stringInputToObject(color) {
462479 format : named ? "name" : "hex"
463480 } ;
464481 }
482+ if ( ( match = matchers . hex4 . exec ( color ) ) ) {
483+ return {
484+ r : parseIntFromHex ( match [ 1 ] + '' + match [ 1 ] ) ,
485+ g : parseIntFromHex ( match [ 2 ] + '' + match [ 2 ] ) ,
486+ b : parseIntFromHex ( match [ 3 ] + '' + match [ 3 ] ) ,
487+ a : convertHexToDecimal ( match [ 4 ] + '' + match [ 4 ] ) ,
488+ format : named ? "name" : "hex"
489+ } ;
490+ }
465491 if ( ( match = matchers . hex3 . exec ( color ) ) ) {
466492 return {
467493 r : parseIntFromHex ( match [ 1 ] + '' + match [ 1 ] ) ,
0 commit comments