Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d4d01b9a74a31521c40cfa3ff32ee06c1bf27bc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.core;

/**
 * Toolkit-independent representation of a color. Colors consist of three
 * integers in the range 0..255 representing red, green, and blue components.
 * Objects of this class are immutable.
 */
public class Color {

	public static final Color BLACK = new Color(0, 0, 0);

	private final int red;
	private final int green;
	private final int blue;

	/**
	 * Class constructor.
	 * 
	 * @param red
	 *            red value, 0..255
	 * @param green
	 *            green value, 0..255
	 * @param blue
	 *            blue value, 0..255
	 */
	public Color(int red, int green, int blue) {
		this.red = red;
		this.green = green;
		this.blue = blue;
	}

	/**
	 * Returns the blue component of the color, in the range 0..255
	 */
	public int getBlue() {
		return blue;
	}

	/**
	 * Returns the green component of the color, in the range 0..255
	 */
	public int getGreen() {
		return green;
	}

	/**
	 * Returns the red component of the color, in the range 0..255
	 */
	public int getRed() {
		return red;
	}

	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		
		Color other = (Color) obj;
		return    red == other.red
		       && green == other.green
		       && blue == other.blue;
	}

	public int hashCode() {
		return this.red + this.green << 16 + this.blue << 24;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer(20);
		sb.append("Color[r=");
		sb.append(this.red);
		sb.append(",g=");
		sb.append(this.green);
		sb.append(",b=");
		sb.append(this.blue);
		sb.append("]");
		return sb.toString();
	}

}

Back to the top