Normalize color in place to reduce garbage generation

This commit is contained in:
Frederic Junod
2018-01-12 15:21:25 +01:00
parent 5ade602183
commit 974f9391f2
+12 -14
View File
@@ -134,7 +134,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);
@@ -167,12 +167,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
} }
@@ -183,16 +183,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;
} }