Skip to content

Commit

Permalink
Aaron and I trying to fix grayscale calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
williamcaruso committed Mar 14, 2017
1 parent 4338303 commit b10c2c0
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ama/build.gradle
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
buildToolsVersion '25.0.0'

defaultConfig {
minSdkVersion 19
Expand Down
19 changes: 10 additions & 9 deletions ama/src/main/java/edu/mit/dig/ama/core/AMA.java
Expand Up @@ -190,7 +190,7 @@ public static void setFont(float size, View ... views) {
public static void setViewsToGrayscale(Activity activity) {
List<View> views = getAllViewsAndGroups(activity);
for (View view : views) {
toGrayscale(activity, view, GrayscaleType.AVERAGE);
viewsToGrayscale(activity, view, GrayscaleType.AVERAGE);
}
}

Expand All @@ -200,7 +200,7 @@ public static void setViewsToGrayscale(Activity activity) {
* @param view The view to change with a color
* @return The view modified with the grayscale color
*/
public static View toGrayscale(Activity activity, View view, GrayscaleType gType) {
public static View viewsToGrayscale(Activity activity, View view, GrayscaleType gType) {

if (view instanceof Button) {
// TODO: Find out best way to do this
Expand All @@ -209,6 +209,7 @@ public static View toGrayscale(Activity activity, View view, GrayscaleType gType
//((Button) view).setBackgroundResource(android.);
} else if (view instanceof TextView) {
int color = ((TextView) view).getCurrentTextColor();
Log.e("UHOH", Integer.toHexString(color));
((TextView) view).setTextColor(colorToGrayscale(color, gType));
} else if (view instanceof ImageView) {
ColorMatrix matrix = new ColorMatrix();
Expand All @@ -233,16 +234,16 @@ public static View toGrayscale(Activity activity, View view, GrayscaleType gType
* @return The grayscale color
*/
private static int colorToGrayscale(int color, GrayscaleType gType) {
int[] ARGB = new int[] {(0x11000000 & color) >> 6, (0x110000 & color) >> 4, (0x1100 & color) >> 2, 0x11 & color};
int[] ARGB = new int[] {(0x11000000 & color) >> 24, (0x110000 & color) >> 16, (0x1100 & color) >> 8, 0x11 & color};
switch (gType) {
case AVERAGE:
int scale = (ARGB[1] + ARGB[2] + ARGB[3]) / 3;
int result = (ARGB[0] << 6) + (scale << 4) + (scale << 2) + (scale);
return result;
Log.e("BOOM", Integer.toHexString((ARGB[0] << 24) + (scale << 16) + (scale << 8) + (scale)));
return (ARGB[0] << 24) + (scale << 16) + (scale << 8) + (scale);
case LIGHTNESS:
return Math.max(ARGB[1], Math.max(ARGB[2], ARGB[3])) + Math.min(ARGB[1], Math.min(ARGB[2], ARGB[3]))/ 2;
return Math.max(ARGB[1], Math.max(ARGB[2], ARGB[3])) + Math.min(ARGB[1], Math.min(ARGB[2], ARGB[3])) / 2;
case LUMINOSITY:
return (int) (0.21*ARGB[1] + 0.72*ARGB[2] + 0.07*ARGB[3]);
return (int) (0.21*ARGB[1] + 0.72*ARGB[2] + 0.07*ARGB[3]);
default:
return color;
}
Expand Down Expand Up @@ -309,8 +310,8 @@ public static double getContrast(Context context, @ColorRes int colorForeground,
// This integer represents 0xAARRGGBB
int color1 = context.getResources().getColor(colorForeground);
int color2 = context.getResources().getColor(colorBackground);
int[] oneARGB = new int[] {(0x11000000 & color1) >> 6, (0x110000 & color1) >> 4, (0x1100 & color1) >> 2, 0x11 & color1};
int[] twoARGB = new int[] {(0x11000000 & color2) >> 6, (0x110000 & color2) >> 4, (0x1100 & color2) >> 2, 0x11 & color2};
int[] oneARGB = new int[] {(0x11000000 & color1) >> 24, (0x110000 & color1) >> 16, (0x1100 & color1) >> 8, 0x11 & color1};
int[] twoARGB = new int[] {(0x11000000 & color2) >> 24, (0x110000 & color2) >> 16, (0x1100 & color2) >> 8, 0x11 & color2};

// Calculation for relative luminance, as defined by https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
float oneL = calcLuminance(oneARGB);
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "edu.mit.dig.amaexample"
minSdkVersion 19
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/edu/mit/dig/amaexample/MainActivity.java
Expand Up @@ -114,13 +114,13 @@ private void makeActivityGrayscale() {

if(!isGrayScale) {

grayBtn.setText(getString(R.string.grayscale_btn_on));
grayBtn.setText(getString(R.string.grayscale_btn_off));
isGrayScale = true;
AMA.setViewsToGrayscale(this);

} else {

grayBtn.setText(getString(R.string.grayscale_btn_off));
grayBtn.setText(getString(R.string.grayscale_btn_on));
isGrayScale = false;

}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Sun Mar 12 23:16:10 EDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 comments on commit b10c2c0

Please sign in to comment.