Skip to content

Commit 2d1a90b

Browse files
committed
Core: Add support for hex colors with alpha transparency - short version
This adds support for the short version of hex colors with alpha transparency: eg. #09be (is #09b color with e alpha) However, jquery-color would convert this hex value to an rgb value losing the alpha transparency: eg. rgb(0, 153, 187) Ref #129 Ref #126
1 parent b32d620 commit 2d1a90b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

jquery.color.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@
7676
}
7777
}, {
7878

79+
// this regex ignores A-F because it's compared against an already lowercased string
80+
re: /#([a-f0-9])([a-f0-9])([a-f0-9])([a-f0-9])/,
81+
parse: function( execResult ) {
82+
return [
83+
parseInt( execResult[ 1 ] + execResult[ 1 ], 16 ),
84+
parseInt( execResult[ 2 ] + execResult[ 2 ], 16 ),
85+
parseInt( execResult[ 3 ] + execResult[ 3 ], 16 ),
86+
( parseInt( execResult[ 4 ] + execResult[ 4 ], 16 ) / 255 )
87+
.toFixed( 2 )
88+
];
89+
}
90+
}, {
91+
7992
// this regex ignores A-F because it's compared against an already lowercased string
8093
re: /#([a-f0-9])([a-f0-9])([a-f0-9])/,
8194
parse: function( execResult ) {

test/unit/color.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ parseTest( "#77777700", {
192192
alpha: 0
193193
} );
194194

195+
parseTest( "#7776", {
196+
expect: 4,
197+
red: 119,
198+
green: 119,
199+
blue: 119,
200+
alpha: 0.4
201+
} );
202+
parseTest( "#777F", {
203+
expect: 4,
204+
red: 119,
205+
green: 119,
206+
blue: 119,
207+
alpha: 1
208+
} );
209+
parseTest( "#7770", {
210+
expect: 4,
211+
red: 119,
212+
green: 119,
213+
blue: 119,
214+
alpha: 0
215+
} );
216+
195217
var fiftypercent = {
196218
expect: 4,
197219
red: 127,

0 commit comments

Comments
 (0)