123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- /*
- * Copyright 1999-2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id$ */
-
- package org.apache.fop.pdf;
-
- // Java
- import java.util.List;
- import java.util.ArrayList;
-
- /**
- * PDF Color object.
- * This is used to output color to a PDF content stream.
- */
- public class PDFColor extends PDFPathPaint {
- // could be 3.0 as well.
- private static double blackFactor = 2.0;
- private double red = -1.0;
- private double green = -1.0;
- private double blue = -1.0;
-
- private double cyan = -1.0;
- private double magenta = -1.0;
- private double yellow = -1.0;
- private double black = -1.0;
-
- /**
- * Create a PDF color with double values ranging from 0 to 1
- *
- * @param theRed the red double value
- * @param theGreen the green double value
- * @param theBlue the blue double value
- */
- public PDFColor(double theRed, double theGreen, double theBlue) {
- // super(theNumber);
- this.colorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB);
-
- this.red = theRed;
- this.green = theGreen;
- this.blue = theBlue;
- }
-
- /**
- * Create a PDF color with int values ranging from 0 to 255
- *
- * @param theRed the red integer value
- * @param theGreen the green integer value
- * @param theBlue the blue integer value
- */
- public PDFColor(int theRed, int theGreen, int theBlue) {
- this(((double)theRed) / 255d, ((double)theGreen) / 255d,
- ((double)theBlue) / 255d);
-
- }
-
- /**
- * Create a PDF color with CMYK values.
- *
- * @param theCyan the cyan value
- * @param theMagenta the magenta value
- * @param theYellow the yellow value
- * @param theBlack the black value
- */
- public PDFColor(double theCyan, double theMagenta, double theYellow,
- double theBlack) {
- // super(theNumber);//?
-
- this.colorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_CMYK);
-
- this.cyan = theCyan;
- this.magenta = theMagenta;
- this.yellow = theYellow;
- this.black = theBlack;
- }
-
- /**
- * Return a vector representation of the color
- * in the appropriate colorspace.
- *
- * @return a list containing the Double values of the color
- */
- public List getVector() {
- List theColorVector = new ArrayList();
- if (this.colorSpace.getColorSpace() == PDFColorSpace.DEVICE_RGB) {
- // RGB
- theColorVector.add(new Double(this.red));
- theColorVector.add(new Double(this.green));
- theColorVector.add(new Double(this.blue));
- } else if (this.colorSpace.getColorSpace()
- == PDFColorSpace.DEVICE_CMYK) {
- // CMYK
- theColorVector.add(new Double(this.cyan));
- theColorVector.add(new Double(this.magenta));
- theColorVector.add(new Double(this.yellow));
- theColorVector.add(new Double(this.black));
- } else {
- // GRAY
- theColorVector.add(new Double(this.black));
- }
- return (theColorVector);
- }
-
- /**
- * Get the red component.
- *
- * @return the red double value
- */
- public double red() {
- return (this.red);
- }
-
- /**
- * Get the green component.
- *
- * @return the green double value
- */
- public double green() {
- return (this.green);
- }
-
- /**
- * Get the blue component.
- *
- * @return the blue double value
- */
- public double blue() {
- return (this.blue);
- }
-
- /**
- * Get the red integer component.
- *
- * @return the red integer value
- */
- public int red255() {
- return (int)(this.red * 255d);
- }
-
- /**
- * Get the green integer component.
- *
- * @return the green integer value
- */
- public int green255() {
- return (int)(this.green * 255d);
- }
-
- /**
- * Get the blue integer component.
- *
- * @return the blue integer value
- */
- public int blue255() {
- return (int)(this.blue * 255d);
- }
-
- /**
- * Get the cyan component.
- *
- * @return the cyan double value
- */
- public double cyan() {
- return (this.cyan);
- }
-
- /**
- * Get the magenta component.
- *
- * @return the magenta double value
- */
- public double magenta() {
- return (this.magenta);
- }
-
- /**
- * Get the yellow component.
- *
- * @return the yellow double value
- */
- public double yellow() {
- return (this.yellow);
- }
-
- /**
- * Get the black component.
- *
- * @return the black double value
- */
- public double black() {
- return (this.black);
- }
-
- /**
- * Set the color space for this color.
- * If the new color space is different the values are converted
- * to the new color space.
- *
- * @param theColorSpace the new color space
- */
- public void setColorSpace(int theColorSpace) {
- int theOldColorSpace = this.colorSpace.getColorSpace();
- if (theOldColorSpace != theColorSpace) {
- if (theOldColorSpace == PDFColorSpace.DEVICE_RGB) {
- if (theColorSpace == PDFColorSpace.DEVICE_CMYK) {
- this.convertRGBtoCMYK();
- } else {
- // convert to Gray?
- this.convertRGBtoGRAY();
- }
- } else if (theOldColorSpace == PDFColorSpace.DEVICE_CMYK) {
- if (theColorSpace == PDFColorSpace.DEVICE_RGB) {
- this.convertCMYKtoRGB();
- } else {
- // convert to Gray?
- this.convertCMYKtoGRAY();
- }
- } else {
- // used to be Gray
- if (theColorSpace == PDFColorSpace.DEVICE_RGB) {
- this.convertGRAYtoRGB();
- } else {
- // convert to CMYK?
- this.convertGRAYtoCMYK();
- }
- }
- this.colorSpace.setColorSpace(theColorSpace);
- }
- }
-
- /**
- * Get the PDF output string for this color.
- * This returns the string to be inserted into PDF for setting
- * the current color.
- *
- * @param fillNotStroke whether to return fill or stroke command
- * @return the PDF string for setting the fill/stroke color
- */
- public String getColorSpaceOut(boolean fillNotStroke) {
- StringBuffer p = new StringBuffer("");
-
- double tempDouble;
-
- if (this.colorSpace.getColorSpace()
- == PDFColorSpace.DEVICE_RGB) { // colorspace is RGB
- // according to pdfspec 12.1 p.399
- // if the colors are the same then just use the g or G operator
- boolean same = false;
- if (this.red == this.green && this.red == this.blue) {
- same = true;
- }
- // output RGB
- if (fillNotStroke) {
- // fill
- if (same) {
- p.append(PDFNumber.doubleOut(this.red) + " g\n");
- } else {
- p.append(PDFNumber.doubleOut(this.red) + " "
- + PDFNumber.doubleOut(this.green) + " "
- + PDFNumber.doubleOut(this.blue)
- + " rg\n");
- }
- } else {
- // stroke/border
- if (same) {
- p.append(PDFNumber.doubleOut(this.red) + " G\n");
- } else {
- p.append(PDFNumber.doubleOut(this.red) + " "
- + PDFNumber.doubleOut(this.green) + " "
- + PDFNumber.doubleOut(this.blue)
- + " RG\n");
- }
- }
- } else if (this.colorSpace.getColorSpace()
- == PDFColorSpace.DEVICE_CMYK) {
- // colorspace is CMYK
-
- if (fillNotStroke) {
- // fill
- p.append(PDFNumber.doubleOut(this.cyan) + " "
- + PDFNumber.doubleOut(this.magenta) + " "
- + PDFNumber.doubleOut(this.yellow) + " "
- + PDFNumber.doubleOut(this.black) + " k\n");
- } else {
- // stroke
- p.append(PDFNumber.doubleOut(this.cyan) + " "
- + PDFNumber.doubleOut(this.magenta) + " "
- + PDFNumber.doubleOut(this.yellow) + " "
- + PDFNumber.doubleOut(this.black) + " K\n");
- }
-
- } else {
- // means we're in DeviceGray or Unknown.
- // assume we're in DeviceGray, because otherwise we're screwed.
-
- if (fillNotStroke) {
- p.append(PDFNumber.doubleOut(this.black) + " g\n");
- } else {
- p.append(PDFNumber.doubleOut(this.black) + " G\n");
- }
-
- }
- return (p.toString());
- }
-
- /**
- * Convert the color from CMYK to RGB.
- */
- protected void convertCMYKtoRGB() {
- // convert CMYK to RGB
- this.red = 1.0 - this.cyan;
- this.green = 1.0 - this.green;
- this.blue = 1.0 - this.yellow;
-
- this.red = (this.black / PDFColor.blackFactor) + this.red;
- this.green = (this.black / PDFColor.blackFactor) + this.green;
- this.blue = (this.black / PDFColor.blackFactor) + this.blue;
-
- }
-
- /**
- * Convert the color from RGB to CMYK.
- */
- protected void convertRGBtoCMYK() {
- // convert RGB to CMYK
- this.cyan = 1.0 - this.red;
- this.magenta = 1.0 - this.green;
- this.yellow = 1.0 - this.blue;
-
- this.black = 0.0;
- /*
- * If you want to calculate black, uncomment this
- * //pick the lowest color
- * tempDouble = this.red;
- *
- * if (this.green < tempDouble)
- * tempDouble = this.green;
- *
- * if (this.blue < tempDouble)
- * tempDouble = this.blue;
- *
- * this.black = tempDouble / this.blackFactor;
- */
- }
-
- /**
- * Convert the color from Gray to RGB.
- */
- protected void convertGRAYtoRGB() {
- this.red = 1.0 - this.black;
- this.green = 1.0 - this.black;
- this.blue = 1.0 - this.black;
- }
-
- /**
- * Convert the color from Gray to CMYK.
- */
- protected void convertGRAYtoCMYK() {
- this.cyan = this.black;
- this.magenta = this.black;
- this.yellow = this.black;
- // this.black=0.0;//?
- }
-
- /**
- * Convert the color from CMYK to Gray.
- */
- protected void convertCMYKtoGRAY() {
- double tempDouble = 0.0;
-
- // pick the lowest color
- tempDouble = this.cyan;
-
- if (this.magenta < tempDouble) {
- tempDouble = this.magenta;
- }
-
- if (this.yellow < tempDouble) {
- tempDouble = this.yellow;
- }
-
- this.black = (tempDouble / PDFColor.blackFactor);
-
- }
-
- /**
- * Convert the color from RGB to Gray.
- */
- protected void convertRGBtoGRAY() {
- double tempDouble = 0.0;
-
- // pick the lowest color
- tempDouble = this.red;
-
- if (this.green < tempDouble) {
- tempDouble = this.green;
- }
-
- if (this.blue < tempDouble) {
- tempDouble = this.blue;
- }
-
- this.black = 1.0 - (tempDouble / PDFColor.blackFactor);
- }
-
- /**
- * Create pdf.
- * Not used for this object.
- *
- * @return the bytes for the pdf
- */
- public byte[] toPDF() {
- return (new byte[0]);
- }
-
- /**
- * Check for equality of color with another object.
- *
- * @param obj the object to compare
- * @return true if colors are equal
- */
- public boolean equals(Object obj) {
- if (!(obj instanceof PDFColor)) {
- return false;
- }
- PDFColor color = (PDFColor)obj;
-
- if (color.red == this.red && color.green == this.green
- && color.blue == this.blue) {
- return true;
- }
- return false;
- }
-
- }
|