/src/org/mt4j/util/MTColor.java

http://mt4j.googlecode.com/ · Java · 422 lines · 139 code · 70 blank · 213 comment · 8 complexity · 8da8fab9001a7dba4a76f589ed77b7ad MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.util;
  19. /**
  20. * The Class MTColor.
  21. *
  22. */
  23. public class MTColor {
  24. /** The name. */
  25. private String name;
  26. // /** The Constant RED. */
  27. // public static transient final String RED = "RED";
  28. //
  29. // /** The Constant GREEN. */
  30. // public static transient final String GREEN = "GREEN";
  31. //
  32. // /** The Constant BLUE. */
  33. // public static transient final String BLUE = "BLUE";
  34. //
  35. // /** The Constant YELLOW. */
  36. // public static transient final String YELLOW = "YELLOW";
  37. //
  38. // /** The Constant BLACK. */
  39. // public static transient final String BLACK = "BLACK";
  40. //
  41. // /** The Constant WHITE. */
  42. // public static transient final String WHITE = "WHITE";
  43. /** The Constant ALPHA_NO_TRANSPARENCY. */
  44. public static transient final float ALPHA_NO_TRANSPARENCY = 255f;
  45. /** The Constant ALPHA_LIGHT_TRANSPARENCY. */
  46. public static transient final float ALPHA_LIGHT_TRANSPARENCY = 255f/1.5f;
  47. /** The Constant ALPHA_HALF_TRANSPARENCY. */
  48. public static transient final float ALPHA_HALF_TRANSPARENCY = 255f/2f;
  49. /** The Constant ALPHA_HIGH_TRANSPARENCY. */
  50. public static transient final float ALPHA_HIGH_TRANSPARENCY = 255f/4f;
  51. /** The Constant ALPHA_FULL_TRANSPARENCY. */
  52. public static transient final float ALPHA_FULL_TRANSPARENCY = 0f;
  53. /** The r. */
  54. private float r;
  55. /** The g. */
  56. private float g;
  57. /** The b. */
  58. private float b;
  59. /** The alpha. */
  60. private float alpha;
  61. private boolean writeProtected;
  62. public static final MTColor RED = new MTColor(255,0,0,255,true);
  63. public static final MTColor GREEN = new MTColor(0,128,0,255,true);
  64. public static final MTColor BLUE = new MTColor(0,0,255,255,true);
  65. public static final MTColor BLACK = new MTColor(0,0,0,255,true);
  66. public static final MTColor WHITE = new MTColor(255,255,255,255,true);
  67. public static final MTColor GREY = new MTColor(128,128,128,255,true);
  68. public static final MTColor GRAY = new MTColor(128,128,128,255,true);
  69. public static final MTColor SILVER = new MTColor(192,192,192,255,true);
  70. public static final MTColor MAROON = new MTColor(128,0,0,255,true);
  71. public static final MTColor PURPLE = new MTColor(128,0,128,255,true);
  72. public static final MTColor FUCHSIA = new MTColor(255,0,255,255,true);
  73. public static final MTColor LIME = new MTColor(0,255,0,255,true);
  74. public static final MTColor OLIVE = new MTColor(128,128,0,255,true);
  75. public static final MTColor YELLOW = new MTColor(255,255,0,255,true);
  76. public static final MTColor NAVY = new MTColor(0,0,128,255,true);
  77. public static final MTColor TEAL = new MTColor(0,128,128,255,true);
  78. public static final MTColor AQUA = new MTColor(0,255,255,255,true);
  79. public static MTColor randomColor(){
  80. return new MTColor((float)(Math.random() * 255), (float)(Math.random() * 255), (float)(Math.random() * 255), 255);
  81. }
  82. /**
  83. * Instantiates a new mT color.
  84. *
  85. * @param color the color
  86. */
  87. public MTColor(MTColor color){
  88. this(color.getName(), color.getR(), color.getG(), color.getB(), color.getAlpha());
  89. }
  90. /**
  91. * Instantiates a new mT color.
  92. *
  93. * @param r the r
  94. * @param g the g
  95. * @param b the b
  96. */
  97. public MTColor(float r, float g, float b) {
  98. this(r, g, b, 255);
  99. }
  100. /**
  101. * Instantiates a new mT color.
  102. *
  103. * @param r the r
  104. * @param g the g
  105. * @param b the b
  106. * @param alpha the alpha
  107. */
  108. public MTColor(float r, float g, float b, float alpha) {
  109. this("undefined", r, g, b, alpha);
  110. }
  111. /**
  112. * Instantiates a new mT color.
  113. *
  114. * @param name the name
  115. * @param r the r
  116. * @param g the g
  117. * @param b the b
  118. */
  119. public MTColor(String name, float r, float g, float b) {
  120. this(name, r, g, b, 255);
  121. }
  122. /**
  123. * Instantiates a new mT color.
  124. *
  125. * @param name the name
  126. * @param r the r
  127. * @param g the g
  128. * @param b the b
  129. * @param alpha the alpha
  130. */
  131. public MTColor(String name, int r, int g, int b, int alpha) {
  132. this(name, (float)r, (float)g, (float)b, (float)alpha);
  133. }
  134. /**
  135. * Instantiates a new mT color.
  136. *
  137. * @param name the name
  138. * @param r the r
  139. * @param g the g
  140. * @param b the b
  141. * @param alpha the alpha
  142. * @param writeProtected the write protected
  143. */
  144. public MTColor(int r, int g, int b, int alpha, boolean writeProtected) {
  145. this("undefined", (float)r, (float)g, (float)b, (float)alpha, writeProtected);
  146. }
  147. /**
  148. * Instantiates a new mT color.
  149. *
  150. * @param name the name
  151. * @param r the r
  152. * @param g the g
  153. * @param b the b
  154. */
  155. public MTColor(String name, int r, int g, int b) {
  156. this(name, (float)r, (float)g, (float)b, 255f);
  157. }
  158. /**
  159. * Instantiates a new mT color.
  160. *
  161. * @param name the name
  162. * @param r the r
  163. * @param g the g
  164. * @param b the b
  165. * @param alpha the alpha
  166. */
  167. public MTColor(String name, float r, float g, float b, float alpha) {
  168. this(name, r, g, b, alpha, false);
  169. }
  170. public MTColor(String name, float r, float g, float b, float alpha, boolean writeProtected) {
  171. this.setColor(r, g, b, alpha);
  172. this.name = name;
  173. this.writeProtected = writeProtected;
  174. }
  175. public boolean isWriteProtected() {
  176. return writeProtected;
  177. }
  178. /**
  179. * Gets the r.
  180. *
  181. * @return the r
  182. */
  183. public float getR() {
  184. return r;
  185. }
  186. /**
  187. * Sets the r.
  188. *
  189. * @param r the new r
  190. */
  191. public void setR(float r) {
  192. if (!this.isWriteProtected())
  193. this.r = r;
  194. }
  195. /**
  196. * Gets the g.
  197. *
  198. * @return the g
  199. */
  200. public float getG() {
  201. return g;
  202. }
  203. /**
  204. * Sets the g.
  205. *
  206. * @param g the new g
  207. */
  208. public void setG(float g) {
  209. if (!this.isWriteProtected())
  210. this.g = g;
  211. }
  212. /**
  213. * Gets the b.
  214. *
  215. * @return the b
  216. */
  217. public float getB() {
  218. return b;
  219. }
  220. /**
  221. * Sets the b.
  222. *
  223. * @param b the new b
  224. */
  225. public void setB(float b) {
  226. if (!this.isWriteProtected())
  227. this.b = b;
  228. }
  229. /**
  230. * Gets the alpha.
  231. *
  232. * @return the alpha
  233. */
  234. public float getAlpha() {
  235. return alpha;
  236. }
  237. /**
  238. * Sets the alpha.
  239. *
  240. * @param alpha the new alpha
  241. */
  242. public void setAlpha(float alpha) {
  243. if (!this.isWriteProtected())
  244. this.alpha = alpha;
  245. }
  246. /**
  247. * Sets the color.
  248. *
  249. * @param r the r
  250. * @param g the g
  251. * @param b the b
  252. * @param alpha the alpha
  253. */
  254. public void setColor(float r, float g, float b, float alpha){
  255. if (!this.isWriteProtected()){
  256. this.r = r;
  257. this.g = g;
  258. this.b = b;
  259. this.alpha = alpha;
  260. }
  261. }
  262. /**
  263. * Sets the color.
  264. *
  265. * @param r the r
  266. * @param g the g
  267. * @param b the b
  268. */
  269. public void setColor(float r, float g, float b){
  270. if (!this.isWriteProtected()){
  271. this.r = r;
  272. this.g = g;
  273. this.b = b;
  274. this.alpha = 255;
  275. }
  276. }
  277. /**
  278. * Sets the color.
  279. *
  280. * @param f the new color
  281. */
  282. public void setColor(float f){
  283. if (!this.isWriteProtected()){
  284. this.r = f;
  285. this.g = f;
  286. this.b = f;
  287. this.alpha = 255;
  288. }
  289. }
  290. /**
  291. * Gets the name.
  292. *
  293. * @return the name
  294. */
  295. public String getName() {
  296. return name;
  297. }
  298. /**
  299. * Gets the copy.
  300. *
  301. * @return the copy
  302. */
  303. public MTColor getCopy(){
  304. return new MTColor(this.getName(), this.getR(),this.getG(),this.getB(), this.getAlpha());
  305. }
  306. /*
  307. public static void fill(AbstractShape as, String colorName){
  308. ColorManager.getInstance().fill(as, colorName);
  309. }
  310. public static void stroke(AbstractShape as, String colorName){
  311. ColorManager.getInstance().stroke(as, colorName);
  312. }
  313. public static void fill(AbstractShape as, String colorName, float alpha){
  314. ColorManager.getInstance().fill(as, colorName, alpha);
  315. }
  316. public static void stroke(AbstractShape as, String colorName, float alpha){
  317. ColorManager.getInstance().stroke(as, colorName, alpha);
  318. }
  319. public static Color get(String colorName){
  320. return ColorManager.getInstance().getColor(colorName);
  321. }
  322. */
  323. /**
  324. * To color string.
  325. *
  326. * @return the string
  327. */
  328. public String toColorString(){
  329. return "Color{" + r + "," + g + "," + b + "_" + alpha + "}";
  330. }
  331. /* (non-Javadoc)
  332. * @see java.lang.Object#toString()
  333. */
  334. @Override
  335. public String toString(){
  336. return this.toColorString();
  337. }
  338. /* (non-Javadoc)
  339. * @see java.lang.Object#equals(java.lang.Object)
  340. */
  341. @Override
  342. public boolean equals(Object obj) {
  343. return (obj instanceof MTColor && this.toColorString().equals(((MTColor)obj).toColorString()));
  344. }
  345. /* (non-Javadoc)
  346. * @see java.lang.Object#hashCode()
  347. */
  348. @Override
  349. public int hashCode() {
  350. return this.toColorString().hashCode();
  351. }
  352. }