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.

ColorExt.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.util;
  19. import java.awt.Color;
  20. import java.awt.color.ColorSpace;
  21. import java.util.Arrays;
  22. /**
  23. * Color helper class.
  24. * <p>
  25. * This class extends java.awt.Color class keeping track of the original color
  26. * property values specified by the fo user in a rgb-icc call.
  27. * @deprecated Replaced by {@link org.apache.xmlgraphics.java2d.color.ColorWithAlternatives}
  28. */
  29. @Deprecated
  30. public final class ColorExt extends Color {
  31. //
  32. private static final long serialVersionUID = 1L;
  33. // Values of fop-rgb-icc arguments
  34. private float rgbReplacementRed;
  35. private float rgbReplacementGreen;
  36. private float rgbReplacementBlue;
  37. private String iccProfileName;
  38. private String iccProfileSrc;
  39. private ColorSpace colorSpace;
  40. private float[] colorValues;
  41. /*
  42. * Helper for createFromFoRgbIcc
  43. */
  44. private ColorExt(ColorSpace colorSpace, float[] colorValues, float opacity) {
  45. super(colorSpace, colorValues, opacity);
  46. }
  47. /*
  48. * Helper for createFromSvgIccColor
  49. */
  50. private ColorExt(float red, float green, float blue, float opacity) {
  51. super(red, green, blue, opacity);
  52. }
  53. /**
  54. * Create ColorExt object backup up FO's rgb-icc color function
  55. *
  56. * @param redReplacement
  57. * Red part of RGB replacement color that will be used when ICC
  58. * profile can not be loaded
  59. * @param greenReplacement
  60. * Green part of RGB replacement color that will be used when ICC
  61. * profile can not be loaded
  62. * @param blueReplacement
  63. * Blue part of RGB replacement color that will be used when ICC
  64. * profile can not be loaded
  65. * @param profileName
  66. * Name of ICC profile
  67. * @param profileSrc
  68. * Source of ICC profile
  69. * @param colorSpace
  70. * ICC ColorSpace for the ICC profile
  71. * @param iccValues
  72. * color values
  73. * @return the requested color object
  74. */
  75. public static ColorExt createFromFoRgbIcc(float redReplacement,
  76. float greenReplacement, float blueReplacement, String profileName,
  77. String profileSrc, ColorSpace colorSpace, float[] iccValues) {
  78. ColorExt ce = new ColorExt(colorSpace, iccValues, 1.0f);
  79. ce.rgbReplacementRed = redReplacement;
  80. ce.rgbReplacementGreen = greenReplacement;
  81. ce.rgbReplacementBlue = blueReplacement;
  82. ce.iccProfileName = profileName;
  83. ce.iccProfileSrc = profileSrc;
  84. ce.colorSpace = colorSpace;
  85. ce.colorValues = iccValues;
  86. return ce;
  87. }
  88. /**
  89. * Create ColorExt object backing up SVG's icc-color function.
  90. *
  91. * @param red
  92. * Red value resulting from the conversion from the user provided
  93. * (icc) color values to the batik (rgb) color space
  94. * @param green
  95. * Green value resulting from the conversion from the user
  96. * provided (icc) color values to the batik (rgb) color space
  97. * @param blue
  98. * Blue value resulting from the conversion from the user
  99. * provided (icc) color values to the batik (rgb) color space
  100. * @param opacity
  101. * Opacity
  102. * @param profileName
  103. * ICC profile name
  104. * @param profileHref
  105. * the URI to the color profile
  106. * @param profileCS
  107. * ICC ColorSpace profile
  108. * @param colorValues
  109. * ICC color values
  110. * @return the requested color object
  111. */
  112. public static ColorExt createFromSvgIccColor(float red, float green,
  113. float blue, float opacity, String profileName, String profileHref,
  114. ColorSpace profileCS, float[] colorValues) {
  115. //TODO this method is not referenced by FOP, can it be deleted?
  116. ColorExt ce = new ColorExt(red, green, blue, opacity);
  117. ce.rgbReplacementRed = -1;
  118. ce.rgbReplacementGreen = -1;
  119. ce.rgbReplacementBlue = -1;
  120. ce.iccProfileName = profileName;
  121. ce.iccProfileSrc = profileHref;
  122. ce.colorSpace = profileCS;
  123. ce.colorValues = colorValues;
  124. return ce;
  125. }
  126. /** {@inheritDoc} */
  127. @Override
  128. public int hashCode() {
  129. //implementation from the superclass should be good enough for our purposes
  130. return super.hashCode();
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public boolean equals(Object obj) {
  135. if (this == obj) {
  136. return true;
  137. }
  138. if (!super.equals(obj)) {
  139. return false;
  140. }
  141. if (getClass() != obj.getClass()) {
  142. return false;
  143. }
  144. ColorExt other = (ColorExt)obj;
  145. //TODO maybe use super.getColorComponents() instead
  146. if (!Arrays.equals(colorValues, other.colorValues)) {
  147. return false;
  148. }
  149. if (iccProfileName == null) {
  150. if (other.iccProfileName != null) {
  151. return false;
  152. }
  153. } else if (!iccProfileName.equals(other.iccProfileName)) {
  154. return false;
  155. }
  156. if (iccProfileSrc == null) {
  157. if (other.iccProfileSrc != null) {
  158. return false;
  159. }
  160. } else if (!iccProfileSrc.equals(other.iccProfileSrc)) {
  161. return false;
  162. }
  163. if (Float.floatToIntBits(rgbReplacementBlue)
  164. != Float.floatToIntBits(other.rgbReplacementBlue)) {
  165. return false;
  166. }
  167. if (Float.floatToIntBits(rgbReplacementGreen)
  168. != Float.floatToIntBits(other.rgbReplacementGreen)) {
  169. return false;
  170. }
  171. if (Float.floatToIntBits(rgbReplacementRed)
  172. != Float.floatToIntBits(other.rgbReplacementRed)) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * Get ICC profile name
  179. *
  180. * @return ICC profile name
  181. */
  182. public String getIccProfileName() {
  183. return this.iccProfileName;
  184. }
  185. /**
  186. * Get ICC profile source
  187. *
  188. * @return ICC profile source
  189. */
  190. public String getIccProfileSrc() {
  191. return this.iccProfileSrc;
  192. }
  193. /**
  194. * @return the original ColorSpace
  195. */
  196. public ColorSpace getOrigColorSpace() {
  197. //TODO this method is probably unnecessary due to super.cs and getColorSpace()
  198. return this.colorSpace;
  199. }
  200. /**
  201. * Returns the original color values.
  202. * @return the original color values
  203. */
  204. public float[] getOriginalColorComponents() {
  205. //TODO this method is probably unnecessary due to super.fvalue and getColorComponents()
  206. float[] copy = new float[this.colorValues.length];
  207. System.arraycopy(this.colorValues, 0, copy, 0, copy.length);
  208. return copy;
  209. }
  210. /**
  211. * Create string representation of fop-rgb-icc function call to map this
  212. * ColorExt settings
  213. * @return the string representing the internal fop-rgb-icc() function call
  214. */
  215. public String toFunctionCall() {
  216. StringBuffer sb = new StringBuffer(40);
  217. sb.append("fop-rgb-icc(");
  218. sb.append(this.rgbReplacementRed + ",");
  219. sb.append(this.rgbReplacementGreen + ",");
  220. sb.append(this.rgbReplacementBlue + ",");
  221. sb.append(this.iccProfileName + ",");
  222. if (this.iccProfileSrc != null) {
  223. sb.append("\"" + this.iccProfileSrc + "\"");
  224. }
  225. float[] colorComponents = this.getColorComponents(null);
  226. for (float colorComponent : colorComponents) {
  227. sb.append(",");
  228. sb.append(colorComponent);
  229. }
  230. sb.append(")");
  231. return sb.toString();
  232. }
  233. }