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.2KB

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