From c62cb30720faacb6ca0315505aae3e156de27a4b Mon Sep 17 00:00:00 2001 From: David Schnur Date: Tue, 17 Jul 2012 19:24:40 -0400 Subject: [PATCH 1/3] Optimize and tidy up calculation of neededColors. --- jquery.flot.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/jquery.flot.js b/jquery.flot.js index 8908bf7..847e1e3 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -416,31 +416,36 @@ } function fillInSeriesOptions() { - var i; - - // collect what we already got of colors - var neededColors = series.length, - usedColors = [], - assignedColors = []; + + var neededColors = series.length, maxIndex = 0, i; + + // Subtract the number of series that already have fixed + // colors from the number we need to generate. + for (i = 0; i < series.length; ++i) { var sc = series[i].color; if (sc != null) { - --neededColors; - if (typeof sc == "number") - assignedColors.push(sc); - else - usedColors.push($.color.parse(series[i].color)); + neededColors--; + if (typeof sc == "number" && sc > maxIndex) { + maxIndex = sc; + } } } - - // we might need to generate more colors if higher indices - // are assigned - for (i = 0; i < assignedColors.length; ++i) { - neededColors = Math.max(neededColors, assignedColors[i] + 1); + + // If any of the user colors are numeric indexes, then we + // need to generate at least as many as the highest index. + + if (maxIndex > neededColors) { + neededColors = maxIndex + 1; } - // produce colors as needed + // Generate as many colors as necessary, using the provided + // colors as a base and alternatingly increasing/decreasing + // their luminosity in steps that grow smaller each time we + // hit the edge of the color space. + var colors = [], variation = 0; + i = 0; while (colors.length < neededColors) { var c; @@ -464,7 +469,8 @@ } } - // fill in the options + // Finalize the series options, filling in their colors + var colori = 0, s; for (i = 0; i < series.length; ++i) { s = series[i]; From 6eaeabcd1d10942ce7d7b281e42620672f9e2ba9 Mon Sep 17 00:00:00 2001 From: David Schnur Date: Wed, 18 Jul 2012 09:54:35 -0400 Subject: [PATCH 2/3] Improve generation of color variations. As the size of the variation increases, the resulting colors approach white and black. To avoid this we now reset the variation when it gets too large. This results in repeated colors, but that's much better than a list full of whites and blacks. --- jquery.flot.js | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/jquery.flot.js b/jquery.flot.js index 847e1e3..f76ebcd 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -439,34 +439,32 @@ neededColors = maxIndex + 1; } - // Generate as many colors as necessary, using the provided - // colors as a base and alternatingly increasing/decreasing - // their luminosity in steps that grow smaller each time we - // hit the edge of the color space. - - var colors = [], variation = 0; - - i = 0; - while (colors.length < neededColors) { - var c; - if (options.colors.length == i) // check degenerate case - c = $.color.make(100, 100, 100); - else - c = $.color.parse(options.colors[i]); + // Generate the needed colors, based on the option colors - // vary color if needed - var sign = variation % 2 == 1 ? -1 : 1; - c.scale('rgb', 1 + sign * Math.ceil(variation / 2) * 0.2); + var c, colors = [], colorPool = options.colors, + colorPoolSize = colorPool.length, variation = 0; - // FIXME: if we're getting to close to something else, - // we should probably skip this one - colors.push(c); - - ++i; - if (i >= options.colors.length) { - i = 0; - ++variation; + for (i = 0; i < neededColors; i++) { + + c = $.color.parse(colorPool[i % colorPoolSize] || "#666"); + + // Each time we exhaust the colors in the pool we adjust + // a scaling factor used to produce more variations on + // those colors. The factor alternates negative/positive + // to produce lighter/darker colors. + + // Reset the variation after every few cycles, or else + // it will end up producing only white or black colors. + + if (i % colorPoolSize == 0 && i) { + if (variation >= 0) { + if (variation < 0.5) { + variation = -variation - 0.2; + } else variation = 0; + } else variation = -variation; } + + colors[i] = c.scale('rgb', 1 + variation); } // Finalize the series options, filling in their colors From 2a7ae881bd91286e5ceeb9427936f0ae5a3bc46a Mon Sep 17 00:00:00 2001 From: David Schnur Date: Wed, 18 Jul 2012 09:57:54 -0400 Subject: [PATCH 3/3] Added credits for improved color generation. --- NEWS.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.txt b/NEWS.txt index a91d899..f92bc60 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -114,6 +114,9 @@ Bug fixes - Axis labels no longer appear as decimals rather than integers in certain cases. (patch by Clemens Stolle, issue 541) +- Automatic color generation no longer produces only whites and blacks + when there are many series. (patch by David Schnur and Tom Cleaveland) + Flot 0.7 --------