Example usage for java.awt Color getColorSpace

List of usage examples for java.awt Color getColorSpace

Introduction

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

Prototype

public ColorSpace getColorSpace() 

Source Link

Document

Returns the ColorSpace of this Color .

Usage

From source file:Main.java

public static void main(String[] args) {
    Color myColor = Color.RED;

    System.out.println(myColor.getColorSpace().getNumComponents());

}

From source file:de.saring.util.gui.jfreechart.StackedRenderer.java

/**
 * Returns the paint used to fill an item drawn by the renderer.
 * Overridden for proper display of legend items
 *
 * @param series the series index (zero-based).
 * @return The paint (never <code>null</code>).
 *///ww w  . j  ava  2  s  . c  om
@Override
public Paint lookupSeriesPaint(int series) {
    Paint paint = super.lookupSeriesPaint(series);
    if (paint instanceof Color) {
        Color color = (Color) paint;
        paint = new Color(color.getColorSpace(), color.getColorComponents(null), areaAlpha);
    }
    return paint;
}

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

/**
 * Set the stroking color, specified as RGB.
 * /*from  www .j a  va 2  s.  co m*/
 * @param color
 *            The color to set.
 * @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[1], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

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

/**
 * Set the non stroking color, specified as RGB.
 * //w w  w  . j a v a  2 s .c  om
 * @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.goca.GraphicsSetProcessColor.java

/**
 * Main constructor//from   www.  ja v a  2 s .c o  m
 *
 * @param color the color to set
 */
public GraphicsSetProcessColor(Color color) {
    if (color instanceof ColorWithAlternatives) {
        ColorWithAlternatives cwa = (ColorWithAlternatives) color;
        Color alt = cwa.getFirstAlternativeOfType(ColorSpace.TYPE_CMYK);
        if (alt != null) {
            this.color = alt;
            this.componentsSize = 4;
            return;
        }
    }
    ColorSpace cs = color.getColorSpace();
    int colSpaceType = cs.getType();
    if (colSpaceType == ColorSpace.TYPE_CMYK) {
        this.color = color;
    } else if (cs instanceof CIELabColorSpace) {
        //TODO Convert between illuminants if not D50 according to rendering intents
        //Right now, we're assuming D50 as the GOCA spec requires.
        this.color = color;
        //16bit components didn't work, and 8-bit sadly has reduced accuracy.
    } else {
        if (!color.getColorSpace().isCS_sRGB()) {
            this.color = ColorUtil.toSRGBColor(color);
        } else {
            this.color = color;
        }
    }
    this.componentsSize = this.color.getColorSpace().getNumComponents();
}

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>// ww  w.j a v  a 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;
        }/*from w  ww. j  a  va 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 {/* ww  w  . j av a  2 s  .com*/
        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.PDFFactory.java

/**
 * Make a gradient//from w  w  w  .  j  a  v a2s.c om
 *
 * @param res the PDF resource context to add the shading, may be null
 * @param radial if true a radial gradient will be created
 * @param theColorspace the colorspace of the gradient
 * @param theColors the list of colors for the gradient
 * @param theBounds the list of bounds associated with the colors
 * @param theCoords the coordinates for the gradient
 * @param theMatrix the coordinate-transformation matrix
 * @return the PDF pattern that was created
 */
public PDFPattern makeGradient(PDFResourceContext res, boolean radial, PDFDeviceColorSpace theColorspace,
        List theColors, List theBounds, List theCoords, List theMatrix) {
    PDFShading myShad;
    PDFFunction myfunky;
    PDFFunction myfunc;
    List theCzero;
    List theCone;
    PDFPattern myPattern;
    //PDFColorSpace theColorSpace;
    double interpolation = 1.000;
    List theFunctions = new ArrayList();

    int currentPosition;
    int lastPosition = theColors.size() - 1;

    // if 5 elements, the penultimate element is 3.
    // do not go beyond that, because you always need
    // to have a next color when creating the function.

    for (currentPosition = 0; currentPosition < lastPosition; currentPosition++) { // for every consecutive color pair
        Color currentColor = (Color) theColors.get(currentPosition);
        Color nextColor = (Color) theColors.get(currentPosition + 1);

        // colorspace must be consistent, so we simply convert to sRGB where necessary
        if (!currentColor.getColorSpace().isCS_sRGB()) {
            //Convert to sRGB
            currentColor = ColorUtil.toSRGBColor(currentColor);
            theColors.set(currentPosition, currentColor);
        }
        if (!nextColor.getColorSpace().isCS_sRGB()) {
            //Convert to sRGB
            nextColor = ColorUtil.toSRGBColor(nextColor);
            theColors.set(currentPosition + 1, nextColor);
        }

        theCzero = toColorVector(currentColor);
        theCone = toColorVector(nextColor);

        myfunc = makeFunction(2, null, null, theCzero, theCone, interpolation);

        theFunctions.add(myfunc);

    } // end of for every consecutive color pair

    myfunky = makeFunction(3, null, null, theFunctions, theBounds, null);

    if (radial) {
        if (theCoords.size() == 6) {
            myShad = makeShading(res, 3, getDocument().getPDFColorSpace(), null, null, false, theCoords, null,
                    myfunky, null);
        } else { // if the center x, center y, and radius specifiy
            // the gradient, then assume the same center x, center y,
            // and radius of zero for the other necessary component
            List newCoords = new ArrayList();
            newCoords.add(theCoords.get(0));
            newCoords.add(theCoords.get(1));
            newCoords.add(theCoords.get(2));
            newCoords.add(theCoords.get(0));
            newCoords.add(theCoords.get(1));
            newCoords.add(new Double(0.0));

            myShad = makeShading(res, 3, getDocument().getPDFColorSpace(), null, null, false, newCoords, null,
                    myfunky, null);

        }
    } else {
        myShad = makeShading(res, 2, getDocument().getPDFColorSpace(), null, null, false, theCoords, null,
                myfunky, null);

    }

    myPattern = makePattern(res, 2, myShad, null, null, theMatrix);

    return (myPattern);
}

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

/**
 * Creates a re-parsable string representation of the given color.
 * <p>/*from w  w w .java 2s .  c o m*/
 * 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);
    }
}