summaryrefslogtreecommitdiff
path: root/labs/lab02_simpleRGB/SimpleRGB.java
blob: 5efc0bdc05d13f9677003f89055e8713fb5d9c06 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
public class SimpleRGB
{
	// TO DO: Instant Variables
	private int width;
	private int height;
	private int[] reds;
	private int[] blues;
	private int[] greens;

	public SimpleRGB(int aWidth, int aHeight)
	{
		this.width = aWidth;
		this.height = aHeight;
		reds = new int[width*height];
		blues = new int[width*height];
		greens = new int[width*height];
	}
	
	/**
	 * Gets the width of this image.
	 * @return the width of this image.
	 */
	public int getWidth()
	{
		return this.width;
	}
	
	/**
	 * Gets the height of this image.
	 * @return the height of this image.
	 */
	public int getHeight()
	{
		return this.height;
	}
	
	/**
	 * Sets the red value at coordinate (x,y) to aRed.
	 * @param x the x coordinate of this image.
	 * @param y the y coordinate of this image.
	 * @param aRed the red value (0 - 255)
	 */
	public void setRed(int x, int y, int aRed)
	{
		reds[(y*this.width) + x] = aRed;
	}

	/**
	 * Sets the green value at coordinate (x,y) to aGreen.
	 * @param x the x coordinate of this image.
	 * @param y the y coordinate of this image.
	 * @param aGreen the green value (0 - 255)
	 */
	public void setGreen(int x, int y, int aGreen)
	{
		greens[(y*this.width)+x] = aGreen;
	}

	/**
	 * Sets the blue value at coordinate (x,y) to aBlue.
	 * @param x the x coordinate of this image.
	 * @param y the y coordinate of this image.
	 * @param aBlue the blue value (0 - 255)
	 */
	public void setBlue(int x, int y, int aBlue)
	{
		blues[(y*this.width)+x] = aBlue;
	}
	
	/**
	 * Gets the red value at coordinate (x,y).
	 * @param x the x coordinate of this image.
	 * @param y the y coordinate of this image.
	 * @return the value of red at coordinate (x,y). 
	 */
	public int getRed(int x, int y)
	{
		return reds[(y*this.width) + x];
	}

	/**
	 * Gets the green value at coordinate (x,y).
	 * @param x the x coordinate of this image.
	 * @param y the y coordinate of this image.
	 * @return the value of green at coordinate (x,y). 
	 */
	public int getGreen(int x, int y)
	{
		return greens[(y*this.width)+x];
	}
	
	/**
	 * Gets the blue value at coordinate (x,y).
	 * @param x the x coordinate of this image.
	 * @param y the y coordinate of this image.
	 * @return the value of blue at coordinate (x,y). 
	 */
	public int getBlue(int x, int y)
	{
		return blues[(y*this.width) + x];
	}
	
	/**
	 * Get the NEW image containing only the red color.
	 * The red values of this new image should be exactly
	 * the same as red value of this image. The green and
	 * blue values of this new image should be 0s.
	 * @return the NEW image (SimpleRGB) containing only
	 * the red color of this image.
	 */
	public SimpleRGB getRedImage()
	{
		SimpleRGB output = new SimpleRGB(this.width,this.height);
		for(int i = 0; i < reds.length; i++)
		{
			output.setRed(i%this.width,(i/this.width),reds[i]);
		}
		return output;
	}
	
	/**
	 * Get the NEW image containing only the green color.
	 * The green values of this new image should be exactly
	 * the same as green value of this image. The red and
	 * blue values of this new image should be 0s.
	 * @return the NEW image (SimpleRGB) containing only
	 * the green color of this image.
	 */
	public SimpleRGB getGreenImage()
	{
		SimpleRGB output = new SimpleRGB(this.width,this.height);
		for(int i = 0; i < greens.length; i++)
		{
			output.setGreen(i%this.width,(i/this.width),greens[i]);
		}
		return output;
	}
	
	/**
	 * Get the NEW image containing only the blue color.
	 * The blue values of this new image should be exactly
	 * the same as blue value of this image. The red and
	 * green values of this new image should be 0s.
	 * @return the NEW image (SimpleRGB) containing only
	 * the blue color of this image.
	 */
	public SimpleRGB getBlueImage()
	{
		SimpleRGB output = new SimpleRGB(this.width,this.height);
		for(int i = 0; i < blues.length; i++)
		{
			output.setBlue(i%this.width,(i/this.width),blues[i]);
		}
		return output;
	}
	
	/**
	 * Get the NEW image representing the greyscale of this
	 * image. The grey colors are colors that the red, green
	 * and blue value are exactly the same. To convert an RGB
	 * image into a greyscale image, use the following formula
	 * to calculate the new value.
	 *    (0.21 * red) + (0.72 * green) + (0.07 * blue)
	 * For example, suppose the (R,G,B) value of this image at
	 * coordinate (10,20) are (10,100,200), since
	 *    (0.21 * 10) + (0.72 * 100) + (0.07 * 200) = 88
	 * the (R,G,B) value of the new greyscale image at (10,20)
	 * should be (88,88,88).
	 * @return the NEW image representing the greyscale of this
	 * image.
	 */
	/**
	 *We're not british!
	 *It's grEy in England,
	 *and grAy in America!
	 */
	public SimpleRGB getGreyImage()
	{
		SimpleRGB output = new SimpleRGB(this.width,this.height);
		for(int i = 0; i < reds.length; i++)
		{
			int gray = (int) ((reds[i]*0.21) + (greens[i]*0.72) + (blues[i]*0.07));
			output.setRed(i%this.width,(i/this.width),gray);
			output.setGreen(i%this.width,(i/this.width),gray);
			output.setBlue(i%this.width,(i/this.width),gray);
		}
		return output;
	}
}