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:smanilov.mandelbrot.compute.Computer.java

/**
 * Mixes the given colors in the given proportion.
 *//*from  ww  w .j av  a 2s.  co m*/
private static Color mixColors(Color c1, Color c2, double proportion) {
    Color result = new Color((int) (c1.getRed() * (1 - proportion) + c2.getRed() * proportion),
            (int) (c1.getGreen() * (1 - proportion) + c2.getGreen() * proportion),
            (int) (c1.getBlue() * (1 - proportion) + c2.getBlue() * proportion));
    return result;
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage autoContrastImage(BufferedImage image) {
    createContrastLUT(image);//from  w  w w.j a  va2s  . c o  m

    int height = image.getHeight();
    int width = image.getWidth();
    int r, g, b;
    Color c;

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) (255 * contrast_lut[c.getRed()]);
            g = (int) (255 * contrast_lut[c.getGreen()]);
            b = (int) (255 * contrast_lut[c.getBlue()]);
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static void clearNoise(BufferedImage image) {
    final int width = image.getWidth();
    final int height = image.getHeight();

    /*/*from  ww w. j a v  a2 s .com*/
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        image.setRGB(x, y, Color.WHITE.getRGB());
    }
    }
    */
    for (int x = image.getMinX(); x < width; x++) {
        for (int y = image.getMinY(); y < height; y++) {

            int point = image.getRGB(x, y);

            // 
            int top = (y > 0) ? image.getRGB(x, y - 1) : -1;
            // ?
            int right = (x < width - 1) ? image.getRGB(x + 1, y) : -1;
            // 
            int bottom = (y < height - 1) ? image.getRGB(x, y + 1) : -1;
            // 
            int left = (x > 0) ? image.getRGB(x - 1, y) : -1;
            // ?
            int topRight = ((x < width - 1) && (y > 0)) ? image.getRGB(x + 1, y - 1) : -1;
            // 
            int topLeft = ((x > 0) && (y > 0)) ? image.getRGB(x - 1, y - 1) : -1;
            // ?
            int bottomRight = ((x < width - 1) && (y < height - 1)) ? image.getRGB(x + 1, y + 1) : -1;
            // 
            int bottomLeft = ((x > 0) && (y < height - 1)) ? image.getRGB(x - 1, y + 1) : -1;

            int i = 0;
            i = (top != -1) ? i + 1 : i;
            i = (right != -1) ? i + 1 : i;
            i = (bottom != -1) ? i + 1 : i;
            i = (left != -1) ? i + 1 : i;

            i = (topRight != -1) ? i + 1 : i;
            i = (topLeft != -1) ? i + 1 : i;
            i = (bottomRight != -1) ? i + 1 : i;
            i = (bottomLeft != -1) ? i + 1 : i;

            // System.out.println("i:" + i + ", top:" + top + ", right:" + right + ", bottom:" + bottom + ", left:" + left + ", topRight:" + topRight + ", topLeft:" + topLeft + ", bottomRight:" + bottomRight + ", bottomLeft:" + bottomLeft);
            if (i < 5) {
                image.setRGB(x, y, Color.WHITE.getRGB());
            }

            /*
            int leftNearby = 0;
            if(left != -1) {
            int secondLeft = (x > 1) ? image.getRGB(x - 2, y) : -1;
            int threeLeft = (x > 2) ? image.getRGB(x - 3, y) : -1;
                    
            leftNearby = ((left == -1) ? 0 : 1) + ((secondLeft == -1) ? 0 : 1) + ((threeLeft == -1) ? 0 : 1);
            }
                    
            int rightNearby = 0;
            if(right != -1) {
            int secondRight = (x + 1 < width - 1) ? image.getRGB(x + 2, y) : -1;
            int threeRight = (x + 2 < width - 1) ? image.getRGB(x + 3, y) : -1;
                    
            rightNearby = ((right == -1) ? 0 : 1) + ((secondRight == -1) ? 0 : 1) + ((threeRight == -1) ? 0 : 1);
            }
                    
            int topNearby = 0;
            if(top != -1) {
            int secondTop = (y > 1) ? image.getRGB(x, y - 2) : -1;
            int threeTop = (y > 2) ? image.getRGB(x, y - 3) : -1;
                    
            topNearby = ((top == -1) ? 0 : 1) + ((secondTop == -1) ? 0 : 1) + ((threeTop == -1) ? 0 : 1);
            }
                    
            int bottomNearby = 0;
            if(bottom != -1) {
            int secondBottom = (y + 1 < height - 1) ? image.getRGB(x, y + 2) : -1;
            int threeBottom = (y + 2 < height - 1) ? image.getRGB(x, y + 3) : -1;
                    
            bottomNearby = ((bottom == -1) ? 0 : 1) + ((secondBottom == -1) ? 0 : 1) + ((threeBottom == -1) ? 0 : 0);
            }
                    
            System.out.println(leftNearby + " " + rightNearby + " " + topNearby + " " + bottomNearby);
            if (leftNearby != 2 && rightNearby != 2 && topNearby != 2 && bottomNearby != 2) {
            image.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            System.out.println(point - top);
            System.out.println(point - right);
            System.out.println(point - bottom);
            System.out.println(point - left);
            System.out.println(point - topRight);
            System.out.println(point - topLeft);
            System.out.println(point - bottomRight);
            System.out.println(point - bottomLeft);
            */

            // if (point != top && point != right && point != bottom && point != left && point != topRight && point != topLeft && point != bottomRight && point != bottomLeft) {
            //     image.setRGB(x, y, Color.WHITE.getRGB());
            // }

            /*
            Color color = new Color(image.getRGB(x, y));
            if((color.getBlue() < 120) || ((color.getRed() + color.getGreen() + color.getBlue()) < 50)) { //
            image.setRGB(x, y, Color.WHITE.getRGB());
            } else if((color.getRed() + color.getGreen() + color.getBlue()) < 400) {
            image.setRGB(x, y, Color.BLACK.getRGB());
            }
            */

            Color color = new Color(image.getRGB(x, y));
            if ((color.getRed() + color.getGreen() + color.getBlue()) < 600) {
                image.setRGB(x, y, Color.BLACK.getRGB());
            } else {
                image.setRGB(x, y, Color.WHITE.getRGB());
            }
        }
    }

}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage normalizeImage(BufferedImage image) {
    int height = image.getHeight();
    int width = image.getWidth();

    int r, g, b, minr = 255, ming = 255, minb = 255, maxr = 0, maxg = 0, maxb = 0;
    Color c;

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            if (minr > c.getRed()) {
                minr = c.getRed();//from w w w . ja  v a 2  s.co m
            }
            if (ming > c.getGreen()) {
                ming = c.getGreen();
            }
            if (minb > c.getBlue()) {
                minb = c.getBlue();
            }
            if (maxr < c.getRed()) {
                maxr = c.getRed();
            }
            if (maxg < c.getGreen()) {
                maxg = c.getGreen();
            }
            if (maxb < c.getBlue()) {
                maxb = c.getBlue();
            }
        }
    }

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) ((c.getRed() - minr) * 255 / (maxr - minr));
            g = (int) ((c.getGreen() - ming) * 255 / (maxg - ming));
            b = (int) ((c.getBlue() - minb) * 255 / (maxb - minb));
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:com.lingxiang2014.util.ImageUtils.java

