Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support #rgba and #rrggbbaa in Color.fromCssColorString #8873

Merged
merged 2 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added `SkyAtmosphere.perFragmentAtmosphere` to switch between per-vertex and per-fragment atmosphere shading. [#8866](https://github.com/CesiumGS/cesium/pull/8866)
- 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)
- Added a new sandcastle example to show how to add fog using a `PostProcessStage` [#8798](https://github.com/CesiumGS/cesium/pull/8798)
- Supported `#rgba` and `#rrggbbaa` formats in `Color.fromCssColorString`. [8873](https://github.com/CesiumGS/cesium/pull/8873)

##### Fixes :wrench:

Expand Down
20 changes: 10 additions & 10 deletions Source/Core/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,19 @@ Color.fromRandom = function (options, result) {
return result;
};

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

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

var matches = rgbMatcher.exec(color);
var matches = rgbaMatcher.exec(color);
if (matches !== null) {
result.red = parseInt(matches[1], 16) / 15;
result.green = parseInt(matches[2], 16) / 15.0;
result.blue = parseInt(matches[3], 16) / 15.0;
result.alpha = 1.0;
result.alpha = parseInt(defaultValue(matches[4], "f"), 16) / 15.0;
return result;
}

matches = rrggbbMatcher.exec(color);
matches = rrggbbaaMatcher.exec(color);
if (matches !== null) {
result.red = parseInt(matches[1], 16) / 255.0;
result.green = parseInt(matches[2], 16) / 255.0;
result.blue = parseInt(matches[3], 16) / 255.0;
result.alpha = 1.0;
result.alpha = parseInt(defaultValue(matches[4], "ff"), 16) / 255.0;
return result;
}

Expand Down
26 changes: 25 additions & 1 deletion Specs/Core/ColorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,19 @@ describe("Core/Color", function () {
expect(Color.fromCssColorString("#00F")).toEqual(Color.BLUE);
});

it("fromCssColorString supports the #rrggbb", function () {
it("fromCssColorString supports the #rgba format", function () {
expect(Color.fromCssColorString("#369c")).toEqual(
new Color(0.2, 0.4, 0.6, 0.8)
);
});

it("fromCssColorString supports the #rgba format with uppercase", function () {
expect(Color.fromCssColorString("#369C")).toEqual(
new Color(0.2, 0.4, 0.6, 0.8)
);
});

it("fromCssColorString supports the #rrggbb format", function () {
expect(Color.fromCssColorString("#336699")).toEqual(
new Color(0.2, 0.4, 0.6, 1.0)
);
Expand All @@ -202,6 +214,18 @@ describe("Core/Color", function () {
expect(Color.fromCssColorString("#0000FF")).toEqual(Color.BLUE);
});

it("fromCssColorString supports the #rrggbbaa format", function () {
expect(Color.fromCssColorString("#336699cc")).toEqual(
new Color(0.2, 0.4, 0.6, 0.8)
);
});

it("fromCssColorString supports the #rrggbbaa format with uppercase", function () {
expect(Color.fromCssColorString("#336699CC")).toEqual(
new Color(0.2, 0.4, 0.6, 0.8)
);
});

it("fromCssColorString supports the rgb() format with absolute values", function () {
expect(Color.fromCssColorString("rgb(255, 0, 0)")).toEqual(Color.RED);
expect(Color.fromCssColorString("rgb(0, 255, 0)")).toEqual(Color.LIME);
Expand Down