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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /**
  22. * Color helper class.
  23. * <p>
  24. * This class extends java.awt.Color class keeping track of the original color
  25. * property values specified by the fo user in a rgb-icc call.
  26. */
  27. public final class ColorExt extends Color {
  28. //
  29. private static final long serialVersionUID = 1L;
  30. // Values of fop-rgb-icc arguments
  31. private float rgbReplacementRed;
  32. private float rgbReplacementGreen;
  33. private float rgbReplacementBlue;
  34. private String iccProfileName;
  35. private String iccProfileSrc;
  36. private ColorSpace colorSpace;
  37. private float[] colorValues;
  38. /*
  39. * Helper for createFromFoRgbIcc
  40. */
  41. private ColorExt(ColorSpace colorSpace, float[] colorValues, float opacity) {
  42. super(colorSpace, colorValues, opacity);
  43. }
  44. /*
  45. * Helper for createFromSvgIccColor
  46. */
  47. private ColorExt(float red, float green, float blue, float opacity) {
  48. super(red, green, blue, opacity);
  49. }
  50. /**
  51. * Create ColorExt object backup up FO's rgb-icc color function
  52. *
  53. * @param redReplacement
  54. * Red part of RGB replacement color that will be used when ICC
  55. * profile can not be loaded
  56. * @param greenReplacement
  57. * Green part of RGB replacement color that will be used when ICC
  58. * profile can not be loaded
  59. * @param blueReplacement
  60. * Blue part of RGB replacement color that will be used when ICC
  61. * profile can not be loaded
  62. * @param profileName
  63. * Name of ICC profile
  64. * @param profileSrc
  65. * Source of ICC profile
  66. * @param colorSpace
  67. * ICC ColorSpace for the ICC profile
  68. * @param iccValues
  69. * color values
  70. * @return the requested color object
  71. */
  72. public static ColorExt createFromFoRgbIcc(float redReplacement,
  73. float greenReplacement, float blueReplacement, String profileName,
  74. String profileSrc, ColorSpace colorSpace, float[] iccValues) {
  75. ColorExt ce = new ColorExt(colorSpace, iccValues, 1.0f);
  76. ce.rgbReplacementRed = redReplacement;
  77. ce.rgbReplacementGreen = greenReplacement;
  78. ce.rgbReplacementBlue = blueReplacement;
  79. ce.iccProfileName = profileName;
  80. ce.iccProfileSrc = profileSrc;
  81. ce.colorSpace = colorSpace;
  82. ce.colorValues = iccValues;
  83. return ce;
  84. }
  85. /**
  86. * Create ColorExt object backing up SVG's icc-color function.
  87. *
  88. * @param red
  89. * Red value resulting from the conversion from the user provided
  90. * (icc) color values to the batik (rgb) color space
  91. * @param green
  92. * Green value resulting from the conversion from the user
  93. * provided (icc) color values to the batik (rgb) color space
  94. * @param blue
  95. * Blue value resulting from the conversion from the user
  96. * provided (icc) color values to the batik (rgb) color space
  97. * @param opacity
  98. * Opacity
  99. * @param profileName
  100. * ICC profile name
  101. * @param profileHref
  102. * the URI to the color profile
  103. * @param profileCS
  104. * ICC ColorSpace profile
  105. * @param colorValues
  106. * ICC color values
  107. * @return the requested color object
  108. */
  109. public static ColorExt createFromSvgIccColor(float red, float green,
  110. float blue, float opacity, String profileName, String profileHref,
  111. ColorSpace profileCS, float[] colorValues) {
  112. ColorExt ce = new ColorExt(red, green, blue, opacity);
  113. ce.rgbReplacementRed = -1;
  114. ce.rgbReplacementGreen = -1;
  115. ce.rgbReplacementBlue = -1;
  116. ce.iccProfileName = profileName;
  117. ce.iccProfileSrc = profileHref;
  118. ce.colorSpace = profileCS;
  119. ce.colorValues = colorValues;
  120. return ce;
  121. }
  122. /**
  123. * Get ICC profile name
  124. *
  125. * @return ICC profile name
  126. */
  127. public String getIccProfileName() {
  128. return this.iccProfileName;
  129. }
  130. /**
  131. * Get ICC profile source
  132. *
  133. * @return ICC profile source
  134. */
  135. public String getIccProfileSrc() {
  136. return this.iccProfileSrc;
  137. }
  138. /**
  139. * @return the original ColorSpace
  140. */
  141. public ColorSpace getOrigColorSpace() {
  142. return this.colorSpace;
  143. }
  144. /**
  145. * @return the original color values
  146. */
  147. public float[] getOriginalColorComponents() {
  148. return this.colorValues;
  149. }
  150. /**
  151. * Create string representation of fop-rgb-icc function call to map this
  152. * ColorExt settings
  153. * @return the string representing the internal fop-rgb-icc() function call
  154. */
  155. public String toFunctionCall() {
  156. StringBuffer sb = new StringBuffer(40);
  157. sb.append("fop-rgb-icc(");
  158. sb.append(this.rgbReplacementRed + ",");
  159. sb.append(this.rgbReplacementGreen + ",");
  160. sb.append(this.rgbReplacementBlue + ",");
  161. sb.append(this.iccProfileName + ",");
  162. sb.append("\"" + this.iccProfileSrc + "\"");
  163. float[] colorComponents = this.getColorComponents(null);
  164. for (int ix = 0; ix < colorComponents.length; ix++) {
  165. sb.append(",");
  166. sb.append(colorComponents[ix]);
  167. }
  168. sb.append(")");
  169. return sb.toString();
  170. }
  171. }