1 51 package org.apache.fop.pdf; 52 53 import java.util.ArrayList ; 55 56 import org.apache.fop.datatypes.ColorSpace; 58 59 public class PDFColor extends PDFPathPaint { 60 protected static double blackFactor = 2.0; protected double red = -1.0; 62 protected double green = -1.0; 63 protected double blue = -1.0; 64 65 protected double cyan = -1.0; 66 protected double magenta = -1.0; 67 protected double yellow = -1.0; 68 protected double black = -1.0; 69 70 public PDFColor(org.apache.fop.datatypes.ColorType theColor) { 71 this.colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 72 this.red = (double)theColor.red(); 74 this.green = (double)theColor.green(); 75 this.blue = (double)theColor.blue(); 76 77 } 78 79 public PDFColor(double theRed, double theGreen, double theBlue) { 80 this.colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 82 83 this.red = theRed; 84 this.green = theGreen; 85 this.blue = theBlue; 86 } 87 88 public PDFColor(int theRed, int theGreen, int theBlue) { 90 this(((double)theRed) / 255d, ((double)theGreen) / 255d, 91 ((double)theBlue) / 255d); 92 93 } 94 95 public PDFColor(double theCyan, double theMagenta, double theYellow, 96 double theBlack) { 97 99 this.colorSpace = new ColorSpace(ColorSpace.DEVICE_CMYK); 100 101 this.cyan = theCyan; 102 this.magenta = theMagenta; 103 this.yellow = theYellow; 104 this.black = theBlack; 105 } 106 107 108 public ArrayList getVector() { ArrayList theColorVector = new ArrayList (); 111 if (this.colorSpace.getColorSpace() == ColorSpace.DEVICE_RGB) { theColorVector.add(new Double (this.red)); 113 theColorVector.add(new Double (this.green)); 114 theColorVector.add(new Double (this.blue)); 115 } else if (this.colorSpace.getColorSpace() 116 == ColorSpace.DEVICE_CMYK) { theColorVector.add(new Double (this.cyan)); 118 theColorVector.add(new Double (this.magenta)); 119 theColorVector.add(new Double (this.yellow)); 120 theColorVector.add(new Double (this.black)); 121 } else { theColorVector.add(new Double (this.black)); 123 } 124 return (theColorVector); 125 } 126 127 public double red() { 128 return (this.red); 129 } 130 131 public double green() { 132 return (this.green); 133 } 134 135 public double blue() { 136 return (this.blue); 137 } 138 139 public int red255() { 140 return (int)(this.red * 255d); 141 } 142 143 public int green255() { 144 return (int)(this.green * 255d); 145 } 146 147 public int blue255() { 148 return (int)(this.blue * 255d); 149 } 150 151 public double cyan() { 152 return (this.cyan); 153 } 154 155 public double magenta() { 156 return (this.magenta); 157 } 158 159 public double yellow() { 160 return (this.yellow); 161 } 162 163 public double black() { 164 return (this.black); 165 } 166 167 public void setColorSpace(int theColorSpace) { 168 int theOldColorSpace = this.colorSpace.getColorSpace(); 169 if (theOldColorSpace != theColorSpace) { 170 if (theOldColorSpace == ColorSpace.DEVICE_RGB) { 171 if (theColorSpace == ColorSpace.DEVICE_CMYK) { 172 this.convertRGBtoCMYK(); 173 } else { 175 this.convertRGBtoGRAY(); 176 } 177 178 } else if (theOldColorSpace == ColorSpace.DEVICE_CMYK) { 179 if (theColorSpace == ColorSpace.DEVICE_RGB) { 180 this.convertCMYKtoRGB(); 181 } else { 183 this.convertCMYKtoGRAY(); 184 } 185 } else { 187 if (theColorSpace == ColorSpace.DEVICE_RGB) { 188 this.convertGRAYtoRGB(); 189 } else { 191 this.convertGRAYtoCMYK(); 192 } 193 } 194 this.colorSpace.setColorSpace(theColorSpace); 195 } 196 } 197 198 public String getColorSpaceOut(boolean fillNotStroke) { 199 StringBuffer p = new StringBuffer (""); 200 201 double tempDouble; 202 203 if (this.colorSpace.getColorSpace() 204 == ColorSpace.DEVICE_RGB) { boolean same = false; 208 if (this.red == this.green && this.red == this.blue) { 209 same = true; 210 } 211 if (fillNotStroke) { if (same) { 214 p.append(PDFNumber.doubleOut(this.red) + " g\n"); 215 } else { 216 p.append(PDFNumber.doubleOut(this.red) + " " 217 + PDFNumber.doubleOut(this.green) + " " 218 + PDFNumber.doubleOut(this.blue) + " " 219 + " rg \n"); 220 } 221 } else { if (same) { 223 p.append(PDFNumber.doubleOut(this.red) + " G\n"); 224 } else { 225 p.append(PDFNumber.doubleOut(this.red) + " " 226 + PDFNumber.doubleOut(this.green) + " " 227 + PDFNumber.doubleOut(this.blue) + " " 228 + " RG \n"); 229 } 230 } 231 } else if (this.colorSpace.getColorSpace() 233 == ColorSpace.DEVICE_CMYK) { 235 if (fillNotStroke) { p.append(PDFNumber.doubleOut(this.cyan) + " " 237 + PDFNumber.doubleOut(this.magenta) + " " 238 + PDFNumber.doubleOut(this.yellow) + " " 239 + PDFNumber.doubleOut(this.black) + " k \n"); 240 } else { p.append(PDFNumber.doubleOut(this.cyan) + " " 242 + PDFNumber.doubleOut(this.magenta) + " " 243 + PDFNumber.doubleOut(this.yellow) + " " 244 + PDFNumber.doubleOut(this.black) + " K \n"); 245 } 246 247 } else { 251 if (fillNotStroke) { 252 p.append(PDFNumber.doubleOut(this.black) + " g \n"); 253 } else { 254 p.append(PDFNumber.doubleOut(this.black) + " G \n"); 255 } 256 257 } 258 return (p.toString()); 259 } 260 261 262 263 264 protected void convertCMYKtoRGB() { 265 this.red = 1.0 - this.cyan; 267 this.green = 1.0 - this.green; 268 this.blue = 1.0 - this.yellow; 269 270 this.red = (this.black / this.blackFactor) + this.red; 271 this.green = (this.black / this.blackFactor) + this.green; 272 this.blue = (this.black / this.blackFactor) + this.blue; 273 274 } 275 276 protected void convertRGBtoCMYK() { 277 this.cyan = 1.0 - this.red; 279 this.magenta = 1.0 - this.green; 280 this.yellow = 1.0 - this.blue; 281 282 this.black = 0.0; 283 296 } 297 298 protected void convertGRAYtoRGB() { 299 this.red = 1.0 - this.black; 300 this.green = 1.0 - this.black; 301 this.blue = 1.0 - this.black; 302 } 303 304 protected void convertGRAYtoCMYK() { 305 this.cyan = this.black; 306 this.magenta = this.black; 307 this.yellow = this.black; 308 } 310 311 protected void convertCMYKtoGRAY() { 312 double tempDouble = 0.0; 313 314 tempDouble = this.cyan; 316 317 if (this.magenta < tempDouble) 318 tempDouble = this.magenta; 319 320 if (this.yellow < tempDouble) 321 tempDouble = this.yellow; 322 323 this.black = (tempDouble / this.blackFactor); 324 325 } 326 327 protected void convertRGBtoGRAY() { 328 double tempDouble = 0.0; 329 330 tempDouble = this.red; 332 333 if (this.green < tempDouble) 334 tempDouble = this.green; 335 336 if (this.blue < tempDouble) 337 tempDouble = this.blue; 338 339 this.black = 1.0 - (tempDouble / this.blackFactor); 340 341 } 342 343 byte[] toPDF() { 344 return (new byte[0]); 345 346 } 348 public boolean equals(Object obj) { 349 if (!(obj instanceof PDFColor)) { 350 return false; 351 } 352 PDFColor color = (PDFColor)obj; 353 354 if (color.red == this.red && color.green == this.green 355 && color.blue == this.blue) { 356 return true; 357 } 358 return false; 359 } 360 361 } 362 | Popular Tags |