Example usage for java.awt Color getColorComponents

List of usage examples for java.awt Color getColorComponents

Introduction

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

Prototype

public float[] getColorComponents(float[] compArray) 

Source Link

Document

Returns a float array containing only the color components of the Color , in the ColorSpace of the Color .

Usage

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

/**
 * Set the non stroking color, specified as RGB.
 * /*w w  w. ja  v a2 s.  c  o m*/
 * @param color
 *            The color to set.
 * @throws IOException
 *             If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(Color color) throws IOException {
    ColorSpace colorSpace = color.getColorSpace();
    if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
        setNonStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
    } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
        color.getColorComponents(colorComponents);
        setNonStrokingColor(colorComponents[0]);
    } else if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        color.getColorComponents(colorComponents);
        setNonStrokingColor(colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

From source file:org.apache.fop.afp.ptoca.PtocaBuilder.java

/**
 * The Set Extended Text Color control sequence specifies a color value and
 * defines the color space and encoding for that value. The specified color
 * value is applied to foreground areas of the text presentation space.
 * <p>/*from   ww  w.j a  va  2  s .c o m*/
 * This is a modal control sequence.
 *
 * @param col The color to be set.
 * @throws IOException if an I/O error occurs
 */
public void setExtendedTextColor(Color col) throws IOException {
    if (ColorUtil.isSameColor(col, currentColor)) {
        return;
    }
    if (col instanceof ColorWithAlternatives) {
        ColorWithAlternatives cwa = (ColorWithAlternatives) col;
        Color alt = cwa.getFirstAlternativeOfType(ColorSpace.TYPE_CMYK);
        if (alt != null) {
            col = alt;
        }
    }
    ColorSpace cs = col.getColorSpace();

    newControlSequence();
    if (col.getColorSpace().getType() == ColorSpace.TYPE_CMYK) {
        // Color space - 0x04 = CMYK, all else are reserved and must be zero
        writeBytes(0x00, 0x04, 0x00, 0x00, 0x00, 0x00);
        writeBytes(8, 8, 8, 8); // Number of bits in component 1, 2, 3 & 4 respectively
        float[] comps = col.getColorComponents(null);
        assert comps.length == 4;
        for (int i = 0; i < 4; i++) {
            int component = Math.round(comps[i] * 255);
            writeBytes(component);
        }
    } else if (cs instanceof CIELabColorSpace) {
        // Color space - 0x08 = CIELAB, all else are reserved and must be zero
        writeBytes(0x00, 0x08, 0x00, 0x00, 0x00, 0x00);
        writeBytes(8, 8, 8, 0); // Number of bits in component 1,2,3 & 4
        //Sadly, 16 bit components don't seem to work
        float[] colorComponents = col.getColorComponents(null);
        int l = Math.round(colorComponents[0] * 255f);
        int a = Math.round(colorComponents[1] * 255f) - 128;
        int b = Math.round(colorComponents[2] * 255f) - 128;
        writeBytes(l, a, b); // l*, a* and b*
    } else {
        // Color space - 0x01 = RGB, all else are reserved and must be zero
        writeBytes(0x00, 0x01, 0x00, 0x00, 0x00, 0x00);
        writeBytes(8, 8, 8, 0); // Number of bits in component 1, 2, 3 & 4 respectively
        writeBytes(col.getRed(), col.getGreen(), col.getBlue()); // RGB intensity
    }
    commit(chained(SEC));
    this.currentColor = col;
}

From source file:org.apache.fop.pdf.PDFColorHandler.java

private boolean establishColorFromColor(StringBuffer codeBuffer, Color color, boolean fill) {
    ColorSpace cs = color.getColorSpace();
    if (cs instanceof DeviceCMYKColorSpace) {
        establishDeviceCMYK(codeBuffer, color, fill);
        return true;
    } else if (!cs.isCS_sRGB()) {
        if (cs instanceof ICC_ColorSpace) {
            PDFICCBasedColorSpace pdfcs = getICCBasedColorSpace((ICC_ColorSpace) cs);
            establishColor(codeBuffer, pdfcs, color, fill);
            return true;
        } else if (cs instanceof NamedColorSpace) {
            PDFSeparationColorSpace sepcs = getSeparationColorSpace((NamedColorSpace) cs);
            establishColor(codeBuffer, sepcs, color, fill);
            return true;
        } else if (cs instanceof CIELabColorSpace) {
            CIELabColorSpace labcs = (CIELabColorSpace) cs;
            PDFCIELabColorSpace pdflab = getCIELabColorSpace(labcs);
            selectColorSpace(codeBuffer, pdflab, fill);
            float[] comps = color.getColorComponents(null);
            float[] nativeComps = labcs.toNativeComponents(comps);
            writeColor(codeBuffer, nativeComps, labcs.getNumComponents(), (fill ? "sc" : "SC"));
            return true;
        }//w w w.  jav a 2  s . c  o m
    }
    return false;
}

From source file:org.apache.fop.pdf.PDFColorHandler.java

private void establishDeviceRGB(StringBuffer codeBuffer, Color color, boolean fill) {
    float[] comps;
    if (color.getColorSpace().isCS_sRGB()) {
        comps = color.getColorComponents(null);
    } else {/*from   w  w  w .j  a  v  a 2 s  .  c om*/
        if (log.isDebugEnabled()) {
            log.debug("Converting color to sRGB as a fallback: " + color);
        }
        ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        comps = color.getColorComponents(sRGB, null);
    }
    if (ColorUtil.isGray(color)) {
        comps = new float[] { comps[0] }; //assuming that all components are the same
        writeColor(codeBuffer, comps, 1, (fill ? "g" : "G"));
    } else {
        writeColor(codeBuffer, comps, 3, (fill ? "rg" : "RG"));
    }
}

From source file:org.apache.fop.pdf.PDFColorHandler.java

private void writeColor(StringBuffer codeBuffer, Color color, int componentCount, String command) {
    float[] comps = color.getColorComponents(null);
    writeColor(codeBuffer, comps, componentCount, command);
}

From source file:org.apache.fop.pdf.PDFFactory.java

private List toColorVector(Color nextColor) {
    List vector = new java.util.ArrayList();
    float[] comps = nextColor.getColorComponents(null);
    for (int i = 0, c = comps.length; i < c; i++) {
        vector.add(new Double(comps[i]));
    }/*from  w  ww  .  j  a va 2  s.co m*/
    return vector;
}

From source file:org.apache.fop.util.ColorUtil.java

/**
 * Creates a re-parsable string representation of the given color.
 * <p>//from  w w w . j  a  va2s  .com
 * First, the color will be converted into the sRGB colorspace. It will then
 * be printed as #rrggbb, or as #rrrggbbaa if an alpha value is present.
 *
 * @param color
 *            the color to represent.
 * @return a re-parsable string representadion.
 */
public static String colorToString(Color color) {
    ColorSpace cs = color.getColorSpace();
    if (color instanceof ColorWithAlternatives) {
        return toFunctionCall((ColorWithAlternatives) color);
    } else if (cs != null && cs.getType() == ColorSpace.TYPE_CMYK) {
        StringBuffer sbuf = new StringBuffer(24);
        float[] cmyk = color.getColorComponents(null);
        sbuf.append("cmyk(").append(cmyk[0]).append(",").append(cmyk[1]).append(",").append(cmyk[2]).append(",")
                .append(cmyk[3]).append(")");
        return sbuf.toString();
    } else {
        return toRGBFunctionCall(color);
    }
}

From source file:org.apache.fop.util.ColorUtil.java

/**
 * Create string representation of a fop-rgb-icc (or fop-rgb-named-color) function call from
 * the given color./*from   w  w  w .j av a  2s.c o m*/
 * @param color the color to turn into a function call
 * @return the string representing the internal fop-rgb-icc() or fop-rgb-named-color()
 *           function call
 */
private static String toFunctionCall(ColorWithAlternatives color) {
    ColorSpace cs = color.getColorSpace();
    if (cs.isCS_sRGB() && !color.hasAlternativeColors()) {
        return toRGBFunctionCall(color);
    }
    if (cs instanceof CIELabColorSpace) {
        return toCIELabFunctionCall(color);
    }

    Color specColor = color;
    if (color.hasAlternativeColors()) {
        Color alt = color.getAlternativeColors()[0];
        if (ColorSpaces.isDeviceColorSpace(alt.getColorSpace())) {
            cs = alt.getColorSpace();
            specColor = alt;
        }
    }
    ColorSpaceOrigin origin = ColorSpaces.getColorSpaceOrigin(cs);
    String functionName;

    Color fallbackColor = getsRGBFallback(color);
    float[] rgb = fallbackColor.getColorComponents(null);
    assert rgb.length == 3;
    StringBuffer sb = new StringBuffer(40);
    sb.append("(");
    sb.append(rgb[0]).append(",");
    sb.append(rgb[1]).append(",");
    sb.append(rgb[2]).append(",");
    String profileName = origin.getProfileName();
    sb.append(profileName).append(",");
    if (origin.getProfileURI() != null) {
        sb.append("\"").append(origin.getProfileURI()).append("\"");
    }

    if (cs instanceof NamedColorSpace) {
        NamedColorSpace ncs = (NamedColorSpace) cs;
        if (SEPARATION_PSEUDO_PROFILE.equalsIgnoreCase(profileName)) {
            functionName = "fop-rgb-icc";
        } else {
            functionName = "fop-rgb-named-color";
        }
        sb.append(",").append(ncs.getColorName());
    } else {
        functionName = "fop-rgb-icc";
        float[] colorComponents = specColor.getColorComponents(null);
        for (float colorComponent : colorComponents) {
            sb.append(",");
            sb.append(colorComponent);
        }
    }
    sb.append(")");
    return functionName + sb.toString();
}

From source file:org.apache.pdfbox.pdmodel.edit.PDPageContentStream.java

/**
 * Set the stroking color, specified as RGB.
 *
 * @param color The color to set.//ww w .jav a 2s  . c om
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setStrokingColor(Color color) throws IOException {
    ColorSpace colorSpace = color.getColorSpace();
    if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
        setStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
    } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
        color.getColorComponents(colorComponents);
        setStrokingColor(colorComponents[0]);
    } else if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        color.getColorComponents(colorComponents);
        setStrokingColor(colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

From source file:org.apache.pdfbox.pdmodel.edit.PDPageContentStream.java

/**
 * Set the non stroking color, specified as RGB.
 *
 * @param color The color to set./*w  ww.  j a va  2  s  . c om*/
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(Color color) throws IOException {
    ColorSpace colorSpace = color.getColorSpace();
    if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
        setNonStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
    } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
        color.getColorComponents(colorComponents);
        setNonStrokingColor(colorComponents[0]);
    } else if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        color.getColorComponents(colorComponents);
        setNonStrokingColor(colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}