Merge pull request #7698 from fredj/test_typo

Fix for loop in color test
This commit is contained in:
Frédéric Junod
2018-01-18 08:35:24 +01:00
committed by GitHub
2 changed files with 23 additions and 21 deletions
+21 -19
View File
@@ -11,7 +11,7 @@ import {clamp} from './math.js';
* @type {RegExp} * @type {RegExp}
* @private * @private
*/ */
const HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3,4}){1,2}$/i; const HEX_COLOR_RE_ = /^#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})$/i;
/** /**
@@ -45,10 +45,14 @@ export function asString(color) {
function fromNamed(color) { function fromNamed(color) {
const el = document.createElement('div'); const el = document.createElement('div');
el.style.color = color; el.style.color = color;
document.body.appendChild(el); if (el.style.color !== '') {
const rgb = getComputedStyle(el).color; document.body.appendChild(el);
document.body.removeChild(el); const rgb = getComputedStyle(el).color;
return rgb; document.body.removeChild(el);
return rgb;
} else {
return '';
}
} }
@@ -129,7 +133,7 @@ export function asArray(color) {
* @return {ol.Color} Color. * @return {ol.Color} Color.
*/ */
function fromStringInternal_(s) { function fromStringInternal_(s) {
let r, g, b, a, color, parts; let r, g, b, a, color;
if (NAMED_COLOR_RE_.exec(s)) { if (NAMED_COLOR_RE_.exec(s)) {
s = fromNamed(s); s = fromNamed(s);
@@ -162,12 +166,12 @@ function fromStringInternal_(s) {
} }
color = [r, g, b, a / 255]; color = [r, g, b, a / 255];
} else if (s.indexOf('rgba(') == 0) { // rgba() } else if (s.indexOf('rgba(') == 0) { // rgba()
parts = s.slice(5, -1).split(',').map(Number); color = s.slice(5, -1).split(',').map(Number);
color = normalize(parts); normalize(color);
} else if (s.indexOf('rgb(') == 0) { // rgb() } else if (s.indexOf('rgb(') == 0) { // rgb()
parts = s.slice(4, -1).split(',').map(Number); color = s.slice(4, -1).split(',').map(Number);
parts.push(1); color.push(1);
color = normalize(parts); normalize(color);
} else { } else {
assert(false, 14); // Invalid color assert(false, 14); // Invalid color
} }
@@ -178,16 +182,14 @@ function fromStringInternal_(s) {
/** /**
* TODO this function is only used in the test, we probably shouldn't export it * TODO this function is only used in the test, we probably shouldn't export it
* @param {ol.Color} color Color. * @param {ol.Color} color Color.
* @param {ol.Color=} opt_color Color.
* @return {ol.Color} Clamped color. * @return {ol.Color} Clamped color.
*/ */
export function normalize(color, opt_color) { export function normalize(color) {
const result = opt_color || []; color[0] = clamp((color[0] + 0.5) | 0, 0, 255);
result[0] = clamp((color[0] + 0.5) | 0, 0, 255); color[1] = clamp((color[1] + 0.5) | 0, 0, 255);
result[1] = clamp((color[1] + 0.5) | 0, 0, 255); color[2] = clamp((color[2] + 0.5) | 0, 0, 255);
result[2] = clamp((color[2] + 0.5) | 0, 0, 255); color[3] = clamp(color[3], 0, 1);
result[3] = clamp(color[3], 0, 1); return color;
return result;
} }
+2 -2
View File
@@ -121,9 +121,9 @@ describe('ol.color', function() {
}); });
it('throws an error on invalid colors', function() { it('throws an error on invalid colors', function() {
const invalidColors = ['tuesday', '#12345', '#1234567', 'rgb(255.0,0,0)']; const invalidColors = ['tuesday', '#12345', '#1234567'];
let i, ii; let i, ii;
for (i = 0, ii < invalidColors.length; i < ii; ++i) { for (i = 0, ii = invalidColors.length; i < ii; ++i) {
expect(function() { expect(function() {
fromString(invalidColors[i]); fromString(invalidColors[i]);
}).to.throwException(); }).to.throwException();