Commit 40969337af1337096b702d6c0953e47a5fddca6d
1 parent
ee22526e
Exists in
master
and in
1 other branch
Apresentação
git-svn-id: https://svn.bento.ifrs.edu.br/default/ASES/ASES%20-%20Web/ASES%20-%20Web/Fontes/avaliador-api@10342 c2178572-b5ca-4887-91d2-9e3a90c7d55b
Showing
1 changed file
with
139 additions
and
0 deletions
Show diff stats
src/main/java/br/com/checker/emag/AvaliadorContraste.java
0 → 100644
... | ... | @@ -0,0 +1,139 @@ |
1 | +package br.com.checker.emag; | |
2 | +import java.awt.Color; | |
3 | +import java.text.DecimalFormat; | |
4 | + | |
5 | +public class AvaliadorContraste { | |
6 | + | |
7 | + private Color corMaisClara; | |
8 | + | |
9 | + private Color corMaisEscura; | |
10 | + | |
11 | + private double contraste; | |
12 | + | |
13 | + private final double fatorDeLuminosidade = 0.05; | |
14 | + | |
15 | + private final double valorMaximoDaCorSRGB = 255.0; | |
16 | + | |
17 | + private final double multiplicadorDoVermelho = 0.2126; | |
18 | + | |
19 | + private final double multiplicadorDoVerde = 0.7152; | |
20 | + | |
21 | + private final double multiplicadorDoAzul = 0.0722; | |
22 | + | |
23 | + private final double limiteSRGB = 0.03928; | |
24 | + | |
25 | + private final double divisorSRGB = 12.92; | |
26 | + | |
27 | + private final double divisorAlternativoSRGB = 1.055; | |
28 | + | |
29 | + private final double fatorDeCorrecaoSRGB = 0.055; | |
30 | + | |
31 | + private final double potenciaSRGB = 2.4; | |
32 | + | |
33 | + | |
34 | + /* | |
35 | + * Construtor padrão que seta as cores do avaliador como | |
36 | + * banca e branca para caso se chame o método avaliar não ocorra | |
37 | + * nenhuma Exception. | |
38 | + */ | |
39 | + public AvaliadorContraste() { | |
40 | + this.setCores(Color.WHITE, Color.WHITE); | |
41 | + } | |
42 | + | |
43 | + /* | |
44 | + * Construtor para passar as cores que se deseja avaliar. | |
45 | + */ | |
46 | + public AvaliadorContraste(Color cor1, Color cor2) { | |
47 | + this.setCores(cor1, cor2); | |
48 | + } | |
49 | + | |
50 | + | |
51 | + /* | |
52 | + * Seta as cores que serão avaliadas. | |
53 | + */ | |
54 | + public void setCores(Color cor1, Color cor2) { | |
55 | + if (cor1 != null && cor2 != null) { | |
56 | + int cor1Soma = cor1.getRed() + cor1.getGreen() + cor1.getBlue(); | |
57 | + int cor2Soma = cor2.getRed() + cor2.getGreen() + cor2.getBlue(); | |
58 | + if (cor1Soma >= cor2Soma) { | |
59 | + this.corMaisClara = cor1; | |
60 | + this.corMaisEscura = cor2; | |
61 | + } else { | |
62 | + this.corMaisClara = cor2; | |
63 | + this.corMaisEscura = cor1; | |
64 | + } | |
65 | + } | |
66 | + } | |
67 | + | |
68 | + /* | |
69 | + * Método que calcula e armazena o contraste das cores avaliadas | |
70 | + * no atributo 'constraste'. | |
71 | + */ | |
72 | + public void avaliar() { | |
73 | + double L1; | |
74 | + double L2; | |
75 | + | |
76 | + L1 = calcularLuminecencia(this.corMaisClara); | |
77 | + L2 = calcularLuminecencia(this.corMaisEscura); | |
78 | + | |
79 | + this.contraste = (L1 + this.fatorDeLuminosidade) / (L2 + fatorDeLuminosidade); | |
80 | + } | |
81 | + | |
82 | + /* | |
83 | + * Método para calcular a luminecência de uma cor. | |
84 | + */ | |
85 | + private double calcularLuminecencia(Color cor) { | |
86 | + double L; | |
87 | + | |
88 | + double R; | |
89 | + double G; | |
90 | + double B; | |
91 | + | |
92 | + double RsRGB; | |
93 | + double GsRGB; | |
94 | + double BsRGB; | |
95 | + | |
96 | + RsRGB = cor.getRed() / this.valorMaximoDaCorSRGB; | |
97 | + GsRGB = cor.getGreen() / this.valorMaximoDaCorSRGB; | |
98 | + BsRGB = cor.getBlue() / this.valorMaximoDaCorSRGB; | |
99 | + | |
100 | + if (RsRGB <= this.limiteSRGB) { | |
101 | + R = RsRGB / this.divisorSRGB; | |
102 | + } else { | |
103 | + R = Math.pow(((RsRGB + this.fatorDeCorrecaoSRGB) / this.divisorAlternativoSRGB), this.potenciaSRGB); | |
104 | + } | |
105 | + | |
106 | + if (GsRGB <= this.limiteSRGB) { | |
107 | + G = GsRGB / this.divisorSRGB; | |
108 | + } else { | |
109 | + G = Math.pow(((GsRGB + this.fatorDeCorrecaoSRGB) / this.divisorAlternativoSRGB), this.potenciaSRGB); | |
110 | + } | |
111 | + | |
112 | + if (BsRGB <= this.limiteSRGB) { | |
113 | + B = BsRGB / this.divisorSRGB; | |
114 | + } else { | |
115 | + B = Math.pow(((BsRGB + this.fatorDeCorrecaoSRGB) / this.divisorAlternativoSRGB), this.potenciaSRGB); | |
116 | + } | |
117 | + | |
118 | + | |
119 | + L = (R * this.multiplicadorDoVermelho) + (G * this.multiplicadorDoVerde) + (B * this.multiplicadorDoAzul); | |
120 | + | |
121 | + return L; | |
122 | + } | |
123 | + | |
124 | + /* | |
125 | + * Metodo que retorna o valor armazenado no atributo 'contraste'. | |
126 | + */ | |
127 | + public double getContraste() { | |
128 | + return this.contraste; | |
129 | + } | |
130 | + | |
131 | + /* | |
132 | + * Método que retorna o valor armazenado no atributo 'contraste' | |
133 | + * devidamente formatado. | |
134 | + */ | |
135 | + public String getContrasteFormatado() { | |
136 | + String strContraste = String.valueOf(new DecimalFormat("#.##").format(this.contraste)); | |
137 | + return strContraste.replace(".", ","); | |
138 | + } | |
139 | +} | ... | ... |