JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / jalview / schemes / UserColourScheme.js
1 Clazz.declarePackage ("jalview.schemes");\r
2 Clazz.load (["jalview.schemes.ResidueColourScheme"], "jalview.schemes.UserColourScheme", ["jalview.schemes.ColourSchemeProperty", "$.ResidueProperties", "java.awt.Color", "java.util.StringTokenizer"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.lowerCaseColours = null;\r
5 this.schemeName = null;\r
6 Clazz.instantialize (this, arguments);\r
7 }, jalview.schemes, "UserColourScheme", jalview.schemes.ResidueColourScheme);\r
8 Clazz.makeConstructor (c$, \r
9 function () {\r
10 Clazz.superConstructor (this, jalview.schemes.UserColourScheme, [jalview.schemes.ResidueProperties.aaIndex]);\r
11 });\r
12 Clazz.makeConstructor (c$, \r
13 function (newColors) {\r
14 Clazz.superConstructor (this, jalview.schemes.UserColourScheme, [jalview.schemes.ResidueProperties.aaIndex]);\r
15 this.colors = newColors;\r
16 }, "~A");\r
17 Clazz.overrideMethod (c$, "applyTo", \r
18 function (sg, hiddenRepSequences) {\r
19 var usc =  new jalview.schemes.UserColourScheme (this.colors);\r
20 if (this.lowerCaseColours != null) {\r
21 usc.schemeName =  String.instantialize (this.schemeName);\r
22 usc.lowerCaseColours =  new Array (this.lowerCaseColours.length);\r
23 System.arraycopy (this.lowerCaseColours, 0, usc.lowerCaseColours, 0, this.lowerCaseColours.length);\r
24 }return usc;\r
25 }, "jalview.datamodel.AnnotatedCollectionI,java.util.Map");\r
26 Clazz.makeConstructor (c$, \r
27 function (colour) {\r
28 Clazz.superConstructor (this, jalview.schemes.UserColourScheme, [jalview.schemes.ResidueProperties.aaIndex]);\r
29 var col = this.getColourFromString (colour);\r
30 if (col == null) {\r
31 System.out.println ("Unknown colour!! " + colour);\r
32 col = this.createColourFromName (colour);\r
33 }this.colors =  new Array (24);\r
34 for (var i = 0; i < 24; i++) {\r
35 this.colors[i] = col;\r
36 }\r
37 this.schemeName = colour;\r
38 }, "~S");\r
39 Clazz.defineMethod (c$, "getColours", \r
40 function () {\r
41 return this.colors;\r
42 });\r
43 Clazz.defineMethod (c$, "getLowerCaseColours", \r
44 function () {\r
45 return this.lowerCaseColours;\r
46 });\r
47 Clazz.defineMethod (c$, "setName", \r
48 function (name) {\r
49 this.schemeName = name;\r
50 }, "~S");\r
51 Clazz.defineMethod (c$, "getName", \r
52 function () {\r
53 return this.schemeName;\r
54 });\r
55 Clazz.defineMethod (c$, "getColourFromString", \r
56 function (colour) {\r
57 colour = colour.trim ();\r
58 var col = null;\r
59 try {\r
60 var value = Integer.parseInt (colour, 16);\r
61 col =  new java.awt.Color (value);\r
62 } catch (ex) {\r
63 if (Clazz.exceptionOf (ex, NumberFormatException)) {\r
64 } else {\r
65 throw ex;\r
66 }\r
67 }\r
68 if (col == null) {\r
69 col = jalview.schemes.ColourSchemeProperty.getAWTColorFromName (colour);\r
70 }if (col == null) {\r
71 try {\r
72 var st =  new java.util.StringTokenizer (colour, ",");\r
73 var r = Integer.parseInt (st.nextToken ());\r
74 var g = Integer.parseInt (st.nextToken ());\r
75 var b = Integer.parseInt (st.nextToken ());\r
76 col =  new java.awt.Color (r, g, b);\r
77 } catch (ex) {\r
78 if (Clazz.exceptionOf (ex, Exception)) {\r
79 } else {\r
80 throw ex;\r
81 }\r
82 }\r
83 }return col;\r
84 }, "~S");\r
85 Clazz.defineMethod (c$, "createColourFromName", \r
86 function (name) {\r
87 var r;\r
88 var g;\r
89 var b;\r
90 var lsize = name.length;\r
91 var start = 0;\r
92 var end = Clazz.doubleToInt (lsize / 3);\r
93 var rgbOffset = Math.abs (name.hashCode () % 10) * 15;\r
94 r = Math.abs (name.substring (start, end).hashCode () + rgbOffset) % 210 + 20;\r
95 start = end;\r
96 end += Clazz.doubleToInt (lsize / 3);\r
97 if (end > lsize) {\r
98 end = lsize;\r
99 }g = Math.abs (name.substring (start, end).hashCode () + rgbOffset) % 210 + 20;\r
100 b = Math.abs (name.substring (end).hashCode () + rgbOffset) % 210 + 20;\r
101 var color =  new java.awt.Color (r, g, b);\r
102 return color;\r
103 }, "~S");\r
104 Clazz.defineMethod (c$, "parseAppletParameter", \r
105 function (paramValue) {\r
106 var st =  new java.util.StringTokenizer (paramValue, ";");\r
107 var st2;\r
108 var token = null;\r
109 var colour;\r
110 var residues;\r
111 try {\r
112 while (st.hasMoreElements ()) {\r
113 token = st.nextToken ().trim ();\r
114 residues = token.substring (0, token.indexOf ("="));\r
115 colour = token.substring (token.indexOf ("=") + 1);\r
116 st2 =  new java.util.StringTokenizer (residues, " ,");\r
117 while (st2.hasMoreTokens ()) {\r
118 token = st2.nextToken ();\r
119 if (jalview.schemes.ResidueProperties.aaIndex[token.charCodeAt (0)] == -1) {\r
120 continue;\r
121 }var colIndex = jalview.schemes.ResidueProperties.aaIndex[token.charCodeAt (0)];\r
122 if (token.equalsIgnoreCase ("lowerCase")) {\r
123 if (this.lowerCaseColours == null) {\r
124 this.lowerCaseColours =  new Array (23);\r
125 }for (var i = 0; i < 23; i++) {\r
126 if (this.lowerCaseColours[i] == null) {\r
127 this.lowerCaseColours[i] = this.getColourFromString (colour);\r
128 }}\r
129 continue;\r
130 }if (token.equals (token.toLowerCase ())) {\r
131 if (this.lowerCaseColours == null) {\r
132 this.lowerCaseColours =  new Array (23);\r
133 }this.lowerCaseColours[colIndex] = this.getColourFromString (colour);\r
134 } else {\r
135 this.colors[colIndex] = this.getColourFromString (colour);\r
136 }}\r
137 }\r
138 } catch (ex) {\r
139 if (Clazz.exceptionOf (ex, Exception)) {\r
140 System.out.println ("Error parsing userDefinedColours:\n" + token + "\n" + ex);\r
141 } else {\r
142 throw ex;\r
143 }\r
144 }\r
145 }, "~S");\r
146 Clazz.defineMethod (c$, "findColour", \r
147 function (c, j, seq) {\r
148 var currentColour;\r
149 var index = jalview.schemes.ResidueProperties.aaIndex[c.charCodeAt (0)];\r
150 if ((this.threshold == 0) || this.aboveThreshold (c, j)) {\r
151 if (this.lowerCaseColours != null && 'a' <= c && c <= 'z') {\r
152 currentColour = this.lowerCaseColours[index];\r
153 } else {\r
154 currentColour = this.colors[index];\r
155 }} else {\r
156 currentColour = java.awt.Color.white;\r
157 }if (this.conservationColouring) {\r
158 currentColour = this.applyConservation (currentColour, j);\r
159 }return currentColour;\r
160 }, "~S,~N,jalview.datamodel.SequenceI");\r
161 Clazz.defineMethod (c$, "setLowerCaseColours", \r
162 function (lcolours) {\r
163 this.lowerCaseColours = lcolours;\r
164 }, "~A");\r
165 });\r