Example usage for android.graphics Color HSVToColor

List of usage examples for android.graphics Color HSVToColor

Introduction

In this page you can find the example usage for android.graphics Color HSVToColor.

Prototype

@ColorInt
public static int HSVToColor(@Size(3) float hsv[]) 

Source Link

Document

Convert HSV components to an ARGB color.

Usage

From source file:Main.java

/**
 * Lighten a color by a specific factor.
 * //  w  w  w . j a  v  a  2 s . co m
 * @param color the color to lighten
 * @param factor the factor to apply; e.g. 150 makes the color 50% brighter, 50
 * makes the color 50% darker, 100 makes no change.
 * 
 * @return the lightened version of the color
 */
public static int lightenColor(int color, int factor) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    float s = hsv[1];
    float v = hsv[2] * factor / 100.0f;
    if (v > 1.0f) {
        // Value has overflowed, so adjust the saturation instead.
        s -= v - 1.0f;
        if (s < 0)
            s = 0;
        v = 1.0f;
    }
    hsv[1] = s;
    hsv[2] = v;
    return Color.HSVToColor(hsv);
}

From source file:com.richtodd.android.quiltdesign.block.Theme.java

public static Theme createColors(int swatchCount, float saturation, float value) {
    m_hsv[1] = saturation;//www.  j a  v  a2s.c  o m
    m_hsv[2] = value;

    Theme theme = new Theme(swatchCount);
    for (int idx = 0; idx < swatchCount; ++idx) {
        m_hsv[0] = ColorFunctions.AngleToHue(360f * idx / swatchCount);
        int color = Color.HSVToColor(m_hsv);

        theme.getSwatches().get(idx).setColor(color);
    }

    return theme;
}

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 w w.ja  v a 2 s. c o m

    return Color.HSVToColor(hsv);
}

From source file:com.richtodd.android.quiltdesign.block.Theme.java

public static Theme createShades(int swatchCount, float hue, float value) {
    m_hsv[0] = hue;//from   w w  w. j av a 2  s .  c o  m
    m_hsv[2] = value;

    Theme theme = new Theme(swatchCount);

    for (int idx = 0; idx < swatchCount; ++idx) {
        m_hsv[1] = 1f - ((float) (idx) / (float) swatchCount);
        int color = Color.HSVToColor(m_hsv);

        theme.getSwatches().get(idx).setColor(color);
    }

    return theme;
}

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 w w w.j a  v  a 2  s.  c  o  m
    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 ww  w  .j  a  va  2  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:edu.stanford.mobisocial.dungbeetle.model.Feed.java

public static int colorFor(String name) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from   www.  j  a  v a  2 s . com
        bos.write(name.getBytes());
    } catch (IOException e) {
    }
    SecureRandom r = new SecureRandom(bos.toByteArray());
    float hsv[] = new float[] { baseHues[r.nextInt(baseHues.length)], r.nextFloat(), r.nextFloat() };
    hsv[0] = hsv[0] + 20 * r.nextFloat() - 10;
    hsv[1] = hsv[1] * 0.2f + 0.8f;
    hsv[2] = hsv[2] * 0.2f + 0.8f;
    return Color.HSVToColor(hsv);
}

From source file:com.richtodd.android.quiltdesign.block.Theme.java

public static Theme createTints(int swatchCount, float hue, float saturation) {
    m_hsv[0] = hue;/*  ww w . ja  v  a2 s  . c  om*/
    m_hsv[1] = saturation;

    Theme theme = new Theme(swatchCount);

    for (int idx = 0; idx < swatchCount; ++idx) {
        m_hsv[2] = 1f - ((float) (idx) / (float) swatchCount);
        int color = Color.HSVToColor(m_hsv);

        theme.getSwatches().get(idx).setColor(color);
    }

    return theme;
}

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  www .  j  a v a  2 s .  c om

            // 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: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);
}