Android Open Source - holoaccent Bitmap Utils






From Project

Back to project page holoaccent.

License

The source code is released under:

Apache License

If you think the Android project holoaccent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2013 NEGU Soft//from   www  .ja  v  a2 s.c o  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.negusoft.holoaccent.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtils {
  
  /**
   * Creates a copy of the bitmap by replacing the color of every pixel 
   * by accentColor while keeping the alpha value.
   * @param bitmap The original bitmap.
   * @param accentColor The color to apply to every pixel.
   * @return A copy of the given bitmap with the accent color applied.
   */
  public static Bitmap applyColor(Bitmap bitmap, int accentColor) {
    int r = Color.red(accentColor);
    int g = Color.green(accentColor);
    int b = Color.blue(accentColor);
    
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    
    for (int i=0; i<pixels.length; i++) {
      int color = pixels[i];
      int alpha = Color.alpha(color);
      pixels[i] = Color.argb(alpha, r, g, b);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
  }
  
  /**
   * Creates a copy of the bitmap by calculating the transformation based on the 
   * original color and applying it to the the destination color.
   * @param bitmap The original bitmap.
   * @param originalColor Tint color in the original bitmap.
   * @param destinationColor Tint color to be applied.
   * @return A copy of the given bitmap with the tint color changed.
   */
  public static Bitmap changeTintColor(Bitmap bitmap, int originalColor, int destinationColor) {
    // original tint color
    int[] o = new int[] {
        Color.red(originalColor),
        Color.green(originalColor),
        Color.blue(originalColor) };
    // destination tint color
    int[] d = new int[] {
        Color.red(destinationColor),
        Color.green(destinationColor),
        Color.blue(destinationColor) };
    
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    int maxIndex = getMaxIndex(o);
    int mintIndex = getMinIndex(o);
    
    for (int i=0; i<pixels.length; i++) {
      int color = pixels[i];
      // pixel color
      int[] p = new int[] {
          Color.red(color),
          Color.green(color),
          Color.blue(color) };
      int alpha = Color.alpha(color);

      float[] transformation = calculateTransformation(o[maxIndex], o[mintIndex], p[maxIndex], p[mintIndex]);
      pixels[i] = applyTransformation(d, alpha, transformation);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
  }
  
  /**
   * Create a bitmap that contains the transformation to make to create the final 
   * bitmap with a new tint color. You can then call processTintTransformationMap() 
   * to get a bitmap with the desired tint.
   * @param bitmap The original bitmap.
   * @param tintColor Tint color in the original bitmap.
   * @return A transformation map to be used with processTintTransformationMap(). The 
   * transformation values are stored in the red and green values. The alpha value is 
   * significant and the blue value can be ignored.
   */
  public static Bitmap createTintTransformationMap(Bitmap bitmap, int tintColor) {
    // tint color
    int[] t = new int[] {
        Color.red(tintColor),
        Color.green(tintColor),
        Color.blue(tintColor) };
    
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    int maxIndex = getMaxIndex(t);
    int mintIndex = getMinIndex(t);
    
    for (int i=0; i<pixels.length; i++) {
      int color = pixels[i];
      // pixel color
      int[] p = new int[] {
          Color.red(color),
          Color.green(color),
          Color.blue(color) };
      int alpha = Color.alpha(color);

      float[] transformation = calculateTransformation(t[maxIndex], t[mintIndex], p[maxIndex], p[mintIndex]);
      pixels[i] = Color.argb(alpha, (int)(transformation[0]*255), (int)(transformation[1]*255), 0);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
  }
  
  /**
   * Apply the given tint color to the transformation map.
   * @param transformationMap A bitmap representing the transformation map.
   * @param tintColor Tint color to be applied.
   * @return A bitmap with the with the tint color set.
   */
  public static Bitmap processTintTransformationMap(Bitmap transformationMap, int tintColor) {
    // tint color
    int[] t = new int[] {
        Color.red(tintColor),
        Color.green(tintColor),
        Color.blue(tintColor) };
    
    int width = transformationMap.getWidth();
    int height = transformationMap.getHeight();
    int[] pixels = new int[width * height];
    transformationMap.getPixels(pixels, 0, width, 0, 0, width, height);
    
    float[] transformation = new float[2];
    
    for (int i=0; i<pixels.length; i++) {
      int color = pixels[i];
      int alpha = Color.alpha(color);
      transformation[0] = Color.red(color) / 255f;
      transformation[1] = Color.green(color) / 255f;
      pixels[i] = applyTransformation(t, alpha, transformation);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
  }
  
  private static int getMaxIndex(int[] values) {
    int result = 0;
    for (int i=1; i<values.length; i++) {
      if (values[result] < values[i])
        result = i;
    }
    return result;
  }
  
  private static int getMinIndex(int[] values) {
    int result = 0;
    for (int i=1; i<values.length; i++) {
      if (values[result] > values[i])
        result = i;
    }
    return result;
  }
  
  private static float[] calculateTransformation(int tintA, int tintB, int colorA, int colorB) {
    float a = tintA / 255f;
    float aP = tintB / 255f;
    float b = colorA / 255f;
    float bP = colorB / 255f;
    
    float[] result = new float[2];
    result[0] = (b-bP) / ((b*aP)+a-(bP*a)-aP);
    result[1] = (bP-(aP*result[0])) / (1f-(aP*result[0]));
    
    return result;
  }
  
  private static int applyTransformation(int[] tintComponents, int alpha, float[] transformation) {
    return Color.argb(
        alpha,
        (int)applyTransformation(tintComponents[0], transformation),
        (int)applyTransformation(tintComponents[1], transformation),
        (int)applyTransformation(tintComponents[2], transformation));
  }
  
  private static float applyTransformation(int colorComponent, float[] transformation) {
    float firstStep = colorComponent*transformation[0];
    return firstStep + ((255-firstStep) * transformation[1]);
  }
  
  /**
   * Write the given bitmap to a file in the external storage. Requires 
   * "android.permission.WRITE_EXTERNAL_STORAGE" permission.
   */
  public static void writeToFile(Bitmap bitmap, String dir, String filename) throws FileNotFoundException, IOException {
    File sdCard = Environment.getExternalStorageDirectory();
    File dirFile = new File (sdCard.getAbsolutePath() + "/" + dir);
    dirFile.mkdirs();
    File f = new File(dirFile, filename);
    FileOutputStream fos = new FileOutputStream(f, false);
    bitmap.compress(CompressFormat.PNG, 100 /*ignored for PNG*/, fos);
    fos.flush();
    fos.close();
  }

}




Java Source Code List

com.negusoft.holoaccent.AccentHelper.java
com.negusoft.holoaccent.AccentPalette.java
com.negusoft.holoaccent.AccentResources.java
com.negusoft.holoaccent.activity.AccentActivity.java
com.negusoft.holoaccent.dialog.AccentAlertDialog.java
com.negusoft.holoaccent.dialog.AccentDatePickerDialog.java
com.negusoft.holoaccent.dialog.AccentDialogFragment.java
com.negusoft.holoaccent.dialog.AccentTimePickerDialog.java
com.negusoft.holoaccent.dialog.DividerPainter.java
com.negusoft.holoaccent.dialog.NumberPickerPainter.java
com.negusoft.holoaccent.drawable.ActionBarBackgroundDrawable.java
com.negusoft.holoaccent.drawable.CircleDrawable.java
com.negusoft.holoaccent.drawable.ContactBadgeDrawable.java
com.negusoft.holoaccent.drawable.FastScrollDrawable.java
com.negusoft.holoaccent.drawable.IndeterminedProgressDrawable.java
com.negusoft.holoaccent.drawable.IndeterminedProgressLegacyDrawable.java
com.negusoft.holoaccent.drawable.RectDrawable.java
com.negusoft.holoaccent.drawable.RoundRectDrawable.java
com.negusoft.holoaccent.drawable.ScrubberControlSelectorDrawable.java
com.negusoft.holoaccent.drawable.ScrubberProgressDrawable.java
com.negusoft.holoaccent.drawable.SearchViewDrawable.java
com.negusoft.holoaccent.drawable.SpinnerDrawable.java
com.negusoft.holoaccent.drawable.ToggleForegroundDrawable.java
com.negusoft.holoaccent.drawable.UnderlineDrawable.java
com.negusoft.holoaccent.example.activity.AccentFragmentActivity.java
com.negusoft.holoaccent.example.activity.DialogActivity.java
com.negusoft.holoaccent.example.activity.MainActivity.java
com.negusoft.holoaccent.example.activity.PreferencesActivity.java
com.negusoft.holoaccent.example.activity.SpinnerActivity.java
com.negusoft.holoaccent.example.activity.TabbedActivity.java
com.negusoft.holoaccent.example.activity.TabbedStripActivity.java
com.negusoft.holoaccent.example.activity.themed.DialogActivityLight.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.PreferencesActivityLight.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.SpinnerActivityLight.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedActivityLight.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLightColoredABInverse.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLightColoredAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLightDarkAB.java
com.negusoft.holoaccent.example.activity.themed.TabbedStripActivityLight.java
com.negusoft.holoaccent.example.fragment.AccentSupportDialogFragment.java
com.negusoft.holoaccent.example.fragment.ButtonFragment.java
com.negusoft.holoaccent.example.fragment.ChoicesFragment.java
com.negusoft.holoaccent.example.fragment.ListFragment.java
com.negusoft.holoaccent.example.fragment.PickersFragment.java
com.negusoft.holoaccent.example.fragment.ProgressFragment.java
com.negusoft.holoaccent.example.fragment.SimpleDialogFragment.java
com.negusoft.holoaccent.example.fragment.TextviewFragment.java
com.negusoft.holoaccent.example.model.ColorOverrideConfig.java
com.negusoft.holoaccent.example.preference.CustomDialogPreference.java
com.negusoft.holoaccent.example.view.CheckableTextView.java
com.negusoft.holoaccent.interceptor.AccentColorInterceptor.java
com.negusoft.holoaccent.interceptor.ActionBarBackgroundInterceptor.java
com.negusoft.holoaccent.interceptor.CircleInterceptor.java
com.negusoft.holoaccent.interceptor.ContactBadgeInterceptor.java
com.negusoft.holoaccent.interceptor.FastScrollInterceptor.java
com.negusoft.holoaccent.interceptor.IndeterminateInterceptor.java
com.negusoft.holoaccent.interceptor.NativeInterceptor.java
com.negusoft.holoaccent.interceptor.PagerTabStripInterceptor.java
com.negusoft.holoaccent.interceptor.RectInterceptor.java
com.negusoft.holoaccent.interceptor.RoundRectInterceptor.java
com.negusoft.holoaccent.interceptor.ScrubberInterceptor.java
com.negusoft.holoaccent.interceptor.SearchViewTextFieldInterceptor.java
com.negusoft.holoaccent.interceptor.SolidColorInterceptor.java
com.negusoft.holoaccent.interceptor.SpinnerInterceptor.java
com.negusoft.holoaccent.interceptor.ToggleInterceptor.java
com.negusoft.holoaccent.interceptor.UnderlineInterceptor.java
com.negusoft.holoaccent.preference.DialogPreference.java
com.negusoft.holoaccent.preference.EditTextPreference.java
com.negusoft.holoaccent.preference.ListPreference.java
com.negusoft.holoaccent.preference.MultiSelectListPreference.java
com.negusoft.holoaccent.preference.SwitchPreference.java
com.negusoft.holoaccent.util.BitmapUtils.java
com.negusoft.holoaccent.util.NativeResources.java
com.negusoft.holoaccent.widget.AccentDatePicker.java
com.negusoft.holoaccent.widget.AccentNumberPicker.java
com.negusoft.holoaccent.widget.AccentQuickContactBadge.java
com.negusoft.holoaccent.widget.AccentRatingBar.java
com.negusoft.holoaccent.widget.AccentSearchView.java
com.negusoft.holoaccent.widget.AccentSwitch.java
com.negusoft.holoaccent.widget.AccentTimePicker.java