Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0e58cc1

Browse files
committedMay 20, 2020
Support #rgba and #rrggbbaa in Color.fromCssColorString
1 parent 9eaf6a8 commit 0e58cc1

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed
 

‎CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Added `Cesium3DTileset.extensions` to get the extensions property from the tileset JSON. [#8829](https://github.com/CesiumGS/cesium/pull/8829)
88
- Added `frustumSplits` option to `DebugCameraPrimitive`. [8849](https://github.com/CesiumGS/cesium/pull/8849)
9+
- Supported `#rgba` and `#rrggbbaa` formats in `Color.fromCssColorString`. [8873](https://github.com/CesiumGS/cesium/pull/8873)
910

1011
##### Fixes :wrench:
1112

‎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

‎Specs/Core/ColorSpec.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ describe("Core/Color", function () {
172172
);
173173
});
174174

175+
it("fromCssColorString supports the #rgba format", function () {
176+
expect(Color.fromCssColorString("#369c")).toEqual(
177+
new Color(0.2, 0.4, 0.6, 0.8)
178+
);
179+
});
180+
181+
it("fromCssColorString supports the #rgba format with uppercase", function () {
182+
expect(Color.fromCssColorString("#369C")).toEqual(
183+
new Color(0.2, 0.4, 0.6, 0.8)
184+
);
185+
});
186+
175187
it("fromCssColorString supports the #rgb format with lowercase", function () {
176188
expect(Color.fromCssColorString("#f00")).toEqual(Color.RED);
177189
expect(Color.fromCssColorString("#0f0")).toEqual(Color.LIME);
@@ -184,12 +196,24 @@ describe("Core/Color", function () {
184196
expect(Color.fromCssColorString("#00F")).toEqual(Color.BLUE);
185197
});
186198

187-
it("fromCssColorString supports the #rrggbb", function () {
199+
it("fromCssColorString supports the #rrggbb format", function () {
188200
expect(Color.fromCssColorString("#336699")).toEqual(
189201
new Color(0.2, 0.4, 0.6, 1.0)
190202
);
191203
});
192204

205+
it("fromCssColorString supports the #rrggbbaa format", function () {
206+
expect(Color.fromCssColorString("#336699cc")).toEqual(
207+
new Color(0.2, 0.4, 0.6, 0.8)
208+
);
209+
});
210+
211+
it("fromCssColorString supports the #rrggbbaa format with uppercase", function () {
212+
expect(Color.fromCssColorString("#336699CC")).toEqual(
213+
new Color(0.2, 0.4, 0.6, 0.8)
214+
);
215+
});
216+
193217
it("fromCssColorString supports the #rrggbb format with lowercase", function () {
194218
expect(Color.fromCssColorString("#ff0000")).toEqual(Color.RED);
195219
expect(Color.fromCssColorString("#00ff00")).toEqual(Color.LIME);

0 commit comments

Comments
 (0)
Please sign in to comment.