diff options
author | Pim van den Berg | 2013-05-10 21:36:54 +0200 |
---|---|---|
committer | Pim van den Berg | 2013-05-10 21:36:54 +0200 |
commit | 0163faefef02207ad0ea3330af688103633293ff (patch) | |
tree | c407fca93a3418af59c4c7133327f5bc1630fba5 /js/Color.js | |
parent | Merge pull request #5 from mce35/nut (diff) | |
download | apt-panopticon_cgp-0163faefef02207ad0ea3330af688103633293ff.zip apt-panopticon_cgp-0163faefef02207ad0ea3330af688103633293ff.tar.gz apt-panopticon_cgp-0163faefef02207ad0ea3330af688103633293ff.tar.bz2 apt-panopticon_cgp-0163faefef02207ad0ea3330af688103633293ff.tar.xz |
import js directory from jsrrdgraph for client side graph rendering
Source: https://github.com/manuelluis/jsrrdgraph@276b880
Diffstat (limited to 'js/Color.js')
-rw-r--r-- | js/Color.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/js/Color.js b/js/Color.js new file mode 100644 index 0000000..e294f65 --- /dev/null +++ b/js/Color.js | |||
@@ -0,0 +1,76 @@ | |||
1 | /** | ||
2 | * | ||
3 | * This program is free software; you can redistribute it and/or modify it | ||
4 | * under the terms of the GNU General Public License as published by the Free | ||
5 | * Software Foundation; either version 2 of the License, or (at your option) | ||
6 | * any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
11 | * more details. | ||
12 | |||
13 | * You should have received a copy of the GNU General Public License along | ||
14 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
15 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
16 | * | ||
17 | **/ | ||
18 | |||
19 | "use strict"; | ||
20 | |||
21 | /** | ||
22 | * ColorError | ||
23 | * @constructor | ||
24 | */ | ||
25 | var ColorError = function (message) | ||
26 | { | ||
27 | this.prototype = Error.prototype; | ||
28 | this.name = "ColorError"; | ||
29 | this.message = (message) ? message : "Error"; | ||
30 | }; | ||
31 | |||
32 | /** | ||
33 | * Color | ||
34 | * @constructor | ||
35 | */ | ||
36 | function Color(str) | ||
37 | { | ||
38 | var bits; | ||
39 | |||
40 | this.r = 0; | ||
41 | this.g = 0; | ||
42 | this.b = 0; | ||
43 | this.a = 1.0; | ||
44 | |||
45 | if ((bits = /^#?([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/.exec(str))) { | ||
46 | this.r = parseInt(bits[1]+bits[1], 16); | ||
47 | this.g = parseInt(bits[2]+bits[2], 16); | ||
48 | this.b = parseInt(bits[3]+bits[3], 16); | ||
49 | } else if ((bits = /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/.exec(str))) { | ||
50 | this.r = parseInt(bits[1], 16); | ||
51 | this.g = parseInt(bits[2], 16); | ||
52 | this.b = parseInt(bits[3], 16); | ||
53 | } else if ((bits = /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/.exec(str))) { | ||
54 | this.r = parseInt(bits[1], 16); | ||
55 | this.g = parseInt(bits[2], 16); | ||
56 | this.b = parseInt(bits[3], 16); | ||
57 | this.a = parseInt(bits[4], 16)/255; | ||
58 | } else if ((bits = /^rgb\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\)$/.exec(str))) { | ||
59 | this.r = parseInt(bits[1], 10); | ||
60 | this.g = parseInt(bits[2], 10); | ||
61 | this.b = parseInt(bits[3], 10); | ||
62 | } else if ((bits = /^rgba\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([0-9.]+)\)$/.exec(str))) { | ||
63 | this.r = parseInt(bits[1], 10); | ||
64 | this.g = parseInt(bits[2], 10); | ||
65 | this.b = parseInt(bits[3], 10); | ||
66 | this.a = parseFloat(bits[4], 10); | ||
67 | } else { | ||
68 | throw new ColorError("Unknow color format '"+str+"'"); | ||
69 | } | ||
70 | }; | ||
71 | |||
72 | Color.prototype.torgba = function () | ||
73 | { | ||
74 | return 'rgba('+this.r+','+this.g+','+this.b+','+this.a+')'; | ||
75 | }; | ||
76 | |||