List of usage examples for android.graphics Color colorToHSV
public static void colorToHSV(@ColorInt int color, @Size(3) float hsv[])
From source file:Main.java
public static int getDominantColor(Bitmap source, boolean applyThreshold) { if (source == null) return Color.argb(255, 255, 255, 255); // Keep track of how many times a hue in a given bin appears in the image. // Hue values range [0 .. 360), so dividing by 10, we get 36 bins. int[] colorBins = new int[36]; // The bin with the most colors. Initialize to -1 to prevent accidentally // thinking the first bin holds the dominant color. int maxBin = -1; // Keep track of sum hue/saturation/value per hue bin, which we'll use to // compute an average to for the dominant color. float[] sumHue = new float[36]; float[] sumSat = new float[36]; float[] sumVal = new float[36]; float[] hsv = new float[3]; int height = source.getHeight(); int width = source.getWidth(); int[] pixels = new int[width * height]; source.getPixels(pixels, 0, width, 0, 0, width, height); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int c = pixels[col + row * width]; // Ignore pixels with a certain transparency. if (Color.alpha(c) < 128) continue; Color.colorToHSV(c, hsv); // If a threshold is applied, ignore arbitrarily chosen values for "white" and "black". if (applyThreshold && (hsv[1] <= 0.35f || hsv[2] <= 0.35f)) continue; // We compute the dominant color by putting colors in bins based on their hue. int bin = (int) Math.floor(hsv[0] / 10.0f); // Update the sum hue/saturation/value for this bin. sumHue[bin] = sumHue[bin] + hsv[0]; sumSat[bin] = sumSat[bin] + hsv[1]; sumVal[bin] = sumVal[bin] + hsv[2]; // Increment the number of colors in this bin. colorBins[bin]++;/*from ww w . j a va 2s . c o m*/ // Keep track of the bin that holds the most colors. if (maxBin < 0 || colorBins[bin] > colorBins[maxBin]) maxBin = bin; } } // maxBin may never get updated if the image holds only transparent and/or black/white pixels. if (maxBin < 0) return Color.argb(255, 255, 255, 255); // Return a color with the average hue/saturation/value of the bin with the most colors. hsv[0] = sumHue[maxBin] / colorBins[maxBin]; hsv[1] = sumSat[maxBin] / colorBins[maxBin]; hsv[2] = sumVal[maxBin] / colorBins[maxBin]; return Color.HSVToColor(hsv); }
From source file:Main.java
public static int darkenColor(int color, float amount) { float[] hsv = new float[3]; Color.colorToHSV(color, hsv); hsv[2] *= amount;//from w ww .j a v a 2s .co m return Color.HSVToColor(hsv); }
From source file:Main.java
/** * This picks a dominant color, looking for high-saturation, high-value, repeated hues. * @param bitmap The bitmap to scan//from w ww .ja va2 s. c om * @param samples The approximate max number of samples to use. */ static int findDominantColorByHue(Bitmap bitmap, int samples) { final int height = bitmap.getHeight(); final int width = bitmap.getWidth(); int sampleStride = (int) Math.sqrt((height * width) / samples); if (sampleStride < 1) { sampleStride = 1; } // This is an out-param, for getting the hsv values for an rgb float[] hsv = new float[3]; // First get the best hue, by creating a histogram over 360 hue buckets, // where each pixel contributes a score weighted by saturation, value, and alpha. float[] hueScoreHistogram = new float[360]; float highScore = -1; int bestHue = -1; for (int y = 0; y < height; y += sampleStride) { for (int x = 0; x < width; x += sampleStride) { int argb = bitmap.getPixel(x, y); int alpha = 0xFF & (argb >> 24); if (alpha < 0x80) { // Drop mostly-transparent pixels. continue; } // Remove the alpha channel. int rgb = argb | 0xFF000000; Color.colorToHSV(rgb, hsv); // Bucket colors by the 360 integer hues. int hue = (int) hsv[0]; if (hue < 0 || hue >= hueScoreHistogram.length) { // Defensively avoid array bounds violations. continue; } float score = hsv[1] * hsv[2]; hueScoreHistogram[hue] += score; if (hueScoreHistogram[hue] > highScore) { highScore = hueScoreHistogram[hue]; bestHue = hue; } } } SparseArray<Float> rgbScores = new SparseArray<Float>(); int bestColor = 0xff000000; highScore = -1; // Go back over the RGB colors that match the winning hue, // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets. // The highest-scoring RGB color wins. for (int y = 0; y < height; y += sampleStride) { for (int x = 0; x < width; x += sampleStride) { int rgb = bitmap.getPixel(x, y) | 0xff000000; Color.colorToHSV(rgb, hsv); int hue = (int) hsv[0]; if (hue == bestHue) { float s = hsv[1]; float v = hsv[2]; int bucket = (int) (s * 100) + (int) (v * 10000); // Score by cumulative saturation * value. float score = s * v; Float oldTotal = rgbScores.get(bucket); float newTotal = oldTotal == null ? score : oldTotal + score; rgbScores.put(bucket, newTotal); if (newTotal > highScore) { highScore = newTotal; // All the colors in the winning bucket are very similar. Last in wins. bestColor = rgb; } } } } return bestColor; }
From source file:com.richtodd.android.quiltdesign.block.Swatch.java
public String getSortString() { if (m_sortString == null) { Color.colorToHSV(m_color, m_hsv); Formatter formatter = new Formatter(); try {/*w w w . j a v a 2s.c o m*/ formatter.format("%03d-%03d-%03d", (int) m_hsv[0], (int) (m_hsv[1] * 100f), (int) (m_hsv[2] * 100f)); m_sortString = formatter.toString(); } finally { formatter.close(); } } return m_sortString; }
From source file:com.dm.material.dashboard.candybar.helpers.ColorHelper.java
public static int getDarkerColor(@ColorInt int color, float transparency) { float[] hsv = new float[3]; Color.colorToHSV(color, hsv); hsv[2] *= transparency;//from www . j av a2 s. com return Color.HSVToColor(hsv); }
From source file:com.irccloud.android.data.model.Avatar.java
public static Bitmap generateBitmap(String text, int textColor, int bgColor, boolean isDarkTheme, int size, boolean round) { Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); if (bitmap != null) { Canvas c = new Canvas(bitmap); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setStyle(Paint.Style.FILL); if (isDarkTheme || !round) { p.setColor(bgColor);/*from w w w. j a v a2 s . c o m*/ if (round) c.drawCircle(size / 2, size / 2, size / 2, p); else c.drawColor(bgColor); } else { float[] hsv = new float[3]; Color.colorToHSV(bgColor, hsv); hsv[2] *= 0.8f; p.setColor(Color.HSVToColor(hsv)); c.drawCircle(size / 2, size / 2, (size / 2) - 2, p); p.setColor(bgColor); c.drawCircle(size / 2, (size / 2) - 2, (size / 2) - 2, p); } TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG); tp.setTextAlign(Paint.Align.CENTER); tp.setTypeface(font); tp.setTextSize((int) (size * 0.65)); tp.setColor(textColor); if (isDarkTheme || !round) { c.drawText(text, size / 2, (size / 2) - ((tp.descent() + tp.ascent()) / 2), tp); } else { c.drawText(text, size / 2, (size / 2) - 4 - ((tp.descent() + tp.ascent()) / 2), tp); } return bitmap; } else { return null; } }
From source file:Main.java
public static int getHighlightColor(int sampleColor) { // Set a constant value level in HSV, in case the averaged color is too light or too dark. float[] hsvBackground = new float[3]; Color.colorToHSV(sampleColor, hsvBackground); hsvBackground[2] = 0.3f; // value parameter return Color.HSVToColor(0xf2, hsvBackground); }
From source file:com.glabs.homegenie.widgets.ColorLightDialogFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** Inflating the layout for this fragment **/ final Fragment _this = this; _view = inflater.inflate(R.layout.widget_control_colorlight, null); _groupText = (TextView) _view.findViewById(R.id.groupText); _levelText = (TextView) _view.findViewById(R.id.levelText); _colorPicker = (ColorPickerView) _view.findViewById(R.id.color_picker_view); _colorPreview = _view.findViewById(R.id.colorPreview); _colorPicker.setOnColorChangedListener(new ColorPickerView.OnColorChangedListener() { @Override/*from ww w . j a v a 2s . co m*/ public void onColorChanged(int newColor) { float[] hsv = new float[3]; Color.colorToHSV(newColor, hsv); String hsbcolor = String.valueOf(hsv[0] / 360f).replace(',', '.') + "," + String.valueOf(hsv[1]).replace(',', '.') + "," + String.valueOf(hsv[2]).replace(',', '.'); _module.setParameter("Status.ColorHsb", hsbcolor); _module.setParameter("Status.Level", String.valueOf(hsv[2])); refreshView(); } }); _colorPicker.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { String cmd = "Control.ColorHsb/" + _module.getParameter("Status.ColorHsb").Value; _module.control(cmd, null); return true; } return false; } }); Button onButton = (Button) _view.findViewById(R.id.onButton); onButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { _module.setParameter("Status.Level", "100"); refreshView(); _module.control("Control.On", null); } }); Button offButton = (Button) _view.findViewById(R.id.offButton); offButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { _module.setParameter("Status.Level", "0"); refreshView(); _module.control("Control.Off", null); } }); refreshView(); return _view; }
From source file:org.ciasaboark.tacere.view.EventListItem.java
private static int desaturateColor(int color, float ratio) { float[] hsv = new float[3]; Color.colorToHSV(color, hsv); hsv[1] = (hsv[1] / 1 * ratio) + (DESATURATE_RATIO * (1.0f - ratio)); return Color.HSVToColor(hsv); }
From source file:com.shopgun.android.utils.ColorUtils.java
public static float[] toHSV(@ColorInt int color) { float[] hsv = new float[3]; Color.colorToHSV(color, hsv); return hsv;/*www .j a v a2 s. c om*/ }