Interim commit.
[yaffs-website] / web / modules / contrib / environment_indicator / js / color.js
1 var pad = function(num, totalChars) {
2     var pad = '0';
3     num = num + '';
4     while (num.length < totalChars) {
5         num = pad + num;
6     }
7     return num;
8 };
9
10 // Ratio is between 0 and 1
11 var changeColor = function(color, ratio, darker) {
12     // Trim trailing/leading whitespace
13     color = color.replace(/^\s*|\s*$/, '');
14
15     // Expand three-digit hex
16     color = color.replace(
17         /^#?([a-f0-9])([a-f0-9])([a-f0-9])$/i,
18         '#$1$1$2$2$3$3'
19     );
20
21     // Calculate ratio
22     var difference = Math.round(ratio * 256) * (darker ? -1 : 1),
23         // Determine if input is RGB(A)
24         rgb = color.match(new RegExp('^rgba?\\(\\s*' +
25             '(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
26             '\\s*,\\s*' +
27             '(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
28             '\\s*,\\s*' +
29             '(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
30             '(?:\\s*,\\s*' +
31             '(0|1|0?\\.\\d+))?' +
32             '\\s*\\)$'
33         , 'i')),
34         alpha = !!rgb && rgb[4] != null ? rgb[4] : null,
35
36         // Convert hex to decimal
37         decimal = !!rgb? [rgb[1], rgb[2], rgb[3]] : color.replace(
38             /^#?([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i,
39             function() {
40                 return parseInt(arguments[1], 16) + ',' +
41                     parseInt(arguments[2], 16) + ',' +
42                     parseInt(arguments[3], 16);
43             }
44         ).split(/,/),
45         returnValue;
46
47     // Return RGB(A)
48     return !!rgb ?
49         'rgb' + (alpha !== null ? 'a' : '') + '(' +
50             Math[darker ? 'max' : 'min'](
51                 parseInt(decimal[0], 10) + difference, darker ? 0 : 255
52             ) + ', ' +
53             Math[darker ? 'max' : 'min'](
54                 parseInt(decimal[1], 10) + difference, darker ? 0 : 255
55             ) + ', ' +
56             Math[darker ? 'max' : 'min'](
57                 parseInt(decimal[2], 10) + difference, darker ? 0 : 255
58             ) +
59             (alpha !== null ? ', ' + alpha : '') +
60             ')' :
61         // Return hex
62         [
63             '#',
64             pad(Math[darker ? 'max' : 'min'](
65                 parseInt(decimal[0], 10) + difference, darker ? 0 : 255
66             ).toString(16), 2),
67             pad(Math[darker ? 'max' : 'min'](
68                 parseInt(decimal[1], 10) + difference, darker ? 0 : 255
69             ).toString(16), 2),
70             pad(Math[darker ? 'max' : 'min'](
71                 parseInt(decimal[2], 10) + difference, darker ? 0 : 255
72             ).toString(16), 2)
73         ].join('');
74 };
75 var lighterColor = function(color, ratio) {
76     return changeColor(color, ratio, false);
77 };
78 var darkerColor = function(color, ratio) {
79     return changeColor(color, ratio, true);
80 };