Android Open Source - yammp Color Analyser






From Project

Back to project page yammp.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project yammp 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

package org.yammp.util;
//www .  j a  v a 2  s. com
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.Color;

/**
 * 
 * @author mariotaku
 * 
 *         Get the main color from a {@link Bitmap}<br>
 * <br>
 * 
 *         <b>Important</b>: I recommand using this method in different thread.
 *         Or it will make your application laggy or unresponceable.
 */
public class ColorAnalyser {

  /**
   * 
   * Get the main color from a {@link Bitmap}.<br>
   * 
   * @param bitmap
   *            The {@link Bitmap} to analyse
   * @return The rgb {@link Color} in integer (no alpha)
   */
  public static int analyse(Bitmap bitmap) {

    return analyse(bitmap, 18, 28);
  }

  /**
   * 
   * Get the main color from a {@link Bitmap}.<br>
   * 
   * @param bitmap
   *            The {@link Bitmap} to analyse
   * @param width
   *            The desired width of scaled bitmap
   * @param height
   *            The desired height of scaled bitmap
   * @return The rgb {@link Color} in integer (no alpha)
   */
  public static int analyse(Bitmap bitmap, int width, int height) {

    if (bitmap == null) return Color.WHITE;

    int color = 0;

    HashMap<Float, Integer> colorsMap = new HashMap<Float, Integer>();
    ArrayList<Float> colorsScore = new ArrayList<Float>();

    Bitmap resized = Bitmap.createScaledBitmap(bitmap, width, height, false);

    for (int y = 0; y < resized.getHeight(); y++) {
      for (int x = 0; x < resized.getWidth(); x++) {
        int temp_color = resized.getPixel(x, y);
        color = Color.argb(0xFF, Color.red(temp_color), Color.green(temp_color),
            Color.blue(temp_color));
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);

        float score = (hsv[1] * hsv[1] + 0.001f) * (hsv[2] * hsv[2]);

        colorsMap.put(score, color);
        colorsScore.add(score);
      }
    }

    Collections.sort(colorsScore);
    bitmap.recycle();
    resized.recycle();
    return colorsMap.get(colorsScore.get(colorsScore.size() - 1));

  }

  /**
   * 
   * Get the main color from a {@link Bitmap}.<br>
   * 
   * @param bitmap
   *            The {@link Bitmap} to analyse
   * @param width
   *            The desired width of scaled bitmap
   * @param height
   *            The desired height of scaled bitmap
   * @param def
   *            The default color returned, if bitmap is null
   * @return The rgb {@link Color} in integer (no alpha)
   */
  public static int analyse(Bitmap bitmap, int width, int height, int def) {

    if (bitmap == null) return def;

    int color = 0;

    HashMap<Float, Integer> colorsMap = new HashMap<Float, Integer>();
    ArrayList<Float> colorsScore = new ArrayList<Float>();

    Bitmap resized = Bitmap.createScaledBitmap(bitmap, width, height, false);

    for (int y = 0; y < resized.getHeight(); y++) {
      for (int x = 0; x < resized.getWidth(); x++) {
        int temp_color = resized.getPixel(x, y);
        color = Color.argb(0xFF, Color.red(temp_color), Color.green(temp_color),
            Color.blue(temp_color));
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);

        float score = (hsv[1] * hsv[1] + 0.001f) * (hsv[2] * hsv[2]);

        colorsMap.put(score, color);
        colorsScore.add(score);
      }
    }

    Collections.sort(colorsScore);
    bitmap.recycle();
    resized.recycle();
    return colorsMap.get(colorsScore.get(colorsScore.size() - 1));

  }
}




Java Source Code List

org.yammp.Constants.java
org.yammp.MediaAppWidgetProvider4x1.java
org.yammp.MediaAppWidgetProvider4x2.java
org.yammp.MediaButtonIntentReceiver.java
org.yammp.MusicPlaybackService.java
org.yammp.app.AlbumFragment.java
org.yammp.app.AppearanceSettingsActivity.java
org.yammp.app.ArtistFragment.java
org.yammp.app.Equalizer.java
org.yammp.app.GenreFragment.java
org.yammp.app.LyricsFragment.java
org.yammp.app.MusicBrowserActivity.java
org.yammp.app.MusicBrowserFragment.java
org.yammp.app.MusicPlaybackActivity.java
org.yammp.app.MusicSettingsActivity.java
org.yammp.app.PlaylistFragment.java
org.yammp.app.PluginFragment.java
org.yammp.app.PluginsManagerActivity.java
org.yammp.app.QueryBrowserActivity.java
org.yammp.app.QueryFragment.java
org.yammp.app.TrackBrowserActivity.java
org.yammp.app.TrackFragment.java
org.yammp.dialog.DeleteDialog.java
org.yammp.dialog.PlayShortcut.java
org.yammp.dialog.PlaylistDialog.java
org.yammp.dialog.PlaylistPickerDialog.java
org.yammp.dialog.PlaylistPicker.java
org.yammp.dialog.ScanningProgress.java
org.yammp.dialog.SearchDialog.java
org.yammp.dialog.SleepTimerDialog.java
org.yammp.dialog.VerticalTextSpinnerDialog.java
org.yammp.dialog.WeekSelector.java
org.yammp.util.ColorAnalyser.java
org.yammp.util.EqualizerWrapper.java
org.yammp.util.ImageDownloader.java
org.yammp.util.LazyImageLoader.java
org.yammp.util.LyricsDownloader.java
org.yammp.util.LyricsParser.java
org.yammp.util.LyricsSplitter.java
org.yammp.util.MusicUtils.java
org.yammp.util.PreferencesEditor.java
org.yammp.util.ServiceToken.java
org.yammp.util.ShakeListener.java
org.yammp.util.SortCursor.java
org.yammp.util.VisualizerCompatAudioFX.java
org.yammp.util.VisualizerCompatScoop.java
org.yammp.util.VisualizerCompat.java
org.yammp.util.VisualizerWrapper.java
org.yammp.view.EqualizerView.java
org.yammp.view.SliderView.java
org.yammp.view.TouchPaintView.java
org.yammp.view.VerticalTextSpinner.java
org.yammp.view.VisualizerViewFftSpectrum.java
org.yammp.view.VisualizerViewWaveForm.java
org.yammp.widget.CheckableRelativeLayout.java
org.yammp.widget.RepeatingImageButton.java
org.yammp.widget.SeparatedListAdapter.java
org.yammp.widget.TextScrollView.java
org.yammp.widget.TouchInterceptor.java