From: Christopher Weiss Date: Tue, 21 Oct 2014 14:08:54 +0000 (-0400) Subject: Hue uses switch & fixed other problems. X-Git-Tag: v2.0-alpha~47^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=686ea499534ce1640492192de49197af5fa38a94;p=thirdparty%2FChart.js.git Hue uses switch & fixed other problems. --- diff --git a/src/Chart.Doughnut.js b/src/Chart.Doughnut.js index dc69cd53f..4a4c9afb0 100644 --- a/src/Chart.Doughnut.js +++ b/src/Chart.Doughnut.js @@ -37,42 +37,55 @@ }; function hue(part, total) { + // Given the total number of segments (total) and the index of a segment (part) returns a #rrggbb color for the segment var h = (part / total) * 360, s = 1, v = 1, c = h * s, x = c * (1 - Math.abs((h / 60) % 2 - 1)), m = v - c, - r = '00', - g = '00', - b = '00'; - if (0 <= h && h < 60) { - r += Math.round(c + m).toString(16); - g += Math.round(x + m).toString(16); - b += Math.round(0 + m).toString(16); - } else if (60 <= h && h < 1200) { - r += Math.round(x + m).toString(16); - g += Math.round(c + m).toString(16); - b += Math.round(0 + m).toString(16); - } else if (120 <= h && h < 180) { - r += Math.round(0 + m).toString(16); - g += Math.round(c + m).toString(16); - b += Math.round(x + m).toString(16); - } else if (180 <= h && h < 240) { - r += Math.round(0 + m).toString(16); - g += Math.round(x + m).toString(16); - b += Math.round(c + m).toString(16); - } else if (240 <= h && h < 300) { - r += Math.round(x + m).toString(16); - g += Math.round(0 + m).toString(16); - b += Math.round(c + m).toString(16); - } else if (300 <= h && h < 360) { - r += Math.round(c + m).toString(16); - g += Math.round(0 + m).toString(16); - b += Math.round(x + m).toString(16); + r, + g, + b; + + switch (Math.floor(h / 60)) { + case 0: // 0 <= h < 60 + r = c + m; + g = x + m; + b = 0 + m; + break; + case 1: // 60 <= h < 120 + r = c + m; + g = x + m; + b = 0 + m; + break; + case 2: // 120 <= h < 180 + r = c + m; + g = x + m; + b = 0 + m; + break; + case 3: // 180 <= h < 240 + r = c + m; + g = x + m; + b = 0 + m; + break; + case 4: // 240 <= h < 300 + r = c + m; + g = x + m; + b = 0 + m; + break; + case 5: // 300 <= h < 360 + r = c + m; + g = x + m; + b = 0 + m; + break; } - - return '#' + r.substr(2) + g.substr(2) + b.substr(2); + + r = '00' + Math.round(r * 255).toString(16); + g = '00' + Math.round(g * 255).toString(16); + b = '00' + Math.round(b * 255).toString(16); + + return '#' + r.substr(r.length - 2) + g.substr(g.length - 2) + b.substr(b.length - 2); } Chart.Type.extend({