Example usage for java.awt Color getRed

List of usage examples for java.awt Color getRed

Introduction

In this page you can find the example usage for java.awt Color getRed.

Prototype

public int getRed() 

Source Link

Document

Returns the red component in the range 0-255 in the default sRGB space.

Usage

From source file:Main.java

/**
 * Make a color scaled using a defined factor.
 *
 * @param color/*from  w  w  w .  ja v  a2  s . co  m*/
 *          the color to scale.
 * @param factor
 *          the factor to use.
 * @return the scaled color.
 */
public static Color getScaledColor(Color color, double factor) {
    if (color == null) {
        return null;
    }
    if (factor <= 1) {
        return new Color(Math.max((int) (color.getRed() * factor), 0),
                Math.max((int) (color.getGreen() * factor), 0), Math.max((int) (color.getBlue() * factor), 0),
                color.getAlpha());
    }
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();

    int i = (int) (1.0 / (factor - 1));
    if (r == 0 && g == 0 && b == 0) {
        return new Color(i, i, i);
    }
    if (r > 0 && r < i) {
        r = i;
    }
    if (g > 0 && g < i) {
        g = i;
    }
    if (b > 0 && b < i) {
        b = i;
    }

    return new Color(Math.min((int) (r / (2 - factor)), 255), Math.min((int) (g / (2 - factor)), 255),
            Math.min((int) (b / (2 - factor)), 255));

}

From source file:Main.java

public static byte matchColor(Color c) {
    int i = 0;/*w  ww. j a va2s. c o m*/
    int combinatronix = 100000;
    for (int z = 0; z < csamples.length; z++) {
        Color grab = csamples[z];
        int calc = Math.abs(c.getRed() - grab.getRed()) + Math.abs(c.getGreen() - grab.getGreen())
                + Math.abs(c.getBlue() - grab.getBlue());
        if (calc < combinatronix) {
            combinatronix = calc;
            i = z;
        }
    }
    return (byte) i;
}

From source file:org.asqatasun.rules.elementchecker.contrast.helper.ContrastHelper.java

/**
 *
 * @param color/*from  w w w  .  j a  va 2s . c om*/
 * @return
 */
public static double getLuminosity(Color color) {
    double luminosity = getComposantValue(color.getRed()) * RED_FACTOR
            + getComposantValue(color.getGreen()) * GREEN_FACTOR
            + getComposantValue(color.getBlue()) * BLUE_FACTOR;

    return luminosity;
}

From source file:Util.java

/**
 * Return a string representation of a color
 * /*w  w  w. j  a  v a  2  s .  c  o m*/
 * @param color
 * @return string representation
 */
public static String colorToString(Color color) {
    if (color == null) {
        return "";
    }

    StringBuffer buf = new StringBuffer();
    buf.append('#');
    buf.append(numberToPaddedHexString(color.getRed(), 2));
    buf.append(numberToPaddedHexString(color.getGreen(), 2));
    buf.append(numberToPaddedHexString(color.getBlue(), 2));

    return buf.toString();
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.Utils.java

public static int calculateBrightness(final Color color) {
    return (int) Math.sqrt(color.getRed() * color.getRed() * .241d + color.getGreen() * color.getGreen() * .691d
            + color.getBlue() * color.getBlue() * .068d);
}

From source file:ColorUtil.java

public static Color blend(Color c1, Color c2, double v) {
    double v2 = 1 - v;
    return c1 == null ? (c2 == null ? null : c2)
            : c2 == null ? c1//from  w w w .  j  a  va2s  .com
                    : new Color(Math.min(255, (int) (c1.getRed() * v2 + c2.getRed() * v)),
                            Math.min(255, (int) (c1.getGreen() * v2 + c2.getGreen() * v)),
                            Math.min(255, (int) (c1.getBlue() * v2 + c2.getBlue() * v)),
                            Math.min(255, (int) (c1.getAlpha() * v2 + c2.getAlpha() * v)));
}

From source file:org.esa.s2tbx.dataio.s2.l1b.L1bSceneDescription.java

private static Color addAlpha(Color color, int alpha) {
    return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
}

From source file:Gradient.java

/**
 * Creates an array of Color objects for use as a gradient, using a linear
 * interpolation between the two specified colors.
 * /*from  w w w  .j  a v  a 2 s. c  om*/
 * @param one
 *            Color used for the bottom of the gradient
 * @param two
 *            Color used for the top of the gradient
 * @param numSteps
 *            The number of steps in the gradient. 250 is a good number.
 */
public static Color[] createGradient(final Color one, final Color two, final int numSteps) {
    int r1 = one.getRed();
    int g1 = one.getGreen();
    int b1 = one.getBlue();
    int a1 = one.getAlpha();

    int r2 = two.getRed();
    int g2 = two.getGreen();
    int b2 = two.getBlue();
    int a2 = two.getAlpha();

    int newR = 0;
    int newG = 0;
    int newB = 0;
    int newA = 0;

    Color[] gradient = new Color[numSteps];
    double iNorm;
    for (int i = 0; i < numSteps; i++) {
        iNorm = i / (double) numSteps; // a normalized [0:1] variable
        newR = (int) (r1 + iNorm * (r2 - r1));
        newG = (int) (g1 + iNorm * (g2 - g1));
        newB = (int) (b1 + iNorm * (b2 - b1));
        newA = (int) (a1 + iNorm * (a2 - a1));
        gradient[i] = new Color(newR, newG, newB, newA);
    }

    return gradient;
}

From source file:ColorUtil.java

/**
 * Make a color darker./*from www.ja v  a  2s  . c om*/
 * 
 * @param color     Color to make darker.
 * @param fraction  Darkness fraction.
 * @return          Darker color.
 */
public static Color darker(Color color, double fraction) {
    int red = (int) Math.round(color.getRed() * (1.0 - fraction));
    int green = (int) Math.round(color.getGreen() * (1.0 - fraction));
    int blue = (int) Math.round(color.getBlue() * (1.0 - fraction));

    if (red < 0)
        red = 0;
    else if (red > 255)
        red = 255;
    if (green < 0)
        green = 0;
    else if (green > 255)
        green = 255;
    if (blue < 0)
        blue = 0;
    else if (blue > 255)
        blue = 255;

    int alpha = color.getAlpha();

    return new Color(red, green, blue, alpha);
}

From source file:ColorUtil.java

/**
 * Make a color lighter.//w  w  w .  j  av a  2s  .co  m
 * 
 * @param color     Color to make lighter.
 * @param fraction  Darkness fraction.
 * @return          Lighter color.
 */
public static Color lighter(Color color, double fraction) {
    int red = (int) Math.round(color.getRed() * (1.0 + fraction));
    int green = (int) Math.round(color.getGreen() * (1.0 + fraction));
    int blue = (int) Math.round(color.getBlue() * (1.0 + fraction));

    if (red < 0)
        red = 0;
    else if (red > 255)
        red = 255;
    if (green < 0)
        green = 0;
    else if (green > 255)
        green = 255;
    if (blue < 0)
        blue = 0;
    else if (blue > 255)
        blue = 255;

    int alpha = color.getAlpha();

    return new Color(red, green, blue, alpha);
}