WebWorldWind/test/util/Color.test.js
Zach Glueckert 03ffcc7393 Update CSS Color String calculation and use (#474)
* Add deprecation warning to toHexString

* Correct CSS color string generation and rename method
- Switch to `round` operation for float to 16 bit conversion

* Update comments

* Update method name

* Add CSS Color String generation unit test

* Add CSS color string conversion unit test and format files

* Add CSS color string conversion unit test and format files

* Fix Color unit tests

* Add CSS color string conversion test

* Switch to colorFromBytes function and fix precision error
2018-02-20 13:33:23 -08:00

78 lines
3.3 KiB
JavaScript

/*
* Copyright 2015-2017 WorldWind Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require([
'src/util/Color'
], function (Color) {
"use strict";
describe("Color-colorFromHex", function () {
it("testValidWhiteHex", function () {
var result = Color.colorFromHex("ffffffff");
expect(result.equals(new Color(1, 1, 1, 1))).toBe(true);
});
it("testValidBlackHex", function () {
var result = Color.colorFromHex("000000ff");
expect(result.equals(new Color(0, 0, 0, 1))).toBe(true);
});
});
describe("Converts Color to CSS Compatible Color String", function () {
// convert CSS Color string to byte array
var re = /\d+(\.\d+)?/g; // pattern to extract CSS color values from a string
var cssStringParse = function (cssString) {
// parse the integer values from the css string
var bytes = [];
var match;
while ((match = re.exec(cssString)) != null) {
bytes.push(match[0]);
}
return bytes;
};
it("should round to the same integer values", function () {
var r = 0.25, g = 0.07, b = 0.75, a = 0.5; // values which test the rounding scheme
var result = new Color(r, g, b, a).toCssColorString();
expect(result === "rgba(64, 18, 191, 0.5)").toBe(true);
});
it("should convert from CSS String to the matching byte value", function () {
var color = new Color(0, 0, 0, 1); // initial color to start testing (similar to DrawContext pickColor)
var tests = 256 * 256; // test two bands of unique pick colors, the complete three band range takes too long
for (var i = 0; i < tests; i++) {
var startColor = color.nextColor().clone();
var colorValues = cssStringParse(startColor.toCssColorString());
var convertedColor = Color.colorFromBytes(colorValues[0], colorValues[1], colorValues[2], colorValues[3] * 255);
expect(startColor.equals(convertedColor)).toBe(true);
}
});
it("should convert from CSS back to the original value within css 8 bit precision", function () {
var err = 1 / 510;
for (var red = 0; red <= 1; red += 0.0001) {
var initialColor = new Color(red, 0, 0, 1);
var colorCssString = initialColor.toCssColorString();
var colorValues = cssStringParse(colorCssString);
var resultColor = Color.colorFromBytes(colorValues[0], colorValues[1], colorValues[2], colorValues[3] * 255);
expect(Math.abs(resultColor.red - initialColor.red) <= err).toBe(true);
}
});
});
});