11package cafe .adriel .androidaudiorecorder ;
22
33import android .graphics .Color ;
4+ import android .graphics .PorterDuff ;
45import android .graphics .drawable .ColorDrawable ;
6+ import android .graphics .drawable .Drawable ;
57import android .os .Bundle ;
68import android .support .v7 .app .AppCompatActivity ;
79import android .view .Menu ;
@@ -30,7 +32,7 @@ public class AudioRecorderActivity extends AppCompatActivity {
3032 private boolean isRecording ;
3133 private String filePath ;
3234 private int color ;
33-
35+ private boolean isBright ; //variable to check brightness
3436 private RelativeLayout contentLayout ;
3537 private TextView timerView ;
3638 private ImageView micView ;
@@ -59,7 +61,21 @@ protected void onCreate(Bundle savedInstanceState) {
5961 micView = (ImageView ) findViewById (R .id .mic );
6062 recordView = (ImageButton ) findViewById (R .id .record );
6163
64+ // to get drawable resources of check icon and clear icon
65+ Drawable clear = getResources ().getDrawable (R .drawable .ic_clear );
66+ Drawable check = getResources ().getDrawable (R .drawable .ic_check );
67+
6268 contentLayout .setBackgroundColor (color );
69+
70+ // check to set tint of images
71+ isBright = isBrightColor (color );
72+ if (isBright ) {
73+ micView .setColorFilter (Color .BLACK );
74+ recordView .setColorFilter (Color .BLACK );
75+ timerView .setTextColor (Color .BLACK );
76+ clear .setColorFilter (Color .BLACK , PorterDuff .Mode .SRC_ATOP );
77+ check .setColorFilter (Color .BLACK , PorterDuff .Mode .SRC_ATOP );
78+ }
6379 }
6480
6581 @ Override
@@ -174,4 +190,25 @@ public void run() {
174190 });
175191 }
176192
177- }
193+ /**
194+ * Function to check brightness of background color
195+ * @param color
196+ * @return
197+ */
198+ public boolean isBrightColor (int color ) {
199+ if (android .R .color .transparent == color ) {
200+ return true ;
201+ }
202+
203+ int [] rgb = {Color .red (color ), Color .green (color ), Color .blue (color )};
204+
205+ int brightness = (int ) Math .sqrt (rgb [0 ]*rgb [0 ]*0.241
206+ + rgb [1 ]*rgb [1 ]*0.691 + rgb [2 ]*rgb [2 ]*0.068 );
207+
208+ //color is bright
209+ if (brightness >= 200 ) {
210+ return true ;
211+ }
212+ return false ;
213+ }
214+ }
0 commit comments