PageRenderTime 35ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/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 ***********************************************************************/
 18package org.mt4j.util;
 19
 20/**
 21 * The Class MTColor.
 22 * 
 23 */
 24public class MTColor {
 25	
 26	/** The name. */
 27	private String name;
 28	
 29//	/** The Constant RED. */
 30//	public static transient final String RED =  "RED";
 31//	
 32//	/** The Constant GREEN. */
 33//	public static transient final String GREEN =  "GREEN";
 34//	
 35//	/** The Constant BLUE. */
 36//	public static transient final String BLUE =  "BLUE";
 37//	
 38//	/** The Constant YELLOW. */
 39//	public static transient final String YELLOW = "YELLOW";
 40//	
 41//	/** The Constant BLACK. */
 42//	public static transient final String BLACK =  "BLACK";
 43//	
 44//	/** The Constant WHITE. */
 45//	public static transient final String WHITE =  "WHITE";
 46	
 47	/** The Constant ALPHA_NO_TRANSPARENCY. */
 48	public static transient final float ALPHA_NO_TRANSPARENCY = 255f;
 49	
 50	/** The Constant ALPHA_LIGHT_TRANSPARENCY. */
 51	public static transient final float ALPHA_LIGHT_TRANSPARENCY = 255f/1.5f;
 52	
 53	/** The Constant ALPHA_HALF_TRANSPARENCY. */
 54	public static transient final float ALPHA_HALF_TRANSPARENCY = 255f/2f;
 55	
 56	/** The Constant ALPHA_HIGH_TRANSPARENCY. */
 57	public static transient final float ALPHA_HIGH_TRANSPARENCY = 255f/4f;
 58	
 59	/** The Constant ALPHA_FULL_TRANSPARENCY. */
 60	public static transient final float ALPHA_FULL_TRANSPARENCY = 0f;
 61	
 62	/** The r. */
 63	private float r;
 64	
 65	/** The g. */
 66	private float g;
 67	
 68	/** The b. */
 69	private float b;
 70	
 71	/** The alpha. */
 72	private float alpha;
 73	
 74	private boolean writeProtected;
 75	
 76	
 77	public static final MTColor RED = new MTColor(255,0,0,255,true);
 78	
 79	public static final MTColor GREEN = new MTColor(0,128,0,255,true);
 80	
 81	public static final MTColor BLUE = new MTColor(0,0,255,255,true);
 82	
 83	public static final MTColor BLACK = new MTColor(0,0,0,255,true);
 84	
 85	public static final MTColor WHITE = new MTColor(255,255,255,255,true);
 86	
 87	public static final MTColor GREY = new MTColor(128,128,128,255,true);
 88	
 89	public static final MTColor GRAY = new MTColor(128,128,128,255,true);
 90	
 91	public static final MTColor SILVER = new MTColor(192,192,192,255,true);
 92	
 93	public static final MTColor MAROON = new MTColor(128,0,0,255,true);
 94	
 95	public static final MTColor PURPLE = new MTColor(128,0,128,255,true);
 96	
 97	public static final MTColor FUCHSIA = new MTColor(255,0,255,255,true);
 98	
 99	public static final MTColor LIME = new MTColor(0,255,0,255,true);
100	
101	public static final MTColor OLIVE = new MTColor(128,128,0,255,true);
102	
103	public static final MTColor YELLOW = new MTColor(255,255,0,255,true);
104	
105	public static final MTColor NAVY = new MTColor(0,0,128,255,true);
106	
107	public static final MTColor TEAL = new MTColor(0,128,128,255,true);
108	
109	public static final MTColor AQUA = new MTColor(0,255,255,255,true);
110	
111	public static MTColor randomColor(){
112		return new MTColor((float)(Math.random() * 255), (float)(Math.random() * 255), (float)(Math.random() * 255), 255);
113	}
114	
115	/**
116	 * Instantiates a new mT color.
117	 * 
118	 * @param color the color
119	 */
120	public MTColor(MTColor color){
121		this(color.getName(), color.getR(), color.getG(), color.getB(), color.getAlpha());
122	}
123	
124	/**
125	 * Instantiates a new mT color.
126	 * 
127	 * @param r the r
128	 * @param g the g
129	 * @param b the b
130	 */
131	public MTColor(float r, float g, float b) {
132		this(r, g, b, 255);
133	}
134	
135	/**
136	 * Instantiates a new mT color.
137	 * 
138	 * @param r the r
139	 * @param g the g
140	 * @param b the b
141	 * @param alpha the alpha
142	 */
143	public MTColor(float r, float g, float b, float alpha) {
144		this("undefined", r, g, b, alpha);
145	}
146	
147	
148	/**
149	 * Instantiates a new mT color.
150	 * 
151	 * @param name the name
152	 * @param r the r
153	 * @param g the g
154	 * @param b the b
155	 */
156	public MTColor(String name, float r, float g, float b) {
157		this(name, r, g, b, 255);
158	}
159	
160	
161	/**
162	 * Instantiates a new mT color.
163	 * 
164	 * @param name the name
165	 * @param r the r
166	 * @param g the g
167	 * @param b the b
168	 * @param alpha the alpha
169	 */
170	public MTColor(String name, int r, int g, int b, int alpha) {
171		this(name, (float)r, (float)g, (float)b, (float)alpha);
172	}
173	
174	/**
175	 * Instantiates a new mT color.
176	 *
177	 * @param name the name
178	 * @param r the r
179	 * @param g the g
180	 * @param b the b
181	 * @param alpha the alpha
182	 * @param writeProtected the write protected
183	 */
184	public MTColor(int r, int g, int b, int alpha, boolean writeProtected) {
185		this("undefined", (float)r, (float)g, (float)b, (float)alpha, writeProtected);
186	}
187	
188	/**
189	 * Instantiates a new mT color.
190	 * 
191	 * @param name the name
192	 * @param r the r
193	 * @param g the g
194	 * @param b the b
195	 */
196	public MTColor(String name, int r, int g, int b) {
197		this(name, (float)r, (float)g, (float)b, 255f);
198	}
199	
200	
201	/**
202	 * Instantiates a new mT color.
203	 * 
204	 * @param name the name
205	 * @param r the r
206	 * @param g the g
207	 * @param b the b
208	 * @param alpha the alpha
209	 */
210	public MTColor(String name, float r, float g, float b, float alpha) {
211		this(name, r, g, b, alpha, false);
212	}
213	
214	
215	public MTColor(String name, float r, float g, float b, float alpha, boolean writeProtected) {
216		this.setColor(r, g, b, alpha);
217		this.name = name;
218		this.writeProtected = writeProtected;
219	}
220	
221	
222	public boolean isWriteProtected() {
223		return writeProtected;
224	}
225
226
227	/**
228	 * Gets the r.
229	 * 
230	 * @return the r
231	 */
232	public float getR() {
233		return r;
234	}
235	
236	/**
237	 * Sets the r.
238	 * 
239	 * @param r the new r
240	 */
241	public void setR(float r) {
242		if (!this.isWriteProtected())
243			this.r = r;
244	}
245	
246	/**
247	 * Gets the g.
248	 * 
249	 * @return the g
250	 */
251	public float getG() {
252		return g;
253	}
254	
255	/**
256	 * Sets the g.
257	 * 
258	 * @param g the new g
259	 */
260	public void setG(float g) {
261		if (!this.isWriteProtected())
262			this.g = g;
263	}
264	
265	/**
266	 * Gets the b.
267	 * 
268	 * @return the b
269	 */
270	public float getB() {
271		return b;
272	}
273	
274	/**
275	 * Sets the b.
276	 * 
277	 * @param b the new b
278	 */
279	public void setB(float b) {
280		if (!this.isWriteProtected())
281			this.b = b;
282	}
283	
284	/**
285	 * Gets the alpha.
286	 * 
287	 * @return the alpha
288	 */
289	public float getAlpha() {
290		return alpha;
291	}
292	
293	/**
294	 * Sets the alpha.
295	 * 
296	 * @param alpha the new alpha
297	 */
298	public void setAlpha(float alpha) {
299		if (!this.isWriteProtected())
300			this.alpha = alpha;
301	}
302	
303	/**
304	 * Sets the color.
305	 * 
306	 * @param r the r
307	 * @param g the g
308	 * @param b the b
309	 * @param alpha the alpha
310	 */
311	public void setColor(float r, float g, float b, float alpha){
312		if (!this.isWriteProtected()){
313			this.r = r;
314			this.g = g;
315			this.b = b;
316			this.alpha = alpha;
317		}
318	}
319
320	/**
321	 * Sets the color.
322	 * 
323	 * @param r the r
324	 * @param g the g
325	 * @param b the b
326	 */
327	public void setColor(float r, float g, float b){
328		if (!this.isWriteProtected()){
329			this.r = r;
330			this.g = g;
331			this.b = b;
332			this.alpha = 255;
333		}
334	}
335	
336	/**
337	 * Sets the color.
338	 * 
339	 * @param f the new color
340	 */
341	public void setColor(float f){
342		if (!this.isWriteProtected()){
343			this.r = f;
344			this.g = f;
345			this.b = f;
346			this.alpha = 255;
347		}
348	}
349	
350	/**
351	 * Gets the name.
352	 * 
353	 * @return the name
354	 */
355	public String getName() {
356		return name;
357	}
358	
359	/**
360	 * Gets the copy.
361	 * 
362	 * @return the copy
363	 */
364	public MTColor getCopy(){
365		return new MTColor(this.getName(), this.getR(),this.getG(),this.getB(), this.getAlpha());
366	}
367	
368	/*
369	public static void fill(AbstractShape as, String colorName){
370		ColorManager.getInstance().fill(as, colorName);
371	}
372	public static void stroke(AbstractShape as, String colorName){
373		ColorManager.getInstance().stroke(as, colorName);
374	}
375	public static void fill(AbstractShape as, String colorName, float alpha){
376		ColorManager.getInstance().fill(as, colorName, alpha);
377	}
378	public static void stroke(AbstractShape as, String colorName, float alpha){
379		ColorManager.getInstance().stroke(as, colorName, alpha);
380	}
381	public static Color get(String colorName){
382		return ColorManager.getInstance().getColor(colorName);
383	}
384	*/
385	
386	
387	/**
388	 * To color string.
389	 * 
390	 * @return the string
391	 */
392	public String toColorString(){
393			return "Color{" + r + "," + g + "," + b + "_" + alpha + "}";
394	}
395	
396	
397	
398	/* (non-Javadoc)
399	 * @see java.lang.Object#toString()
400	 */
401	@Override
402	public String toString(){
403		return this.toColorString();
404	}
405	
406	/* (non-Javadoc)
407	 * @see java.lang.Object#equals(java.lang.Object)
408	 */
409	@Override
410	public boolean equals(Object obj) {
411		return (obj instanceof MTColor && this.toColorString().equals(((MTColor)obj).toColorString()));
412	}
413	
414	/* (non-Javadoc)
415	 * @see java.lang.Object#hashCode()
416	 */
417	@Override
418	public int hashCode() {
419		return this.toColorString().hashCode();
420	}
421}
422