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

View File

@@ -11,7 +11,7 @@ import {clamp} from './math.js';
* @type {RegExp}
* @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) {
const el = document.createElement('div');
el.style.color = color;
document.body.appendChild(el);
const rgb = getComputedStyle(el).color;
document.body.removeChild(el);
return rgb;
if (el.style.color !== '') {
document.body.appendChild(el);
const rgb = getComputedStyle(el).color;
document.body.removeChild(el);
return rgb;
} else {
return '';
}
}
@@ -129,7 +133,7 @@ export function asArray(color) {
* @return {ol.Color} Color.
*/
function fromStringInternal_(s) {
let r, g, b, a, color, parts;
let r, g, b, a, color;
if (NAMED_COLOR_RE_.exec(s)) {
s = fromNamed(s);
@@ -162,12 +166,12 @@ function fromStringInternal_(s) {
}
color = [r, g, b, a / 255];
} else if (s.indexOf('rgba(') == 0) { // rgba()
parts = s.slice(5, -1).split(',').map(Number);
color = normalize(parts);
color = s.slice(5, -1).split(',').map(Number);
normalize(color);
} else if (s.indexOf('rgb(') == 0) { // rgb()
parts = s.slice(4, -1).split(',').map(Number);
parts.push(1);
color = normalize(parts);
color = s.slice(4, -1).split(',').map(Number);
color.push(1);
normalize(color);
} else {
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
* @param {ol.Color} color Color.
* @param {ol.Color=} opt_color Color.
* @return {ol.Color} Clamped color.
*/
export function normalize(color, opt_color) {
const result = opt_color || [];
result[0] = clamp((color[0] + 0.5) | 0, 0, 255);
result[1] = clamp((color[1] + 0.5) | 0, 0, 255);
result[2] = clamp((color[2] + 0.5) | 0, 0, 255);
result[3] = clamp(color[3], 0, 1);
return result;
export function normalize(color) {
color[0] = clamp((color[0] + 0.5) | 0, 0, 255);
color[1] = clamp((color[1] + 0.5) | 0, 0, 255);
color[2] = clamp((color[2] + 0.5) | 0, 0, 255);
color[3] = clamp(color[3], 0, 1);
return color;
}

View File

@@ -121,9 +121,9 @@ describe('ol.color', 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;
for (i = 0, ii < invalidColors.length; i < ii; ++i) {
for (i = 0, ii = invalidColors.length; i < ii; ++i) {
expect(function() {
fromString(invalidColors[i]);
}).to.throwException();