-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPixel.java
More file actions
executable file
·188 lines (167 loc) · 4.44 KB
/
Pixel.java
File metadata and controls
executable file
·188 lines (167 loc) · 4.44 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bric.Image;
/**
*
* @author BrianCarlsen
*/
public class Pixel implements Comparable<Pixel> {
private int[] position; //row, col
private int red;
private int green;
private int blue;
public Pixel (int[] p, int r, int g, int b) {
position = p;
red = r;
green = g;
blue = b;
}
public Pixel (int[] p, int... rgb) {
position = p;
//TODO throw exception
if (rgb.length != 3) {
red = 0;
green = 0;
blue = 0;
}
else {
red = rgb[0];
green = rgb[1];
blue = rgb[2];
}
}
public Pixel(int[] p) {
this(p, 0, 0, 0);
}
public int[] getPosition() {
return position;
}
public void setPosition(int r , int c) {
position[0] = r;
position[1] = c;
}
public void setPosition(int[] rc) {
position = rc;
}
public int getRow() {
return position[0];
}
public void setRow(int r) {
position[0] = r;
}
public int getColumn() {
return position[1];
}
public void setColumn(int c) {
position[1] = c;
}
public int getRed() {
return red;
}
public void setRed(int r) {
red = r;
}
public int getGreen() {
return green;
}
public void setGreen(int g) {
green = g;
}
public int getBlue() {
return blue;
}
public void setBlue(int b) {
blue = b;
}
public int getColor(ColorType c) {
if (c == ColorType.RED)
return red;
else if (c== ColorType.GREEN)
return green;
else if (c == ColorType.BLUE)
return blue;
else if (c == ColorType.YELLOW)
return (red + green)/2;
else if (c == ColorType.BLACK || c == ColorType.WHITE)
return getGreyScale();
else
return -1;
}
public int[] getColors() {
return new int[] {red, green, blue};
}
public void setColor(ColorType c, int val) {
if (c == ColorType.RED)
red = val;
else if (c== ColorType.GREEN)
green = val;
else if (c == ColorType.BLUE)
blue = val;
else if (c == ColorType.YELLOW) {
red += val;
green += val;
}
else if (c == ColorType.BLACK || c == ColorType.WHITE) {
red = val;
green = val;
blue = val;
}
else //TODO: throw error
System.out.println("No Color");
}
public int getGreyScale() {
return (red + green + blue) / 3;
}
public int toInt() {
int out = (red << 16) & 0x00ff0000;
out |= (green << 8) & 0x0000ff00;
out |= blue & 0x000000ff;
out |= 0xff000000;
return out;
}
public void show() {
System.out.print("Position: " + position[0] + ", " + position[1]);
System.out.println("\tColors: " + red + ", " + green + ", " + blue);
}
@Override
public int compareTo(Pixel other) {
//sort by row then column
int[] oPos = other.getPosition();
//rows equal
if (position[0] == oPos[0]) {
if (position[1] < oPos[1])
return -1;
else if (position [1] > oPos[1])
return 1;
else
return 0;
}
else { //rows not equal
if (position[0] < oPos[0])
return -1;
else
return 1;
}
}
public boolean equals(Pixel other) {
if (position[0] == other.getRow() &&
position[1] == other.getColumn() &&
red == other.getRed() &&
green == other.getGreen() &&
blue == other.getBlue())
return true;
return false;
}
public static void main(String... args) {
Pixel p1 = new Pixel(new int[] {2, 1});
Pixel p2 = new Pixel(new int[] {1, 2});
System.out.println(p1.compareTo(p2));
System.out.println("NEXT");
p1.setColor(ColorType.RED, 255);
p1.setGreen(255/2);
p1.show();
System.out.println("\n" + Integer.toHexString(p1.toInt()));
}
}