Example usage for java.awt Color getRGB

List of usage examples for java.awt Color getRGB

Introduction

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

Prototype

public int getRGB() 

Source Link

Document

Returns the RGB value representing the color in the default sRGB ColorModel .

Usage

From source file:org.jspringbot.syntax.HighlighterUtils.java

private static void cssColor(StringBuilder buf, String style, Color color) {
    String rgb = Integer.toHexString(color.getRGB());
    rgb = rgb.substring(2, rgb.length());

    buf.append(style).append(":#").append(StringUtils.lowerCase(rgb)).append(";");
}

From source file:XMLWriteTest.java

/**
 * Converts a color to a hex value.//  w w  w  .jav a  2s . co  m
 * @param c a color
 * @return a string of the form #rrggbb
 */
private static String colorToString(Color c) {
    StringBuffer buffer = new StringBuffer();
    buffer.append(Integer.toHexString(c.getRGB() & 0xFFFFFF));
    while (buffer.length() < 6)
        buffer.insert(0, '0');
    buffer.insert(0, '#');
    return buffer.toString();
}

From source file:org.isatools.isacreator.qrcode.logic.CodeGenerator.java

public static Image createQRCodeImage(String toEncode, Dimension imageSize, Color foregroundColor,
        Color backgroundColor) {//from w ww.j  a v  a2 s .com

    QRCodeWriter qrWrite = new QRCodeWriter();

    try {
        BitMatrix matrix = qrWrite.encode(toEncode, BarcodeFormat.QR_CODE, imageSize.width, imageSize.height);

        MatrixToImageWriter.setForeground(foregroundColor.getRGB());
        MatrixToImageWriter.setBackground(backgroundColor.getRGB());

        return Imaging.createImageFromBufferedImage(MatrixToImageWriter.toBufferedImage(matrix));
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.saiku.adhoc.utils.TemplateUtils.java

private static String colorToStr(Color color) {
    if (color != null) {
        int colInt = color.getRGB();
        String str = Integer.toHexString(colInt);
        return str.replaceFirst("ff", "#");
    } else {/*from  w  w w .  j av a 2s. co m*/
        return null;
    }
}

From source file:gov.nih.nci.lmp.mimGpml.CommonHelper.java

/**
 * Convert color to hex string.//from ww w  . j  a  va2s  .c  o m
 * 
 * @param color
 *            the color
 * @return the string
 */
public static String convertColorToHex(Color color) {

    String rgbColorStr = Integer.toHexString(color.getRGB());
    String hexColorStr = rgbColorStr.substring(2, rgbColorStr.length());

    Logger.log.debug("RGB color: " + rgbColorStr);
    Logger.log.debug("Hex color: " + hexColorStr);

    return hexColorStr.toUpperCase();
}

From source file:Main.java

private static void applyShadow(BufferedImage image, int shadowSize, Color shadowColor, float shadowOpacity) {
    int dstWidth = image.getWidth();
    int dstHeight = image.getHeight();

    int left = (shadowSize - 1) >> 1;
    int right = shadowSize - left;
    int xStart = left;
    int xStop = dstWidth - right;
    int yStart = left;
    int yStop = dstHeight - right;

    int shadowRgb = shadowColor.getRGB() & 0x00FFFFFF;

    int[] aHistory = new int[shadowSize];
    int historyIdx = 0;

    int aSum;/*  ww  w  .  ja v a 2s . c  o m*/

    int[] dataBuffer = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    int lastPixelOffset = right * dstWidth;
    float sumDivider = shadowOpacity / shadowSize;

    // horizontal pass

    for (int y = 0, bufferOffset = 0; y < dstHeight; y++, bufferOffset = y * dstWidth) {
        aSum = 0;
        historyIdx = 0;
        for (int x = 0; x < shadowSize; x++, bufferOffset++) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[x] = a;
            aSum += a;
        }

        bufferOffset -= right;

        for (int x = xStart; x < xStop; x++, bufferOffset++) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest pixel
            a = dataBuffer[bufferOffset + right] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;

            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }

    // vertical pass
    for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {
        aSum = 0;
        historyIdx = 0;
        for (int y = 0; y < shadowSize; y++, bufferOffset += dstWidth) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[y] = a;
            aSum += a;
        }

        bufferOffset -= lastPixelOffset;

        for (int y = yStart; y < yStop; y++, bufferOffset += dstWidth) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest pixel
            a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;

            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
}

From source file:com.zacwolf.commons.email.Email.java

public static String color2hex(Color col) {
    return Integer.toHexString((col.getRGB() & 0xffffff) | 0x1000000).substring(1);
}

From source file:org.polymap.core.data.image.ImageTransparencyProcessor.java

public static BufferedImage transparency(Image image, final Color markerColor) throws IOException {
    long start = System.currentTimeMillis();

    RGBImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = markerColor.getRGB() | 0xFF000000;

        byte threshold = 25;

        double range = ((double) 0xFF) / (3 * threshold);

        public final int filterRGB(int x, int y, int rgb) {
            Color probe = new Color(rgb);
            //log.info( "probe=" + probe + ", marker=" + markerColor );

            // delta values
            int dRed = markerColor.getRed() - probe.getRed();
            int dGreen = markerColor.getGreen() - probe.getGreen();
            int dBlue = markerColor.getBlue() - probe.getBlue();
            //log.info( "    dRed=" + dRed + ", dGreen=" + dGreen );

            if (dRed >= 0 && dRed < threshold && dGreen >= 0 && dGreen < threshold && dBlue >= 0
                    && dBlue < threshold) {
                int alpha = (int) Math.round(range * (dRed + dGreen + dBlue));
                //log.info( "    -> alpha=" + alpha );

                return ((alpha << 24) | 0x00FFFFFF) & rgb;
            } else {
                // nothing to do
                return rgb;
            }/* ww  w  .  j a v  a  2  s.  c o  m*/
        }
    };

    //        BufferedImage bimage = null;
    //        if (image instanceof BufferedImage) {
    //            bimage = (BufferedImage)image;
    //        }
    //        else {
    //            bimage = new BufferedImage(
    //                    image.getHeight( null ), image.getWidth( null ), BufferedImage.TYPE_INT_ARGB );
    //            Graphics g = bimage.getGraphics();
    //            g.drawImage( image, 0, 0, null );
    //            g.dispose();
    //        }

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    Image result = Toolkit.getDefaultToolkit().createImage(ip);

    BufferedImage bresult = new BufferedImage(image.getHeight(null), image.getWidth(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = bresult.getGraphics();
    g.drawImage(result, 0, 0, null);
    g.dispose();

    //        // XXX this can surely be done any more clever
    //        int width = bimage.getWidth();
    //        int height = bimage.getHeight();
    //        for (int x=bimage.getMinX(); x<width; x++) {
    //            for (int y=bimage.getMinY(); y<height; y++) {
    //                int filtered = filter.filterRGB( x, y, bimage.getRGB( x, y ) );
    //                result.setRGB( x, y, filtered );
    //            }
    //        }

    log.debug("Transparency done. (" + (System.currentTimeMillis() - start) + "ms)");
    return bresult;
}

From source file:Main.java

public static BufferedImage generateOutline(BufferedImage source, Color color1, Color color2, boolean alpha) {
    int w = source.getWidth();
    int h = source.getHeight();
    BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            if (!testNeighbour(source, x, y, alpha)) {
                if (testNeighbours(source, x, y, alpha)) {
                    boolean a = ((x + y) % 10) <= 5;
                    result.setRGB(x, y, a ? color1.getRGB() : color2.getRGB());
                }/*from w w  w.j ava 2 s  .  c  o  m*/
            }
        }
    }
    return result;
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {

        private int shift = 0xFF000000;

        public int rgbToMakeTransparent = color.getRGB() | shift;

        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | shift) == rgbToMakeTransparent) {
                return 0x00FFFFFF & rgb;
            }//www .  j a  va 2  s. co  m
            return rgb;
        }
    };
    Image newImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter));
    BufferedImage bufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(newImage, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}