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.

PDFDeviceColorSpace.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  21. * Represents a device-specific color space. Used for mapping DeviceRGB, DeviceCMYK and DeviceGray.
  22. */
  23. public class PDFDeviceColorSpace implements PDFColorSpace {
  24. private int numComponents;
  25. /**
  26. * Unknown colorspace
  27. */
  28. public static final int DEVICE_UNKNOWN = -1;
  29. /**
  30. * Gray colorspace
  31. */
  32. public static final int DEVICE_GRAY = 1;
  33. /**
  34. * RGB colorspace
  35. */
  36. public static final int DEVICE_RGB = 2;
  37. /**
  38. * CMYK colorspace
  39. */
  40. public static final int DEVICE_CMYK = 3;
  41. // Are there any others?
  42. /**
  43. * Current color space value.
  44. */
  45. protected int currentColorSpace = DEVICE_UNKNOWN;
  46. /**
  47. * Create a PDF colorspace object.
  48. *
  49. * @param theColorSpace the current colorspace
  50. */
  51. public PDFDeviceColorSpace(int theColorSpace) {
  52. this.currentColorSpace = theColorSpace;
  53. numComponents = calculateNumComponents();
  54. }
  55. private int calculateNumComponents() {
  56. if (currentColorSpace == DEVICE_GRAY) {
  57. return 1;
  58. } else if (currentColorSpace == DEVICE_RGB) {
  59. return 3;
  60. } else if (currentColorSpace == DEVICE_CMYK) {
  61. return 4;
  62. } else {
  63. return 0;
  64. }
  65. }
  66. /**
  67. * Set the current colorspace.
  68. *
  69. * @param theColorSpace the new color space value
  70. */
  71. public void setColorSpace(int theColorSpace) {
  72. this.currentColorSpace = theColorSpace;
  73. numComponents = calculateNumComponents();
  74. }
  75. /**
  76. * Get the colorspace value
  77. *
  78. * @return the colorspace value
  79. */
  80. public int getColorSpace() {
  81. return (this.currentColorSpace);
  82. }
  83. /**
  84. * Get the number of color components for this colorspace
  85. *
  86. * @return the number of components
  87. */
  88. public int getNumComponents() {
  89. return numComponents;
  90. }
  91. /** @return the name of the color space */
  92. public String getName() {
  93. switch (currentColorSpace) {
  94. case DEVICE_CMYK:
  95. return "DeviceCMYK";
  96. case DEVICE_GRAY:
  97. return "DeviceGray";
  98. case DEVICE_RGB:
  99. return "DeviceRGB";
  100. default:
  101. throw new IllegalStateException("Unsupported color space in use.");
  102. }
  103. }
  104. /** {@inheritDoc} */
  105. public boolean isDeviceColorSpace() {
  106. return true;
  107. }
  108. /** {@inheritDoc} */
  109. public boolean isRGBColorSpace() {
  110. return getColorSpace() == DEVICE_RGB;
  111. }
  112. /** {@inheritDoc} */
  113. public boolean isCMYKColorSpace() {
  114. return getColorSpace() == DEVICE_CMYK;
  115. }
  116. /** {@inheritDoc} */
  117. public boolean isGrayColorSpace() {
  118. return getColorSpace() == DEVICE_GRAY;
  119. }
  120. /**
  121. * Returns a suitable {@link PDFDeviceColorSpace} object given a {@link ColorSpace} object.
  122. * @param cs ColorSpace instance
  123. * @return a PDF-based color space
  124. */
  125. public static PDFDeviceColorSpace toPDFColorSpace(ColorSpace cs) {
  126. if (cs == null) {
  127. return null;
  128. }
  129. PDFDeviceColorSpace pdfCS = new PDFDeviceColorSpace(0);
  130. switch (cs.getType()) {
  131. case ColorSpace.TYPE_CMYK:
  132. pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_CMYK);
  133. break;
  134. case ColorSpace.TYPE_GRAY:
  135. pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_GRAY);
  136. break;
  137. default:
  138. pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  139. }
  140. return pdfCS;
  141. }
  142. }