private static String toHexEncoding(Color color) {
    String R, G, B;/*from  w  w  w.  ja v a2s .  co  m*/
    StringBuffer stringBuffer = new StringBuffer();
    R = Integer.toHexString(color.getRed());
    G = Integer.toHexString(color.getGreen());
    B = Integer.toHexString(color.getBlue());
    R = R.length() == 1 ? "0" + R : R;
    G = G.length() == 1 ? "0" + G : G;
    B = B.length() == 1 ? "0" + B : B;
    stringBuffer.append("#");
    stringBuffer.append(R);
    stringBuffer.append(G);
    stringBuffer.append(B);
    return stringBuffer.toString();
}

From source file:org.pgptool.gui.ui.tools.UiUtils.java

public static void setLookAndFeel() {
    // NOTE: We doing it this way to prevent dead=locks that is sometimes
    // happens if do it in main thread
    Edt.invokeOnEdtAndWait(new Runnable() {
        @Override/*from  w  w  w . java2  s  .  co  m*/
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                fixCheckBoxMenuItemForeground();
                fixFontSize();
            } catch (Throwable t) {
                log.error("Failed to set L&F", t);
            }
        }

        /**
         * In some cases (depends on OS theme) check menu item foreground is same as
         * bacground - thus it;'s invisible when cheked
         */
        private void fixCheckBoxMenuItemForeground() {
            UIDefaults defaults = UIManager.getDefaults();
            Color selectionForeground = defaults.getColor("CheckBoxMenuItem.selectionForeground");
            Color foreground = defaults.getColor("CheckBoxMenuItem.foreground");
            Color background = defaults.getColor("CheckBoxMenuItem.background");
            if (colorsDiffPercentage(selectionForeground, background) < 10) {
                // TODO: That doesn't actually affect defaults. Need to find out how to fix it
                defaults.put("CheckBoxMenuItem.selectionForeground", foreground);
            }
        }

        private int colorsDiffPercentage(Color c1, Color c2) {
            int diffRed = Math.abs(c1.getRed() - c2.getRed());
            int diffGreen = Math.abs(c1.getGreen() - c2.getGreen());
            int diffBlue = Math.abs(c1.getBlue() - c2.getBlue());

            float pctDiffRed = (float) diffRed / 255;
            float pctDiffGreen = (float) diffGreen / 255;
            float pctDiffBlue = (float) diffBlue / 255;

            return (int) ((pctDiffRed + pctDiffGreen + pctDiffBlue) / 3 * 100);
        }

        private void fixFontSize() {
            if (isJreHandlesScaling()) {
                log.info("JRE handles font scaling, won't change it");
                return;
            }
            log.info("JRE doesnt't seem to support font scaling");

            Toolkit toolkit = Toolkit.getDefaultToolkit();
            int dpi = toolkit.getScreenResolution();
            if (dpi == 96) {
                if (log.isDebugEnabled()) {
                    Font font = UIManager.getDefaults().getFont("TextField.font");
                    String current = font != null ? Integer.toString(font.getSize()) : "unknown";
                    log.debug(
                            "Screen dpi seem to be 96. Not going to change font size. Btw current size seem to be "
                                    + current);
                }
                return;
            }
            int targetFontSize = 12 * dpi / 96;
            log.debug("Screen dpi = " + dpi + ", decided to change font size to " + targetFontSize);
            setDefaultSize(targetFontSize);
        }

        private boolean isJreHandlesScaling() {
            try {
                JreVersion noNeedToScaleForVer = JreVersion.parseString("9");
                String jreVersionStr = System.getProperty("java.version");
                if (jreVersionStr != null) {
                    JreVersion curVersion = JreVersion.parseString(jreVersionStr);
                    if (noNeedToScaleForVer.compareTo(curVersion) <= 0) {
                        return true;
                    }
                }

                return false;
            } catch (Throwable t) {
                log.warn("Failed to see oif JRE can handle font scaling. Will assume it does. JRE version: "
                        + System.getProperty("java.version"), t);
                return true;
            }
        }

        public void setDefaultSize(int size) {
            Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
            Object[] keys = keySet.toArray(new Object[keySet.size()]);
            for (Object key : keys) {
                if (key != null && key.toString().toLowerCase().contains("font")) {
                    Font font = UIManager.getDefaults().getFont(key);
                    if (font != null) {
                        Font changedFont = font.deriveFont((float) size);
                        UIManager.put(key, changedFont);
                        Font doubleCheck = UIManager.getDefaults().getFont(key);
                        log.debug("Font size changed for " + key + ". From " + font.getSize() + " to "
                                + doubleCheck.getSize());
                    }
                }
            }
        }
    });
    log.info("L&F set");
}

