Skip to content

Commit

Permalink
Core: Add support for hex colors with alpha transparency
Browse files Browse the repository at this point in the history
Hex colors with alpha transparency are supported in Firefox and Chrome,
at least, perhaps others as well:

  eg. #0099bbff (is #0099bb color with ff alpha)

However, jquery-color would convert this hex value to an rgb value
losing the alpha transparency:

  eg. rgb(0, 153, 187)

Fixes #129
Closes #126
  • Loading branch information
rejacobson authored and mgol committed May 6, 2020
1 parent 2769d8e commit be4c76d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions jquery.color.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
}
}, {

// this regex ignores A-F because it's compared against an already lowercased string
re: /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/,
parse: function( execResult ) {
return [
parseInt( execResult[ 1 ], 16 ),
parseInt( execResult[ 2 ], 16 ),
parseInt( execResult[ 3 ], 16 ),
( parseInt( execResult[ 4 ], 16 ) / 255.0 ).toFixed( 2 )
];
}
}, {

// this regex ignores A-F because it's compared against an already lowercased string
re: /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/,
parse: function( execResult ) {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ var sevens = {
};
parseTest( "#777", sevens );
parseTest( "#777777", sevens );
parseTest( "#77777726", {
expect: 4,
red: 119,
green: 119,
blue: 119,
alpha: 0.15
} );
parseTest( "#777777FF", {
expect: 4,
red: 119,
green: 119,
blue: 119,
alpha: 1
} );
parseTest( "#77777700", {
expect: 4,
red: 119,
green: 119,
blue: 119,
alpha: 0
} );

var fiftypercent = {
expect: 4,
Expand Down

0 comments on commit be4c76d

Please sign in to comment.