Android Open Source - android-gskbyte-utils Bitmap Colorizer






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils 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 (c) 2013 Jose Alcal Correa.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * //from ww w.j  av  a  2s  . c  om
 * Contributors:
 *     Jose Alcal Correa - initial API and implementation
 ******************************************************************************/
package org.gskbyte.bitmap;

import org.gskbyte.util.Logger;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * Utility class to get colorized copies of a given bitmap.
 * */
public class BitmapColorizer
{
protected final Paint colorPaint =          new Paint();
protected final ColorMatrix colorMatrix =   new ColorMatrix();
protected final Canvas colorCanvas =        new Canvas();

protected final Bitmap        baseBitmap;
protected final int           bitmapWidth, bitmapHeight;
protected final Bitmap.Config outputConfig;

/**
 * Constructor.
 * @param bitmap The source bitmap to which to apply color.
 * @param outputConfig The bitmapConfig to use for the resulting bitmaps.
 * */
public BitmapColorizer(Bitmap bitmap, Bitmap.Config outputConfig)
{
    this.baseBitmap = bitmap;
    this.bitmapWidth = this.baseBitmap.getWidth();
    this.bitmapHeight = this.baseBitmap.getHeight();
    this.outputConfig = outputConfig;
}

/**
 * Constructor to load a bitmap from resources
 * @param Context the context from which to load the bitmap resource
 * @param drawableResource The drawable id to load the bitmap
 * @param outputConfig The bitmapConfig to use for the resulting bitmaps.
 * */
public BitmapColorizer(Context context, int drawableResource, Bitmap.Config outputConfig)
{
    this(BitmapFactory.decodeResource(context.getResources(), drawableResource), outputConfig);
}

/**
 * Applies the color given with the integer to a copy of the source bitmap,
 * which is returned. This bitmap has a transparent background (if the current
 * bitmapConfig supports it)
 * @param color The color to apply to the bitmap.
 * @return A colorized copy of the source bitmap.
 * */
public Bitmap colorize(int color)
{
    int a = Color.alpha(color),
        r = Color.red(color),
        g = Color.green(color),
        b = Color.blue(color);
    return colorize(a, r, g, b, Color.TRANSPARENT);
}

/**
 * Applies the color given with the integer to a copy of the source bitmap,
 * which is returned.
 * @param color The color to apply to the bitmap.
 * @param bgcolor The background color for the returned copy.
 * @return A colorized copy of the source bitmap with a defined background color.
 * */
public Bitmap colorize(int color, int bgcolor)
{
    int a = Color.alpha(color),
        r = Color.red(color),
        g = Color.green(color),
        b = Color.blue(color);
    
    return colorize(a, r, g, b, bgcolor);
}

/**
 * Applies the color given with the integer to a copy of the source bitmap,
 * which is returned. This bitmap has a transparent background (if the current
 * bitmapConfig supports it)
 * @param a alpha component of the color, in the range [0,255]
 * @param r red component of the color, in the range [0,255]
 * @param g green component of the color, in the range [0,255]
 * @param b blue component of the color, in the range [0,255]
 * @return A colorized copy of the source bitmap.
 * */
public Bitmap colorize(int a, int r, int g, int b)
{
    return colorize(a, r, g, b, Color.TRANSPARENT);
}


/**
 * Applies the color given with the integer to a copy of the source bitmap,
 * which is returned. This bitmap has a transparent background (if the current
 * bitmapConfig supports it)
 * @param a alpha component of the color, in the range [0,255]
 * @param r red component of the color, in the range [0,255]
 * @param g green component of the color, in the range [0,255]
 * @param b blue component of the color, in the range [0,255]
 * @param bgcolor The background color for the returned copy.
 * @return A colorized copy of the source bitmap.
 * */
public Bitmap colorize(int a, int r, int g, int b, int bgcolor)
{
    try {
        final float fa = a/256.0f, fr = r / 256.0f, fg = g / 256.0f, fb = b / 256.0f;

        colorMatrix.setScale(fr, fg, fb, fa);
        colorPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        final Bitmap ret = Bitmap.createBitmap(bitmapWidth, bitmapHeight, outputConfig);
        colorCanvas.setBitmap(ret);
        if(bgcolor != Color.TRANSPARENT)
            colorCanvas.drawColor(bgcolor);
        
        colorCanvas.drawBitmap(baseBitmap, 0, 0, colorPaint);

        return ret;
    } catch(Exception e) {
        Logger.except(getClass(), e);
        return baseBitmap;
    }
}
}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java