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

Add Color.toCssHexString and unit tests #8987

Merged
merged 4 commits into from
Jun 24, 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 @@ -8,6 +8,7 @@

##### Additions :tada:

- Added `Color.toCssHexString` for getting the CSS hex string equivalent for a color. [#8987](https://github.com/CesiumGS/cesium/pull/8987)
- Added support for PolylineVolume in CZML [#8841](https://github.com/CesiumGS/cesium/pull/8841)

##### Fixes :wrench:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
- [Sam Suhag](https://github.com/sanjeetsuhag)
- [Youssef Victor](https://github.com/YoussefV)
- [Eli Bogomolny](https://github.com/ebogo1)
- [Erixen Cruz](https://github.com/ErixenCruz)
- [Northrop Grumman](http://www.northropgrumman.com)
- [Joseph Stein](https://github.com/nahgrin)
- [EOX IT Services GmbH](https://eox.at)
Expand Down
28 changes: 28 additions & 0 deletions Source/Core/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,34 @@ Color.prototype.toCssColorString = function () {
return "rgba(" + red + "," + green + "," + blue + "," + this.alpha + ")";
};

/**
* Creates a string containing CSS hex string color value for this color.
*
* @returns {String} The CSS hex string equivalent of this color.
*/
Color.prototype.toCssHexString = function () {
var r = Color.floatToByte(this.red).toString(16);
if (r.length < 2) {
r = "0" + r;
}
var g = Color.floatToByte(this.green).toString(16);
if (g.length < 2) {
g = "0" + g;
}
var b = Color.floatToByte(this.blue).toString(16);
if (b.length < 2) {
b = "0" + b;
}
if (this.alpha < 1) {
var hexAlpha = Color.floatToByte(this.alpha).toString(16);
if (hexAlpha.length < 2) {
hexAlpha = "0" + hexAlpha;
}
return "#" + r + g + b + hexAlpha;
}
return "#" + r + g + b;
};

/**
* Converts this color to an array of red, green, blue, and alpha values
* that are in the range of 0 to 255.
Expand Down
9 changes: 9 additions & 0 deletions Specs/Core/ColorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ describe("Core/Color", function () {
);
});

it("toCssHexString produces expected output", function () {
expect(Color.WHITE.toCssHexString()).toEqual("#ffffff");
expect(Color.RED.toCssHexString()).toEqual("#ff0000");
expect(Color.BLUE.toCssHexString()).toEqual("#0000ff");
expect(Color.LIME.toCssHexString()).toEqual("#00ff00");
expect(new Color(0.0, 0.0, 0.0, 1.0).toCssHexString()).toEqual("#000000");
expect(new Color(0.1, 0.2, 0.3, 0.4).toCssHexString()).toEqual("#19334c66");
});

it("fromCssColorString supports transparent", function () {
expect(Color.fromCssColorString("transparent")).toEqual(
new Color(0.0, 0.0, 0.0, 0.0)
Expand Down