-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCorRGB.java
More file actions
216 lines (176 loc) · 5.56 KB
/
CorRGB.java
File metadata and controls
216 lines (176 loc) · 5.56 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class CorRGB
{
private int valorRed;
private int valorGreen;
private int valorBlue;
private double valorLuminosidade;
public static final CorRGB preto = new CorRGB(0,0,0);
public static final CorRGB branca = new CorRGB(255,255,255);
public static final CorRGB red = new CorRGB(255,0,0);
public static final CorRGB green = new CorRGB(0,255,0);
public static final CorRGB blue = new CorRGB(0,0,255);
//construtor cor RGB
CorRGB(int red, int green, int blue)
{
try
{
if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255)
throw new Exception("Número inválido");
else {
this.setValorRed(red);
this.setValorGreen(green);
this.setValorBlue(blue);
this.setLuminosidade();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//CorPreta
CorRGB()
{
this.setValorRed(0);
this.setValorGreen(0);
this.setValorBlue(0);
this.setLuminosidade();
}
//Copia
CorRGB(CorRGB x){
this.setValorRed(x.getValorRed());
this.setValorGreen(x.getValorGreen());
this.setValorBlue(x.getValorBlue());
this.setLuminosidade();
}
public int getValorRed() {
return valorRed;
}
protected void setValorRed(int valorRed)
{
if(valorRed >= 255)
{this.valorRed = 255;}
else
{
if(valorRed < 0)
{this.valorRed = 0;}
else
{this.valorRed = valorRed;}
}
this.setLuminosidade();
}
public int getValorGreen() {
return valorGreen;
}
protected void setValorGreen(int valorGreen) {
if(valorGreen >= 255)
{this.valorGreen = 255;}
else
{
if(valorGreen < 0)
{this.valorGreen = 0;}
else
{this.valorGreen = valorGreen;}
}
this.setLuminosidade();
}
public int getValorBlue() {
return valorBlue;
}
protected void setValorBlue(int valorBlue) {
if(valorBlue >= 255)
{this.valorBlue = 255;}
else
{
if(valorBlue < 0)
{this.valorBlue = 0;}
else
{this.valorBlue = valorBlue;}
}
this.setLuminosidade();
}
public double getLuminosidade()
{
return valorLuminosidade;
}
private void setLuminosidade()
{
this.valorLuminosidade = (this.getValorRed()*0.3 + this.getValorGreen()*0.59 + this.getValorBlue()*0.11);
}
public CorRGB clonar()
{
CorRGB clone = new CorRGB(this.getValorRed(),this.getValorGreen(),this.getValorBlue());
return clone;
}
public boolean setIgualdadeCores(CorRGB x)
{
if (this.getValorRed() == x.getValorRed() &&
this.getValorGreen() == x.getValorGreen() &&
this.getValorBlue() == x.getValorBlue())
{return true;}
else
{return false;}
}
private String conversorHexadecimal(int valor) {
int dezenaHexa;
int unidadeHexa;
String dezenaHexaStr = "";
String unidadeHexaStr = "";
dezenaHexa = (int) valor/16;
unidadeHexa = (int) ((valor/16.0 - dezenaHexa) * 16.0);
switch(unidadeHexa)
{
case 10: unidadeHexaStr = "A";break;
case 11: unidadeHexaStr = "B";break;
case 12: unidadeHexaStr = "C";break;
case 13: unidadeHexaStr = "D";break;
case 14: unidadeHexaStr = "E";break;
case 15: unidadeHexaStr = "F";break;
default: unidadeHexaStr = "" + unidadeHexa;
}
switch(dezenaHexa)
{
case 10: dezenaHexaStr = "A";break;
case 11: dezenaHexaStr = "B";break;
case 12: dezenaHexaStr = "C";break;
case 13: dezenaHexaStr = "D";break;
case 14: dezenaHexaStr = "E";break;
case 15: dezenaHexaStr = "F";break;
default: dezenaHexaStr = "" + dezenaHexa;
}
return dezenaHexaStr + unidadeHexaStr;
}
public String corRGBHexadecimal(){
String r= "RGB em Hexa: #";
r += this.conversorHexadecimal(getValorRed()) +
this.conversorHexadecimal(getValorGreen())+
this.conversorHexadecimal(getValorBlue());
return r;
}
public void clarear(float percentual)
{
setValorRed(getValorRed() + (int)(getValorRed()*percentual));
setValorGreen(getValorGreen() + (int)(getValorGreen()*percentual));
setValorBlue(getValorBlue() + (int)(getValorBlue()*percentual));
}
public void escurecer(float percentual)
{
setValorRed(getValorRed() - (int)(getValorRed()*percentual));
setValorGreen(getValorGreen() - (int)(getValorGreen()*percentual));
setValorBlue(getValorBlue() - (int)(getValorBlue()*percentual));
}
public CorRGB corCinza()
{
int x = (int) this.getLuminosidade();
CorRGB corCinza = new CorRGB(x, x, x);
return corCinza;
}
public String toString(){
String s = "";
s += "valor de vermelho: "+ this.valorRed +"\n"+
"valor de verde: "+ this.valorGreen +"\n"+
"valor de azul: " + this.valorBlue +"\n"+
"Luminosidade: " + this.valorLuminosidade +"\n"+
"Escala de Cinza: "+ corCinza() +"\n"+
"Cor em Hexa: " + corRGBHexadecimal()+"\n";;
return s;
}
}