Skip to content

Commit be4c76d

Browse files
rejacobsonmgol
authored andcommitted
Core: Add support for hex colors with alpha transparency
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
1 parent 2769d8e commit be4c76d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

jquery.color.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@
5353
}
5454
}, {
5555

56+
// this regex ignores A-F because it's compared against an already lowercased string
57+
re: /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/,
58+
parse: function( execResult ) {
59+
return [
60+
parseInt( execResult[ 1 ], 16 ),
61+
parseInt( execResult[ 2 ], 16 ),
62+
parseInt( execResult[ 3 ], 16 ),
63+
( parseInt( execResult[ 4 ], 16 ) / 255.0 ).toFixed( 2 )
64+
];
65+
}
66+
}, {
67+
5668
// this regex ignores A-F because it's compared against an already lowercased string
5769
re: /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/,
5870
parse: function( execResult ) {

test/unit/color.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ var sevens = {
170170
};
171171
parseTest( "#777", sevens );
172172
parseTest( "#777777", sevens );
173+
parseTest( "#77777726", {
174+
expect: 4,
175+
red: 119,
176+
green: 119,
177+
blue: 119,
178+
alpha: 0.15
179+
} );
180+
parseTest( "#777777FF", {
181+
expect: 4,
182+
red: 119,
183+
green: 119,
184+
blue: 119,
185+
alpha: 1
186+
} );
187+
parseTest( "#77777700", {
188+
expect: 4,
189+
red: 119,
190+
green: 119,
191+
blue: 119,
192+
alpha: 0
193+
} );
173194

174195
var fiftypercent = {
175196
expect: 4,

0 commit comments

Comments
 (0)