You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFColor.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.awt.color.ColorSpace;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace;
  23. /**
  24. * PDF Color object. It is currently only used to hold the transparent color of a masked bitmap
  25. * image. And in this context, only RGB and Gray values are used.
  26. * <p>
  27. * Use of this class is discouraged. {@link PDFColorHandler} is now used for in-content color
  28. * selection. For masked bitmaps, it may be wiser to switch to {@link java.awt.Color} in the long run.
  29. */
  30. public class PDFColor extends PDFPathPaint {
  31. // could be 3.0 as well.
  32. private static double blackFactor = 2.0;
  33. private double red = -1.0;
  34. private double green = -1.0;
  35. private double blue = -1.0;
  36. private double cyan = -1.0;
  37. private double magenta = -1.0;
  38. private double yellow = -1.0;
  39. private double black = -1.0;
  40. /**
  41. * Create a PDF color with double values ranging from 0 to 1.
  42. *
  43. * @param theRed the red double value
  44. * @param theGreen the green double value
  45. * @param theBlue the blue double value
  46. */
  47. public PDFColor(double theRed, double theGreen, double theBlue) {
  48. // super(theNumber);
  49. this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  50. this.red = theRed;
  51. this.green = theGreen;
  52. this.blue = theBlue;
  53. }
  54. /**
  55. * Create a PDF color from a java.awt.Color object.
  56. *
  57. * Different Color objects are handled differently. Cases recognized are.
  58. *
  59. * 1. CMYK color
  60. * 3. 'Normal' java.awt.Color (RGB case assumed or implicit conversion to sRGB)
  61. *
  62. * @param col the java.awt.Color object for which to create a PDFColor object
  63. */
  64. public PDFColor(java.awt.Color col) {
  65. ColorSpace cs = col.getColorSpace();
  66. if (cs != null && cs instanceof DeviceCMYKColorSpace) {
  67. // CMYK case
  68. this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_CMYK);
  69. float[] cmyk = col.getColorComponents(null);
  70. this.cyan = cmyk[0];
  71. this.magenta = cmyk[1];
  72. this.yellow = cmyk[2];
  73. this.black = cmyk[3];
  74. } else {
  75. // Default (RGB) Color (ICC Colors are converted to sRGB, too)
  76. this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  77. float[] comps = new float[3];
  78. comps = col.getColorComponents(comps);
  79. this.red = comps[0];
  80. this.green = comps[1];
  81. this.blue = comps[2];
  82. }
  83. }
  84. /**
  85. * Create a PDF color with int values ranging from 0 to 255
  86. *
  87. * @param theRed the red integer value
  88. * @param theGreen the green integer value
  89. * @param theBlue the blue integer value
  90. */
  91. public PDFColor(int theRed, int theGreen, int theBlue) {
  92. this(((double)theRed) / 255d, ((double)theGreen) / 255d,
  93. ((double)theBlue) / 255d);
  94. }
  95. /**
  96. * Create a PDF color with CMYK values.
  97. *
  98. * @param theCyan the cyan value
  99. * @param theMagenta the magenta value
  100. * @param theYellow the yellow value
  101. * @param theBlack the black value
  102. */
  103. public PDFColor(double theCyan, double theMagenta, double theYellow,
  104. double theBlack) {
  105. this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_CMYK);
  106. this.cyan = theCyan;
  107. this.magenta = theMagenta;
  108. this.yellow = theYellow;
  109. this.black = theBlack;
  110. }
  111. /**
  112. * Return a vector representation of the color
  113. * in the appropriate colorspace.
  114. *
  115. * @return a list containing the Double values of the color
  116. */
  117. public List getVector() {
  118. List theColorVector = new ArrayList();
  119. if (this.colorSpace.getColorSpace() == PDFDeviceColorSpace.DEVICE_RGB) {
  120. // RGB
  121. theColorVector.add(this.red);
  122. theColorVector.add(this.green);
  123. theColorVector.add(this.blue);
  124. } else if (this.colorSpace.getColorSpace()
  125. == PDFDeviceColorSpace.DEVICE_CMYK) {
  126. // CMYK
  127. theColorVector.add(this.cyan);
  128. theColorVector.add(this.magenta);
  129. theColorVector.add(this.yellow);
  130. theColorVector.add(this.black);
  131. } else {
  132. // GRAY
  133. theColorVector.add(this.black);
  134. }
  135. return (theColorVector);
  136. }
  137. /**
  138. * Get the red component.
  139. *
  140. * @return the red double value
  141. */
  142. public double red() {
  143. return (this.red);
  144. }
  145. /**
  146. * Get the green component.
  147. *
  148. * @return the green double value
  149. */
  150. public double green() {
  151. return (this.green);
  152. }
  153. /**
  154. * Get the blue component.
  155. *
  156. * @return the blue double value
  157. */
  158. public double blue() {
  159. return (this.blue);
  160. }
  161. /**
  162. * Get the red integer component.
  163. *
  164. * @return the red integer value
  165. */
  166. public int red255() {
  167. return (int)(this.red * 255d);
  168. }
  169. /**
  170. * Get the green integer component.
  171. *
  172. * @return the green integer value
  173. */
  174. public int green255() {
  175. return (int)(this.green * 255d);
  176. }
  177. /**
  178. * Get the blue integer component.
  179. *
  180. * @return the blue integer value
  181. */
  182. public int blue255() {
  183. return (int)(this.blue * 255d);
  184. }
  185. /**
  186. * Get the cyan component.
  187. *
  188. * @return the cyan double value
  189. */
  190. public double cyan() {
  191. return (this.cyan);
  192. }
  193. /**
  194. * Get the magenta component.
  195. *
  196. * @return the magenta double value
  197. */
  198. public double magenta() {
  199. return (this.magenta);
  200. }
  201. /**
  202. * Get the yellow component.
  203. *
  204. * @return the yellow double value
  205. */
  206. public double yellow() {
  207. return (this.yellow);
  208. }
  209. /**
  210. * Get the black component.
  211. *
  212. * @return the black double value
  213. */
  214. public double black() {
  215. return (this.black);
  216. }
  217. /**
  218. * Set the color space for this color.
  219. * If the new color space is different the values are converted
  220. * to the new color space.
  221. *
  222. * @param theColorSpace the new color space
  223. */
  224. public void setColorSpace(int theColorSpace) {
  225. int theOldColorSpace = this.colorSpace.getColorSpace();
  226. if (theOldColorSpace != theColorSpace) {
  227. if (theOldColorSpace == PDFDeviceColorSpace.DEVICE_RGB) {
  228. if (theColorSpace == PDFDeviceColorSpace.DEVICE_CMYK) {
  229. this.convertRGBtoCMYK();
  230. } else {
  231. // convert to Gray?
  232. this.convertRGBtoGRAY();
  233. }
  234. } else if (theOldColorSpace == PDFDeviceColorSpace.DEVICE_CMYK) {
  235. if (theColorSpace == PDFDeviceColorSpace.DEVICE_RGB) {
  236. this.convertCMYKtoRGB();
  237. } else {
  238. // convert to Gray?
  239. this.convertCMYKtoGRAY();
  240. }
  241. } else {
  242. // used to be Gray
  243. if (theColorSpace == PDFDeviceColorSpace.DEVICE_RGB) {
  244. this.convertGRAYtoRGB();
  245. } else {
  246. // convert to CMYK?
  247. this.convertGRAYtoCMYK();
  248. }
  249. }
  250. this.colorSpace.setColorSpace(theColorSpace);
  251. }
  252. }
  253. /**
  254. * Get the PDF output string for this color.
  255. * This returns the string to be inserted into PDF for setting
  256. * the current color.
  257. *
  258. * @param fillNotStroke whether to return fill or stroke command
  259. * @return the PDF string for setting the fill/stroke color
  260. */
  261. public String getColorSpaceOut(boolean fillNotStroke) {
  262. StringBuffer p = new StringBuffer("");
  263. if (this.colorSpace.getColorSpace()
  264. == PDFDeviceColorSpace.DEVICE_RGB) { // colorspace is RGB
  265. // according to pdfspec 12.1 p.399
  266. // if the colors are the same then just use the g or G operator
  267. boolean same = false;
  268. if (this.red == this.green && this.red == this.blue) {
  269. same = true;
  270. }
  271. // output RGB
  272. if (fillNotStroke) {
  273. // fill
  274. if (same) {
  275. p.append(PDFNumber.doubleOut(this.red) + " g\n");
  276. } else {
  277. p.append(PDFNumber.doubleOut(this.red) + " "
  278. + PDFNumber.doubleOut(this.green) + " "
  279. + PDFNumber.doubleOut(this.blue)
  280. + " rg\n");
  281. }
  282. } else {
  283. // stroke/border
  284. if (same) {
  285. p.append(PDFNumber.doubleOut(this.red) + " G\n");
  286. } else {
  287. p.append(PDFNumber.doubleOut(this.red) + " "
  288. + PDFNumber.doubleOut(this.green) + " "
  289. + PDFNumber.doubleOut(this.blue)
  290. + " RG\n");
  291. }
  292. }
  293. } else if (this.colorSpace.getColorSpace()
  294. == PDFDeviceColorSpace.DEVICE_CMYK) {
  295. // colorspace is CMYK
  296. if (fillNotStroke) {
  297. // fill
  298. p.append(PDFNumber.doubleOut(this.cyan) + " "
  299. + PDFNumber.doubleOut(this.magenta) + " "
  300. + PDFNumber.doubleOut(this.yellow) + " "
  301. + PDFNumber.doubleOut(this.black) + " k\n");
  302. } else {
  303. // stroke
  304. p.append(PDFNumber.doubleOut(this.cyan) + " "
  305. + PDFNumber.doubleOut(this.magenta) + " "
  306. + PDFNumber.doubleOut(this.yellow) + " "
  307. + PDFNumber.doubleOut(this.black) + " K\n");
  308. }
  309. } else {
  310. // means we're in DeviceGray or Unknown.
  311. // assume we're in DeviceGray, because otherwise we're screwed.
  312. if (fillNotStroke) {
  313. p.append(PDFNumber.doubleOut(this.black) + " g\n");
  314. } else {
  315. p.append(PDFNumber.doubleOut(this.black) + " G\n");
  316. }
  317. }
  318. return (p.toString());
  319. }
  320. /**
  321. * Convert the color from CMYK to RGB.
  322. */
  323. protected void convertCMYKtoRGB() {
  324. // convert CMYK to RGB
  325. this.red = 1.0 - this.cyan;
  326. this.green = 1.0 - this.green;
  327. this.blue = 1.0 - this.yellow;
  328. this.red = (this.black / PDFColor.blackFactor) + this.red;
  329. this.green = (this.black / PDFColor.blackFactor) + this.green;
  330. this.blue = (this.black / PDFColor.blackFactor) + this.blue;
  331. }
  332. /**
  333. * Convert the color from RGB to CMYK.
  334. */
  335. protected void convertRGBtoCMYK() {
  336. // convert RGB to CMYK
  337. this.cyan = 1.0 - this.red;
  338. this.magenta = 1.0 - this.green;
  339. this.yellow = 1.0 - this.blue;
  340. this.black = 0.0;
  341. /*
  342. * If you want to calculate black, uncomment this
  343. * //pick the lowest color
  344. * tempDouble = this.red;
  345. *
  346. * if (this.green < tempDouble)
  347. * tempDouble = this.green;
  348. *
  349. * if (this.blue < tempDouble)
  350. * tempDouble = this.blue;
  351. *
  352. * this.black = tempDouble / this.blackFactor;
  353. */
  354. }
  355. /**
  356. * Convert the color from Gray to RGB.
  357. */
  358. protected void convertGRAYtoRGB() {
  359. this.red = 1.0 - this.black;
  360. this.green = 1.0 - this.black;
  361. this.blue = 1.0 - this.black;
  362. }
  363. /**
  364. * Convert the color from Gray to CMYK.
  365. */
  366. protected void convertGRAYtoCMYK() {
  367. this.cyan = this.black;
  368. this.magenta = this.black;
  369. this.yellow = this.black;
  370. // this.black=0.0;//?
  371. }
  372. /**
  373. * Convert the color from CMYK to Gray.
  374. */
  375. protected void convertCMYKtoGRAY() {
  376. double tempDouble = 0.0;
  377. // pick the lowest color
  378. tempDouble = this.cyan;
  379. if (this.magenta < tempDouble) {
  380. tempDouble = this.magenta;
  381. }
  382. if (this.yellow < tempDouble) {
  383. tempDouble = this.yellow;
  384. }
  385. this.black = (tempDouble / PDFColor.blackFactor);
  386. }
  387. /**
  388. * Convert the color from RGB to Gray.
  389. */
  390. protected void convertRGBtoGRAY() {
  391. double tempDouble = 0.0;
  392. // pick the lowest color
  393. tempDouble = this.red;
  394. if (this.green < tempDouble) {
  395. tempDouble = this.green;
  396. }
  397. if (this.blue < tempDouble) {
  398. tempDouble = this.blue;
  399. }
  400. this.black = 1.0 - (tempDouble / PDFColor.blackFactor);
  401. }
  402. /**
  403. * Create pdf.
  404. * Not used for this object.
  405. *
  406. * @return the bytes for the pdf
  407. */
  408. public byte[] toPDF() {
  409. return (new byte[0]);
  410. }
  411. /** {@inheritDoc} */
  412. protected boolean contentEquals(PDFObject obj) {
  413. if (!(obj instanceof PDFColor)) {
  414. return false;
  415. }
  416. PDFColor color = (PDFColor)obj;
  417. if (color.red == this.red
  418. && color.green == this.green
  419. && color.blue == this.blue) {
  420. return true;
  421. }
  422. return false;
  423. }
  424. }