Skip to content

Commit c362f7c

Browse files
author
Hannah
authored
Merge pull request #8873 from vrana/patch-8
Support #rgba and #rrggbbaa in Color.fromCssColorString
2 parents 43c6aec + 46fc3ee commit c362f7c

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
@@ -14,6 +14,7 @@
1414
- Added `SkyAtmosphere.perFragmentAtmosphere` to switch between per-vertex and per-fragment atmosphere shading. [#8866](https://github.com/CesiumGS/cesium/pull/8866)
1515
- Added `Globe.undergroundColor` and `Globe.undergroundColorAlphaByDistance` for controlling how the back side of the globe is rendered when the camera is underground or the globe is translucent. [#8867](https://github.com/CesiumGS/cesium/pull/8867)
1616
- Added a new sandcastle example to show how to add fog using a `PostProcessStage` [#8798](https://github.com/CesiumGS/cesium/pull/8798)
17+
- Supported `#rgba` and `#rrggbbaa` formats in `Color.fromCssColorString`. [8873](https://github.com/CesiumGS/cesium/pull/8873)
1718

1819
##### Fixes :wrench:
1920

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
@@ -184,7 +184,19 @@ describe("Core/Color", function () {
184184
expect(Color.fromCssColorString("#00F")).toEqual(Color.BLUE);
185185
});
186186

187-
it("fromCssColorString supports the #rrggbb", function () {
187+
it("fromCssColorString supports the #rgba format", function () {
188+
expect(Color.fromCssColorString("#369c")).toEqual(
189+
new Color(0.2, 0.4, 0.6, 0.8)
190+
);
191+
});
192+
193+
it("fromCssColorString supports the #rgba format with uppercase", function () {
194+
expect(Color.fromCssColorString("#369C")).toEqual(
195+
new Color(0.2, 0.4, 0.6, 0.8)
196+
);
197+
});
198+
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
);
@@ -202,6 +214,18 @@ describe("Core/Color", function () {
202214
expect(Color.fromCssColorString("#0000FF")).toEqual(Color.BLUE);
203215
});
204216

217+
it("fromCssColorString supports the #rrggbbaa format", function () {
218+
expect(Color.fromCssColorString("#336699cc")).toEqual(
219+
new Color(0.2, 0.4, 0.6, 0.8)
220+
);
221+
});
222+
223+
it("fromCssColorString supports the #rrggbbaa format with uppercase", function () {
224+
expect(Color.fromCssColorString("#336699CC")).toEqual(
225+
new Color(0.2, 0.4, 0.6, 0.8)
226+
);
227+
});
228+
205229
it("fromCssColorString supports the rgb() format with absolute values", function () {
206230
expect(Color.fromCssColorString("rgb(255, 0, 0)")).toEqual(Color.RED);
207231
expect(Color.fromCssColorString("rgb(0, 255, 0)")).toEqual(Color.LIME);

0 commit comments

Comments
 (0)