Skip to content

Commit da23b10

Browse files
authored
Support #rgba and #rrggbbaa
1 parent 9eaf6a8 commit da23b10

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Source/Core/Color.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,19 @@ Color.fromRandom = function (options, result) {
347347
return result;
348348
};
349349

350-
//#rgb
351-
var rgbMatcher = /^#([0-9a-f])([0-9a-f])([0-9a-f])$/i;
352-
//#rrggbb
353-
var rrggbbMatcher = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i;
350+
//#rgba
351+
var rgbaMatcher = /^#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i;
352+
//#rrggbbaa
353+
var rrggbbaaMatcher = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i;
354354
//rgb(), rgba(), or rgb%()
355355
var rgbParenthesesMatcher = /^rgba?\(\s*([0-9.]+%?)\s*,\s*([0-9.]+%?)\s*,\s*([0-9.]+%?)(?:\s*,\s*([0-9.]+))?\s*\)$/i;
356-
//hsl(), hsla(), or hsl%()
356+
//hsl() or hsla()
357357
var hslParenthesesMatcher = /^hsla?\(\s*([0-9.]+)\s*,\s*([0-9.]+%)\s*,\s*([0-9.]+%)(?:\s*,\s*([0-9.]+))?\s*\)$/i;
358358

359359
/**
360360
* Creates a Color instance from a CSS color value.
361361
*
362-
* @param {String} color The CSS color value in #rgb, #rrggbb, rgb(), rgba(), hsl(), or hsla() format.
362+
* @param {String} color The CSS color value in #rgb, #rgba, #rrggbb, #rrggbbaa, rgb(), rgba(), hsl(), or hsla() format.
363363
* @param {Color} [result] The object to store the result in, if undefined a new instance will be created.
364364
* @returns {Color} The color object, or undefined if the string was not a valid CSS color.
365365
*
@@ -385,21 +385,21 @@ Color.fromCssColorString = function (color, result) {
385385
return result;
386386
}
387387

388-
var matches = rgbMatcher.exec(color);
388+
var matches = rgbaMatcher.exec(color);
389389
if (matches !== null) {
390390
result.red = parseInt(matches[1], 16) / 15;
391391
result.green = parseInt(matches[2], 16) / 15.0;
392392
result.blue = parseInt(matches[3], 16) / 15.0;
393-
result.alpha = 1.0;
393+
result.alpha = parseInt(defaultValue(matches[4], "f"), 16) / 15.0;
394394
return result;
395395
}
396396

397-
matches = rrggbbMatcher.exec(color);
397+
matches = rrggbbaaMatcher.exec(color);
398398
if (matches !== null) {
399399
result.red = parseInt(matches[1], 16) / 255.0;
400400
result.green = parseInt(matches[2], 16) / 255.0;
401401
result.blue = parseInt(matches[3], 16) / 255.0;
402-
result.alpha = 1.0;
402+
result.alpha = parseInt(defaultValue(matches[4], "ff"), 16) / 255.0;
403403
return result;
404404
}
405405

0 commit comments

Comments
 (0)