From source file:lu.lippmann.cdb.graph.renderer.CadralEdgeColorTransformer.java

private static Color rangeColor(Color colorMin, Color colorMax, BigDecimal grow, BigDecimal minValue,
        BigDecimal maxValue) {//from ww w . j  av  a2s  .  co  m
    BigDecimal colorValue = grow;
    if (colorValue == null) {
        return Color.LIGHT_GRAY;
    }

    if (maxValue.compareTo(minValue) < 0) {
        return rangeColor(colorMin, colorMax, grow, maxValue, minValue);
    } else {
        int rMax = colorMax.getRed();
        int gMax = colorMax.getGreen();
        int bMax = colorMax.getBlue();

        double color = 0.0;

        color = 255.0
                - grow.subtract(minValue).doubleValue() * 255.0 / maxValue.subtract(minValue).doubleValue();

        int r = rMax + (int) ((255 - rMax) * color / 255.0);
        int g = gMax + (int) ((255 - gMax) * color / 255.0);
        int b = bMax + (int) ((255 - bMax) * color / 255.0);

        if (r > 192 && g > 192 && b > 192)
            return Color.LIGHT_GRAY;

        return new Color(r, g, b);
    }

}

From source file:cn.z.Ocr5.java

public static int getColorBright(int colorInt) {
    Color color = new Color(colorInt);
    return color.getRed() + color.getGreen() + color.getBlue();
}

From source file:cn.z.Ocr5.java

public static int isBlack(int colorInt, int blackThreshold) {
    final Color color = new Color(colorInt);
    if (color.getRed() + color.getGreen() + color.getBlue() <= blackThreshold) {
        return 1;
    }//from   w ww  .ja va  2 s. c o  m
    return 0;
}

From source file:cn.z.Ocr5.java

public static int isWhite(int colorInt, int whiteThreshold) {
    final Color color = new Color(colorInt);
    if (color.getRed() + color.getGreen() + color.getBlue() > whiteThreshold) {
        return 1;
    }/*w w  w  .java 2s .  c om*/
    return 0;
}