Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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