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.

ColorUtil.java 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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.awt.color.ICC_ColorSpace;
  22. import java.awt.color.ICC_Profile;
  23. import java.io.IOException;
  24. import java.util.Collections;
  25. import java.util.Map;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.xmlgraphics.java2d.color.CIELabColorSpace;
  29. import org.apache.xmlgraphics.java2d.color.ColorSpaceOrigin;
  30. import org.apache.xmlgraphics.java2d.color.ColorSpaces;
  31. import org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
  32. import org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace;
  33. import org.apache.xmlgraphics.java2d.color.NamedColorSpace;
  34. import org.apache.xmlgraphics.java2d.color.RenderingIntent;
  35. import org.apache.xmlgraphics.java2d.color.profile.NamedColorProfile;
  36. import org.apache.xmlgraphics.java2d.color.profile.NamedColorProfileParser;
  37. import org.apache.fop.apps.FOUserAgent;
  38. import org.apache.fop.fo.expr.PropertyException;
  39. /**
  40. * Generic Color helper class.
  41. * <p>
  42. * This class supports parsing string values into color values and creating
  43. * color values for strings. It provides a list of standard color names.
  44. */
  45. public final class ColorUtil {
  46. //ColorWithFallback is used to preserve the sRGB fallback exclusively for the purpose
  47. //of regenerating textual color functions as specified in XSL-FO.
  48. /** The name for the uncalibrated CMYK pseudo-profile */
  49. public static final String CMYK_PSEUDO_PROFILE = "#CMYK";
  50. /** The name for the Separation pseudo-profile used for spot colors */
  51. public static final String SEPARATION_PSEUDO_PROFILE = "#Separation";
  52. /**
  53. * Keeps all the predefined and parsed colors.
  54. * <p>
  55. * This map is used to predefine given colors, as well as speeding up
  56. * parsing of already parsed colors.
  57. * <p>
  58. * Important: The use of this color map assumes that all Color instances are immutable!
  59. */
  60. private static Map<String, Color> colorMap = null;
  61. /** Logger instance */
  62. protected static final Log log = LogFactory.getLog(ColorUtil.class);
  63. static {
  64. initializeColorMap();
  65. }
  66. /**
  67. * Private constructor since this is an utility class.
  68. */
  69. private ColorUtil() {
  70. }
  71. /**
  72. * Creates a color from a given string.
  73. * <p>
  74. * This function supports a wide variety of inputs.
  75. * <ul>
  76. * <li>#RGB (hex 0..f)</li>
  77. * <li>#RGBA (hex 0..f)</li>
  78. * <li>#RRGGBB (hex 00..ff)</li>
  79. * <li>#RRGGBBAA (hex 00..ff)</li>
  80. * <li>rgb(r,g,b) (0..255 or 0%..100%)</li>
  81. * <li>java.awt.Color[r=r,g=g,b=b] (0..255)</li>
  82. * <li>system-color(colorname)</li>
  83. * <li>transparent</li>
  84. * <li>colorname</li>
  85. * <li>fop-rgb-icc(r,g,b,cs,cs-src,[num]+) (r/g/b: 0..1, num: 0..1)</li>
  86. * <li>cmyk(c,m,y,k) (0..1)</li>
  87. * </ul>
  88. *
  89. * @param foUserAgent FOUserAgent object
  90. * @param value
  91. * the string to parse.
  92. * @return a Color representing the string if possible
  93. * @throws PropertyException
  94. * if the string is not parsable or does not follow any of the
  95. * given formats.
  96. */
  97. public static Color parseColorString(FOUserAgent foUserAgent, String value)
  98. throws PropertyException {
  99. if (value == null) {
  100. return null;
  101. }
  102. Color parsedColor = colorMap.get(value.toLowerCase());
  103. if (parsedColor == null) {
  104. if (value.startsWith("#")) {
  105. parsedColor = parseWithHash(value);
  106. } else if (value.startsWith("rgb(")) {
  107. parsedColor = parseAsRGB(value);
  108. } else if (value.startsWith("url(")) {
  109. throw new PropertyException(
  110. "Colors starting with url( are not yet supported!");
  111. } else if (value.startsWith("java.awt.Color")) {
  112. parsedColor = parseAsJavaAWTColor(value);
  113. } else if (value.startsWith("system-color(")) {
  114. parsedColor = parseAsSystemColor(value);
  115. } else if (value.startsWith("fop-rgb-icc")) {
  116. parsedColor = parseAsFopRgbIcc(foUserAgent, value);
  117. } else if (value.startsWith("fop-rgb-named-color")) {
  118. parsedColor = parseAsFopRgbNamedColor(foUserAgent, value);
  119. } else if (value.startsWith("cie-lab-color")) {
  120. parsedColor = parseAsCIELabColor(foUserAgent, value);
  121. } else if (value.startsWith("cmyk")) {
  122. parsedColor = parseAsCMYK(value);
  123. }
  124. if (parsedColor == null) {
  125. throw new PropertyException("Unknown Color: " + value);
  126. }
  127. colorMap.put(value, parsedColor);
  128. }
  129. return parsedColor;
  130. }
  131. /**
  132. * Tries to parse a color given with the system-color() function.
  133. *
  134. * @param value
  135. * the complete line
  136. * @return a color if possible
  137. * @throws PropertyException
  138. * if the format is wrong.
  139. */
  140. private static Color parseAsSystemColor(String value)
  141. throws PropertyException {
  142. int poss = value.indexOf("(");
  143. int pose = value.indexOf(")");
  144. if (poss != -1 && pose != -1) {
  145. value = value.substring(poss + 1, pose);
  146. } else {
  147. throw new PropertyException("Unknown color format: " + value
  148. + ". Must be system-color(x)");
  149. }
  150. return colorMap.get(value);
  151. }
  152. /**
  153. * Tries to parse the standard java.awt.Color toString output.
  154. *
  155. * @param value
  156. * the complete line
  157. * @return a color if possible
  158. * @throws PropertyException
  159. * if the format is wrong.
  160. * @see java.awt.Color#toString()
  161. */
  162. private static Color parseAsJavaAWTColor(String value)
  163. throws PropertyException {
  164. float red = 0.0f;
  165. float green = 0.0f;
  166. float blue = 0.0f;
  167. int poss = value.indexOf("[");
  168. int pose = value.indexOf("]");
  169. try {
  170. if (poss != -1 && pose != -1) {
  171. value = value.substring(poss + 1, pose);
  172. String[] args = value.split(",");
  173. if (args.length != 3) {
  174. throw new PropertyException(
  175. "Invalid number of arguments for a java.awt.Color: " + value);
  176. }
  177. red = Float.parseFloat(args[0].trim().substring(2)) / 255f;
  178. green = Float.parseFloat(args[1].trim().substring(2)) / 255f;
  179. blue = Float.parseFloat(args[2].trim().substring(2)) / 255f;
  180. if ((red < 0.0 || red > 1.0)
  181. || (green < 0.0 || green > 1.0)
  182. || (blue < 0.0 || blue > 1.0)) {
  183. throw new PropertyException("Color values out of range");
  184. }
  185. } else {
  186. throw new IllegalArgumentException(
  187. "Invalid format for a java.awt.Color: " + value);
  188. }
  189. } catch (RuntimeException re) {
  190. throw new PropertyException(re);
  191. }
  192. return new Color(red, green, blue);
  193. }
  194. /**
  195. * Parse a color given with the rgb() function.
  196. *
  197. * @param value
  198. * the complete line
  199. * @return a color if possible
  200. * @throws PropertyException
  201. * if the format is wrong.
  202. */
  203. private static Color parseAsRGB(String value) throws PropertyException {
  204. Color parsedColor;
  205. int poss = value.indexOf("(");
  206. int pose = value.indexOf(")");
  207. if (poss != -1 && pose != -1) {
  208. value = value.substring(poss + 1, pose);
  209. try {
  210. String[] args = value.split(",");
  211. if (args.length != 3) {
  212. throw new PropertyException(
  213. "Invalid number of arguments: rgb(" + value + ")");
  214. }
  215. float red = parseComponent255(args[0], value);
  216. float green = parseComponent255(args[1], value);
  217. float blue = parseComponent255(args[2], value);
  218. //Convert to ints to synchronize the behaviour with toRGBFunctionCall()
  219. int r = (int)(red * 255 + 0.5);
  220. int g = (int)(green * 255 + 0.5);
  221. int b = (int)(blue * 255 + 0.5);
  222. parsedColor = new Color(r, g, b);
  223. } catch (RuntimeException re) {
  224. //wrap in a PropertyException
  225. throw new PropertyException(re);
  226. }
  227. } else {
  228. throw new PropertyException("Unknown color format: " + value
  229. + ". Must be rgb(r,g,b)");
  230. }
  231. return parsedColor;
  232. }
  233. private static float parseComponent255(String str, String function)
  234. throws PropertyException {
  235. float component;
  236. str = str.trim();
  237. if (str.endsWith("%")) {
  238. component = Float.parseFloat(str.substring(0,
  239. str.length() - 1)) / 100f;
  240. } else {
  241. component = Float.parseFloat(str) / 255f;
  242. }
  243. if ((component < 0.0 || component > 1.0)) {
  244. throw new PropertyException("Color value out of range for " + function + ": "
  245. + str + ". Valid range: [0..255] or [0%..100%]");
  246. }
  247. return component;
  248. }
  249. private static float parseComponent1(String argument, String function)
  250. throws PropertyException {
  251. return parseComponent(argument, 0f, 1f, function);
  252. }
  253. private static float parseComponent(String argument, float min, float max, String function)
  254. throws PropertyException {
  255. float component = Float.parseFloat(argument.trim());
  256. if ((component < min || component > max)) {
  257. throw new PropertyException("Color value out of range for " + function + ": "
  258. + argument + ". Valid range: [" + min + ".." + max + "]");
  259. }
  260. return component;
  261. }
  262. private static Color parseFallback(String[] args, String value) throws PropertyException {
  263. float red = parseComponent1(args[0], value);
  264. float green = parseComponent1(args[1], value);
  265. float blue = parseComponent1(args[2], value);
  266. //Sun's classlib rounds differently with this constructor than when converting to sRGB
  267. //via CIE XYZ.
  268. Color sRGB = new Color(red, green, blue);
  269. return sRGB;
  270. }
  271. /**
  272. * Parse a color given in the #.... format.
  273. *
  274. * @param value
  275. * the complete line
  276. * @return a color if possible
  277. * @throws PropertyException
  278. * if the format is wrong.
  279. */
  280. private static Color parseWithHash(String value) throws PropertyException {
  281. Color parsedColor;
  282. try {
  283. int len = value.length();
  284. int alpha;
  285. if (len == 5 || len == 9) {
  286. alpha = Integer.parseInt(
  287. value.substring((len == 5) ? 3 : 7), 16);
  288. } else {
  289. alpha = 0xFF;
  290. }
  291. int red = 0;
  292. int green = 0;
  293. int blue = 0;
  294. if ((len == 4) || (len == 5)) {
  295. //multiply by 0x11 = 17 = 255/15
  296. red = Integer.parseInt(value.substring(1, 2), 16) * 0x11;
  297. green = Integer.parseInt(value.substring(2, 3), 16) * 0x11;
  298. blue = Integer.parseInt(value.substring(3, 4), 16) * 0X11;
  299. } else if ((len == 7) || (len == 9)) {
  300. red = Integer.parseInt(value.substring(1, 3), 16);
  301. green = Integer.parseInt(value.substring(3, 5), 16);
  302. blue = Integer.parseInt(value.substring(5, 7), 16);
  303. } else {
  304. throw new NumberFormatException();
  305. }
  306. parsedColor = new Color(red, green, blue, alpha);
  307. } catch (RuntimeException re) {
  308. throw new PropertyException("Unknown color format: " + value
  309. + ". Must be #RGB. #RGBA, #RRGGBB, or #RRGGBBAA");
  310. }
  311. return parsedColor;
  312. }
  313. /**
  314. * Parse a color specified using the fop-rgb-icc() function.
  315. *
  316. * @param value the function call
  317. * @return a color if possible
  318. * @throws PropertyException if the format is wrong.
  319. */
  320. private static Color parseAsFopRgbIcc(FOUserAgent foUserAgent, String value)
  321. throws PropertyException {
  322. Color parsedColor;
  323. int poss = value.indexOf("(");
  324. int pose = value.indexOf(")");
  325. if (poss != -1 && pose != -1) {
  326. String[] args = value.substring(poss + 1, pose).split(",");
  327. try {
  328. if (args.length < 5) {
  329. throw new PropertyException("Too few arguments for rgb-icc() function");
  330. }
  331. //Set up fallback sRGB value
  332. Color sRGB = parseFallback(args, value);
  333. /* Get and verify ICC profile name */
  334. String iccProfileName = args[3].trim();
  335. if (iccProfileName == null || "".equals(iccProfileName)) {
  336. throw new PropertyException("ICC profile name missing");
  337. }
  338. ColorSpace colorSpace = null;
  339. String iccProfileSrc = null;
  340. if (isPseudoProfile(iccProfileName)) {
  341. if (CMYK_PSEUDO_PROFILE.equalsIgnoreCase(iccProfileName)) {
  342. colorSpace = ColorSpaces.getDeviceCMYKColorSpace();
  343. } else if (SEPARATION_PSEUDO_PROFILE.equalsIgnoreCase(iccProfileName)) {
  344. colorSpace = new NamedColorSpace(args[5], sRGB,
  345. SEPARATION_PSEUDO_PROFILE, null);
  346. } else {
  347. assert false : "Incomplete implementation";
  348. }
  349. } else {
  350. /* Get and verify ICC profile source */
  351. iccProfileSrc = args[4].trim();
  352. if (iccProfileSrc == null || "".equals(iccProfileSrc)) {
  353. throw new PropertyException("ICC profile source missing");
  354. }
  355. iccProfileSrc = unescapeString(iccProfileSrc);
  356. }
  357. /* ICC profile arguments */
  358. int componentStart = 4;
  359. if (colorSpace instanceof NamedColorSpace) {
  360. componentStart++;
  361. }
  362. float[] iccComponents = new float[args.length - componentStart - 1];
  363. for (int ix = componentStart; ++ix < args.length;) {
  364. iccComponents[ix - componentStart - 1] = Float.parseFloat(args[ix].trim());
  365. }
  366. if (colorSpace instanceof NamedColorSpace && iccComponents.length == 0) {
  367. iccComponents = new float[] {1.0f}; //full tint if not specified
  368. }
  369. /* Ask FOP factory to get ColorSpace for the specified ICC profile source */
  370. if (foUserAgent != null && iccProfileSrc != null) {
  371. RenderingIntent renderingIntent = RenderingIntent.AUTO;
  372. //TODO connect to fo:color-profile/@rendering-intent
  373. colorSpace = foUserAgent.getColorSpaceCache().get(
  374. iccProfileName, iccProfileSrc, renderingIntent);
  375. }
  376. if (colorSpace != null) {
  377. // ColorSpace is available
  378. if (ColorSpaces.isDeviceColorSpace(colorSpace)) {
  379. //Device-specific colors are handled differently:
  380. //sRGB is the primary color with the CMYK as the alternative
  381. Color deviceColor = new Color(colorSpace, iccComponents, 1.0f);
  382. float[] rgbComps = sRGB.getRGBColorComponents(null);
  383. parsedColor = new ColorWithAlternatives(
  384. rgbComps[0], rgbComps[1], rgbComps[2],
  385. new Color[] {deviceColor});
  386. } else {
  387. parsedColor = new ColorWithFallback(
  388. colorSpace, iccComponents, 1.0f, null, sRGB);
  389. }
  390. } else {
  391. // ICC profile could not be loaded - use rgb replacement values */
  392. log.warn("Color profile '" + iccProfileSrc
  393. + "' not found. Using sRGB replacement values.");
  394. parsedColor = sRGB;
  395. }
  396. } catch (RuntimeException re) {
  397. throw new PropertyException(re);
  398. }
  399. } else {
  400. throw new PropertyException("Unknown color format: " + value
  401. + ". Must be fop-rgb-icc(r,g,b,NCNAME,src,....)");
  402. }
  403. return parsedColor;
  404. }
  405. /**
  406. * Parse a color specified using the fop-rgb-named-color() function.
  407. *
  408. * @param value the function call
  409. * @return a color if possible
  410. * @throws PropertyException if the format is wrong.
  411. */
  412. private static Color parseAsFopRgbNamedColor(FOUserAgent foUserAgent, String value)
  413. throws PropertyException {
  414. Color parsedColor;
  415. int poss = value.indexOf("(");
  416. int pose = value.indexOf(")");
  417. if (poss != -1 && pose != -1) {
  418. String[] args = value.substring(poss + 1, pose).split(",");
  419. try {
  420. if (args.length != 6) {
  421. throw new PropertyException("rgb-named-color() function must have 6 arguments");
  422. }
  423. //Set up fallback sRGB value
  424. Color sRGB = parseFallback(args, value);
  425. /* Get and verify ICC profile name */
  426. String iccProfileName = args[3].trim();
  427. if (iccProfileName == null || "".equals(iccProfileName)) {
  428. throw new PropertyException("ICC profile name missing");
  429. }
  430. ICC_ColorSpace colorSpace = null;
  431. String iccProfileSrc;
  432. if (isPseudoProfile(iccProfileName)) {
  433. throw new IllegalArgumentException(
  434. "Pseudo-profiles are not allowed with fop-rgb-named-color()");
  435. } else {
  436. /* Get and verify ICC profile source */
  437. iccProfileSrc = args[4].trim();
  438. if (iccProfileSrc == null || "".equals(iccProfileSrc)) {
  439. throw new PropertyException("ICC profile source missing");
  440. }
  441. iccProfileSrc = unescapeString(iccProfileSrc);
  442. }
  443. // color name
  444. String colorName = unescapeString(args[5].trim());
  445. /* Ask FOP factory to get ColorSpace for the specified ICC profile source */
  446. if (foUserAgent != null && iccProfileSrc != null) {
  447. RenderingIntent renderingIntent = RenderingIntent.AUTO;
  448. //TODO connect to fo:color-profile/@rendering-intent
  449. colorSpace = (ICC_ColorSpace)foUserAgent.getColorSpaceCache().get(
  450. iccProfileName, iccProfileSrc, renderingIntent);
  451. }
  452. if (colorSpace != null) {
  453. ICC_Profile profile = colorSpace.getProfile();
  454. if (NamedColorProfileParser.isNamedColorProfile(profile)) {
  455. NamedColorProfileParser parser = new NamedColorProfileParser();
  456. NamedColorProfile ncp = parser.parseProfile(profile,
  457. iccProfileName, iccProfileSrc);
  458. NamedColorSpace ncs = ncp.getNamedColor(colorName);
  459. if (ncs != null) {
  460. parsedColor = new ColorWithFallback(ncs,
  461. new float[] {1.0f}, 1.0f, null, sRGB);
  462. } else {
  463. log.warn("Color '" + colorName
  464. + "' does not exist in named color profile: " + iccProfileSrc);
  465. parsedColor = sRGB;
  466. }
  467. } else {
  468. log.warn("ICC profile is no named color profile: " + iccProfileSrc);
  469. parsedColor = sRGB;
  470. }
  471. } else {
  472. // ICC profile could not be loaded - use rgb replacement values */
  473. log.warn("Color profile '" + iccProfileSrc
  474. + "' not found. Using sRGB replacement values.");
  475. parsedColor = sRGB;
  476. }
  477. } catch (IOException ioe) {
  478. //wrap in a PropertyException
  479. throw new PropertyException(ioe);
  480. } catch (RuntimeException re) {
  481. throw new PropertyException(re);
  482. //wrap in a PropertyException
  483. }
  484. } else {
  485. throw new PropertyException("Unknown color format: " + value
  486. + ". Must be fop-rgb-named-color(r,g,b,NCNAME,src,color-name)");
  487. }
  488. return parsedColor;
  489. }
  490. /**
  491. * Parse a color specified using the cie-lab-color() function.
  492. *
  493. * @param value the function call
  494. * @return a color if possible
  495. * @throws PropertyException if the format is wrong.
  496. */
  497. private static Color parseAsCIELabColor(FOUserAgent foUserAgent, String value)
  498. throws PropertyException {
  499. Color parsedColor;
  500. int poss = value.indexOf("(");
  501. int pose = value.indexOf(")");
  502. if (poss != -1 && pose != -1) {
  503. try {
  504. String[] args = value.substring(poss + 1, pose).split(",");
  505. if (args.length != 6) {
  506. throw new PropertyException("cie-lab-color() function must have 6 arguments");
  507. }
  508. //Set up fallback sRGB value
  509. float red = parseComponent255(args[0], value);
  510. float green = parseComponent255(args[1], value);
  511. float blue = parseComponent255(args[2], value);
  512. Color sRGB = new Color(red, green, blue);
  513. float l = parseComponent(args[3], 0f, 100f, value);
  514. float a = parseComponent(args[4], -127f, 127f, value);
  515. float b = parseComponent(args[5], -127f, 127f, value);
  516. //Assuming the XSL-FO spec uses the D50 white point
  517. CIELabColorSpace cs = ColorSpaces.getCIELabColorSpaceD50();
  518. //use toColor() to have components normalized
  519. Color labColor = cs.toColor(l, a, b, 1.0f);
  520. //Convert to ColorWithFallback
  521. parsedColor = new ColorWithFallback(labColor, sRGB);
  522. } catch (RuntimeException re) {
  523. throw new PropertyException(re);
  524. }
  525. } else {
  526. throw new PropertyException("Unknown color format: " + value
  527. + ". Must be cie-lab-color(r,g,b,Lightness,a-value,b-value)");
  528. }
  529. return parsedColor;
  530. }
  531. private static String unescapeString(String iccProfileSrc) {
  532. if (iccProfileSrc.startsWith("\"") || iccProfileSrc.startsWith("'")) {
  533. iccProfileSrc = iccProfileSrc.substring(1);
  534. }
  535. if (iccProfileSrc.endsWith("\"") || iccProfileSrc.endsWith("'")) {
  536. iccProfileSrc = iccProfileSrc.substring(0, iccProfileSrc.length() - 1);
  537. }
  538. return iccProfileSrc;
  539. }
  540. /**
  541. * Parse a color given with the cmyk() function.
  542. *
  543. * @param value
  544. * the complete line
  545. * @return a color if possible
  546. * @throws PropertyException
  547. * if the format is wrong.
  548. */
  549. private static Color parseAsCMYK(String value) throws PropertyException {
  550. Color parsedColor;
  551. int poss = value.indexOf("(");
  552. int pose = value.indexOf(")");
  553. if (poss != -1 && pose != -1) {
  554. value = value.substring(poss + 1, pose);
  555. String[] args = value.split(",");
  556. try {
  557. if (args.length != 4) {
  558. throw new PropertyException(
  559. "Invalid number of arguments: cmyk(" + value + ")");
  560. }
  561. float cyan = parseComponent1(args[0], value);
  562. float magenta = parseComponent1(args[1], value);
  563. float yellow = parseComponent1(args[2], value);
  564. float black = parseComponent1(args[3], value);
  565. float[] comps = new float[] {cyan, magenta, yellow, black};
  566. Color cmykColor = DeviceCMYKColorSpace.createCMYKColor(comps);
  567. float[] rgbComps = cmykColor.getRGBColorComponents(null);
  568. parsedColor = new ColorWithAlternatives(rgbComps[0], rgbComps[1], rgbComps[2],
  569. new Color[] {cmykColor});
  570. } catch (RuntimeException re) {
  571. throw new PropertyException(re);
  572. }
  573. } else {
  574. throw new PropertyException("Unknown color format: " + value
  575. + ". Must be cmyk(c,m,y,k)");
  576. }
  577. return parsedColor;
  578. }
  579. /**
  580. * Creates a re-parsable string representation of the given color.
  581. * <p>
  582. * First, the color will be converted into the sRGB colorspace. It will then
  583. * be printed as #rrggbb, or as #rrrggbbaa if an alpha value is present.
  584. *
  585. * @param color
  586. * the color to represent.
  587. * @return a re-parsable string representadion.
  588. */
  589. public static String colorToString(Color color) {
  590. ColorSpace cs = color.getColorSpace();
  591. if (color instanceof ColorWithAlternatives) {
  592. return toFunctionCall((ColorWithAlternatives)color);
  593. } else if (cs != null && cs.getType() == ColorSpace.TYPE_CMYK) {
  594. StringBuffer sbuf = new StringBuffer(24);
  595. float[] cmyk = color.getColorComponents(null);
  596. sbuf.append("cmyk(").append(cmyk[0])
  597. .append(",").append(cmyk[1])
  598. .append(",").append(cmyk[2])
  599. .append(",").append(cmyk[3]).append(")");
  600. return sbuf.toString();
  601. } else {
  602. return toRGBFunctionCall(color);
  603. }
  604. }
  605. private static String toRGBFunctionCall(Color color) {
  606. StringBuffer sbuf = new StringBuffer();
  607. sbuf.append('#');
  608. String s = Integer.toHexString(color.getRed());
  609. if (s.length() == 1) {
  610. sbuf.append('0');
  611. }
  612. sbuf.append(s);
  613. s = Integer.toHexString(color.getGreen());
  614. if (s.length() == 1) {
  615. sbuf.append('0');
  616. }
  617. sbuf.append(s);
  618. s = Integer.toHexString(color.getBlue());
  619. if (s.length() == 1) {
  620. sbuf.append('0');
  621. }
  622. sbuf.append(s);
  623. if (color.getAlpha() != 255) {
  624. s = Integer.toHexString(color.getAlpha());
  625. if (s.length() == 1) {
  626. sbuf.append('0');
  627. }
  628. sbuf.append(s);
  629. }
  630. return sbuf.toString();
  631. }
  632. private static Color getsRGBFallback(ColorWithAlternatives color) {
  633. Color fallbackColor;
  634. if (color instanceof ColorWithFallback) {
  635. fallbackColor = ((ColorWithFallback)color).getFallbackColor();
  636. if (!fallbackColor.getColorSpace().isCS_sRGB()) {
  637. fallbackColor = toSRGBColor(fallbackColor);
  638. }
  639. } else {
  640. fallbackColor = toSRGBColor(color);
  641. }
  642. return fallbackColor;
  643. }
  644. private static Color toSRGBColor(Color color) {
  645. float[] comps;
  646. ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  647. comps = color.getRGBColorComponents(null);
  648. float[] allComps = color.getComponents(null);
  649. float alpha = allComps[allComps.length - 1]; //Alpha is on last component
  650. return new Color(sRGB, comps, alpha);
  651. }
  652. /**
  653. * Create string representation of a fop-rgb-icc (or fop-rgb-named-color) function call from
  654. * the given color.
  655. * @param color the color to turn into a function call
  656. * @return the string representing the internal fop-rgb-icc() or fop-rgb-named-color()
  657. * function call
  658. */
  659. private static String toFunctionCall(ColorWithAlternatives color) {
  660. ColorSpace cs = color.getColorSpace();
  661. if (cs.isCS_sRGB() && !color.hasAlternativeColors()) {
  662. return toRGBFunctionCall(color);
  663. }
  664. if (cs instanceof CIELabColorSpace) {
  665. return toCIELabFunctionCall(color);
  666. }
  667. Color specColor = color;
  668. if (color.hasAlternativeColors()) {
  669. Color alt = color.getAlternativeColors()[0];
  670. if (ColorSpaces.isDeviceColorSpace(alt.getColorSpace())) {
  671. cs = alt.getColorSpace();
  672. specColor = alt;
  673. }
  674. }
  675. ColorSpaceOrigin origin = ColorSpaces.getColorSpaceOrigin(cs);
  676. String functionName;
  677. Color fallbackColor = getsRGBFallback(color);
  678. float[] rgb = fallbackColor.getColorComponents(null);
  679. assert rgb.length == 3;
  680. StringBuffer sb = new StringBuffer(40);
  681. sb.append("(");
  682. sb.append(rgb[0]).append(",");
  683. sb.append(rgb[1]).append(",");
  684. sb.append(rgb[2]).append(",");
  685. String profileName = origin.getProfileName();
  686. sb.append(profileName).append(",");
  687. if (origin.getProfileURI() != null) {
  688. sb.append("\"").append(origin.getProfileURI()).append("\"");
  689. }
  690. if (cs instanceof NamedColorSpace) {
  691. NamedColorSpace ncs = (NamedColorSpace)cs;
  692. if (SEPARATION_PSEUDO_PROFILE.equalsIgnoreCase(profileName)) {
  693. functionName = "fop-rgb-icc";
  694. } else {
  695. functionName = "fop-rgb-named-color";
  696. }
  697. sb.append(",").append(ncs.getColorName());
  698. } else {
  699. functionName = "fop-rgb-icc";
  700. float[] colorComponents = specColor.getColorComponents(null);
  701. for (float colorComponent : colorComponents) {
  702. sb.append(",");
  703. sb.append(colorComponent);
  704. }
  705. }
  706. sb.append(")");
  707. return functionName + sb.toString();
  708. }
  709. private static String toCIELabFunctionCall(ColorWithAlternatives color) {
  710. Color fallbackColor = getsRGBFallback(color);
  711. StringBuffer sb = new StringBuffer("cie-lab-color(");
  712. sb.append(fallbackColor.getRed()).append(',');
  713. sb.append(fallbackColor.getGreen()).append(',');
  714. sb.append(fallbackColor.getBlue());
  715. CIELabColorSpace cs = (CIELabColorSpace)color.getColorSpace();
  716. float[] lab = cs.toNativeComponents(color.getColorComponents(null));
  717. for (int i = 0; i < 3; i++) {
  718. sb.append(',').append(lab[i]);
  719. }
  720. sb.append(')');
  721. return sb.toString();
  722. }
  723. private static Color createColor(int r, int g, int b) {
  724. return new Color(r, g, b);
  725. }
  726. /**
  727. * Initializes the colorMap with some predefined values.
  728. */
  729. private static void initializeColorMap() { // CSOK: MethodLength
  730. colorMap = Collections.synchronizedMap(new java.util.HashMap<String, Color>());
  731. colorMap.put("aliceblue", createColor(240, 248, 255));
  732. colorMap.put("antiquewhite", createColor(250, 235, 215));
  733. colorMap.put("aqua", createColor(0, 255, 255));
  734. colorMap.put("aquamarine", createColor(127, 255, 212));
  735. colorMap.put("azure", createColor(240, 255, 255));
  736. colorMap.put("beige", createColor(245, 245, 220));
  737. colorMap.put("bisque", createColor(255, 228, 196));
  738. colorMap.put("black", createColor(0, 0, 0));
  739. colorMap.put("blanchedalmond", createColor(255, 235, 205));
  740. colorMap.put("blue", createColor(0, 0, 255));
  741. colorMap.put("blueviolet", createColor(138, 43, 226));
  742. colorMap.put("brown", createColor(165, 42, 42));
  743. colorMap.put("burlywood", createColor(222, 184, 135));
  744. colorMap.put("cadetblue", createColor(95, 158, 160));
  745. colorMap.put("chartreuse", createColor(127, 255, 0));
  746. colorMap.put("chocolate", createColor(210, 105, 30));
  747. colorMap.put("coral", createColor(255, 127, 80));
  748. colorMap.put("cornflowerblue", createColor(100, 149, 237));
  749. colorMap.put("cornsilk", createColor(255, 248, 220));
  750. colorMap.put("crimson", createColor(220, 20, 60));
  751. colorMap.put("cyan", createColor(0, 255, 255));
  752. colorMap.put("darkblue", createColor(0, 0, 139));
  753. colorMap.put("darkcyan", createColor(0, 139, 139));
  754. colorMap.put("darkgoldenrod", createColor(184, 134, 11));
  755. colorMap.put("darkgray", createColor(169, 169, 169));
  756. colorMap.put("darkgreen", createColor(0, 100, 0));
  757. colorMap.put("darkgrey", createColor(169, 169, 169));
  758. colorMap.put("darkkhaki", createColor(189, 183, 107));
  759. colorMap.put("darkmagenta", createColor(139, 0, 139));
  760. colorMap.put("darkolivegreen", createColor(85, 107, 47));
  761. colorMap.put("darkorange", createColor(255, 140, 0));
  762. colorMap.put("darkorchid", createColor(153, 50, 204));
  763. colorMap.put("darkred", createColor(139, 0, 0));
  764. colorMap.put("darksalmon", createColor(233, 150, 122));
  765. colorMap.put("darkseagreen", createColor(143, 188, 143));
  766. colorMap.put("darkslateblue", createColor(72, 61, 139));
  767. colorMap.put("darkslategray", createColor(47, 79, 79));
  768. colorMap.put("darkslategrey", createColor(47, 79, 79));
  769. colorMap.put("darkturquoise", createColor(0, 206, 209));
  770. colorMap.put("darkviolet", createColor(148, 0, 211));
  771. colorMap.put("deeppink", createColor(255, 20, 147));
  772. colorMap.put("deepskyblue", createColor(0, 191, 255));
  773. colorMap.put("dimgray", createColor(105, 105, 105));
  774. colorMap.put("dimgrey", createColor(105, 105, 105));
  775. colorMap.put("dodgerblue", createColor(30, 144, 255));
  776. colorMap.put("firebrick", createColor(178, 34, 34));
  777. colorMap.put("floralwhite", createColor(255, 250, 240));
  778. colorMap.put("forestgreen", createColor(34, 139, 34));
  779. colorMap.put("fuchsia", createColor(255, 0, 255));
  780. colorMap.put("gainsboro", createColor(220, 220, 220));
  781. colorMap.put("ghostwhite", createColor(248, 248, 255));
  782. colorMap.put("gold", createColor(255, 215, 0));
  783. colorMap.put("goldenrod", createColor(218, 165, 32));
  784. colorMap.put("gray", createColor(128, 128, 128));
  785. colorMap.put("green", createColor(0, 128, 0));
  786. colorMap.put("greenyellow", createColor(173, 255, 47));
  787. colorMap.put("grey", createColor(128, 128, 128));
  788. colorMap.put("honeydew", createColor(240, 255, 240));
  789. colorMap.put("hotpink", createColor(255, 105, 180));
  790. colorMap.put("indianred", createColor(205, 92, 92));
  791. colorMap.put("indigo", createColor(75, 0, 130));
  792. colorMap.put("ivory", createColor(255, 255, 240));
  793. colorMap.put("khaki", createColor(240, 230, 140));
  794. colorMap.put("lavender", createColor(230, 230, 250));
  795. colorMap.put("lavenderblush", createColor(255, 240, 245));
  796. colorMap.put("lawngreen", createColor(124, 252, 0));
  797. colorMap.put("lemonchiffon", createColor(255, 250, 205));
  798. colorMap.put("lightblue", createColor(173, 216, 230));
  799. colorMap.put("lightcoral", createColor(240, 128, 128));
  800. colorMap.put("lightcyan", createColor(224, 255, 255));
  801. colorMap.put("lightgoldenrodyellow", createColor(250, 250, 210));
  802. colorMap.put("lightgray", createColor(211, 211, 211));
  803. colorMap.put("lightgreen", createColor(144, 238, 144));
  804. colorMap.put("lightgrey", createColor(211, 211, 211));
  805. colorMap.put("lightpink", createColor(255, 182, 193));
  806. colorMap.put("lightsalmon", createColor(255, 160, 122));
  807. colorMap.put("lightseagreen", createColor(32, 178, 170));
  808. colorMap.put("lightskyblue", createColor(135, 206, 250));
  809. colorMap.put("lightslategray", createColor(119, 136, 153));
  810. colorMap.put("lightslategrey", createColor(119, 136, 153));
  811. colorMap.put("lightsteelblue", createColor(176, 196, 222));
  812. colorMap.put("lightyellow", createColor(255, 255, 224));
  813. colorMap.put("lime", createColor(0, 255, 0));
  814. colorMap.put("limegreen", createColor(50, 205, 50));
  815. colorMap.put("linen", createColor(250, 240, 230));
  816. colorMap.put("magenta", createColor(255, 0, 255));
  817. colorMap.put("maroon", createColor(128, 0, 0));
  818. colorMap.put("mediumaquamarine", createColor(102, 205, 170));
  819. colorMap.put("mediumblue", createColor(0, 0, 205));
  820. colorMap.put("mediumorchid", createColor(186, 85, 211));
  821. colorMap.put("mediumpurple", createColor(147, 112, 219));
  822. colorMap.put("mediumseagreen", createColor(60, 179, 113));
  823. colorMap.put("mediumslateblue", createColor(123, 104, 238));
  824. colorMap.put("mediumspringgreen", createColor(0, 250, 154));
  825. colorMap.put("mediumturquoise", createColor(72, 209, 204));
  826. colorMap.put("mediumvioletred", createColor(199, 21, 133));
  827. colorMap.put("midnightblue", createColor(25, 25, 112));
  828. colorMap.put("mintcream", createColor(245, 255, 250));
  829. colorMap.put("mistyrose", createColor(255, 228, 225));
  830. colorMap.put("moccasin", createColor(255, 228, 181));
  831. colorMap.put("navajowhite", createColor(255, 222, 173));
  832. colorMap.put("navy", createColor(0, 0, 128));
  833. colorMap.put("oldlace", createColor(253, 245, 230));
  834. colorMap.put("olive", createColor(128, 128, 0));
  835. colorMap.put("olivedrab", createColor(107, 142, 35));
  836. colorMap.put("orange", createColor(255, 165, 0));
  837. colorMap.put("orangered", createColor(255, 69, 0));
  838. colorMap.put("orchid", createColor(218, 112, 214));
  839. colorMap.put("palegoldenrod", createColor(238, 232, 170));
  840. colorMap.put("palegreen", createColor(152, 251, 152));
  841. colorMap.put("paleturquoise", createColor(175, 238, 238));
  842. colorMap.put("palevioletred", createColor(219, 112, 147));
  843. colorMap.put("papayawhip", createColor(255, 239, 213));
  844. colorMap.put("peachpuff", createColor(255, 218, 185));
  845. colorMap.put("peru", createColor(205, 133, 63));
  846. colorMap.put("pink", createColor(255, 192, 203));
  847. colorMap.put("plum ", createColor(221, 160, 221));
  848. colorMap.put("plum", createColor(221, 160, 221));
  849. colorMap.put("powderblue", createColor(176, 224, 230));
  850. colorMap.put("purple", createColor(128, 0, 128));
  851. colorMap.put("red", createColor(255, 0, 0));
  852. colorMap.put("rosybrown", createColor(188, 143, 143));
  853. colorMap.put("royalblue", createColor(65, 105, 225));
  854. colorMap.put("saddlebrown", createColor(139, 69, 19));
  855. colorMap.put("salmon", createColor(250, 128, 114));
  856. colorMap.put("sandybrown", createColor(244, 164, 96));
  857. colorMap.put("seagreen", createColor(46, 139, 87));
  858. colorMap.put("seashell", createColor(255, 245, 238));
  859. colorMap.put("sienna", createColor(160, 82, 45));
  860. colorMap.put("silver", createColor(192, 192, 192));
  861. colorMap.put("skyblue", createColor(135, 206, 235));
  862. colorMap.put("slateblue", createColor(106, 90, 205));
  863. colorMap.put("slategray", createColor(112, 128, 144));
  864. colorMap.put("slategrey", createColor(112, 128, 144));
  865. colorMap.put("snow", createColor(255, 250, 250));
  866. colorMap.put("springgreen", createColor(0, 255, 127));
  867. colorMap.put("steelblue", createColor(70, 130, 180));
  868. colorMap.put("tan", createColor(210, 180, 140));
  869. colorMap.put("teal", createColor(0, 128, 128));
  870. colorMap.put("thistle", createColor(216, 191, 216));
  871. colorMap.put("tomato", createColor(255, 99, 71));
  872. colorMap.put("turquoise", createColor(64, 224, 208));
  873. colorMap.put("violet", createColor(238, 130, 238));
  874. colorMap.put("wheat", createColor(245, 222, 179));
  875. colorMap.put("white", createColor(255, 255, 255));
  876. colorMap.put("whitesmoke", createColor(245, 245, 245));
  877. colorMap.put("yellow", createColor(255, 255, 0));
  878. colorMap.put("yellowgreen", createColor(154, 205, 50));
  879. colorMap.put("transparent", new ColorWithAlternatives(0, 0, 0, 0, null));
  880. }
  881. /**
  882. * Lightens up a color for groove, ridge, inset and outset border effects.
  883. * @param col the color to lighten up
  884. * @param factor factor by which to lighten up (negative values darken the color)
  885. * @return the modified color
  886. */
  887. public static Color lightenColor(Color col, float factor) {
  888. return org.apache.xmlgraphics.java2d.color.ColorUtil.lightenColor(col, factor);
  889. }
  890. /**
  891. * Indicates whether the given color profile name is one of the pseudo-profiles supported
  892. * by FOP (ex. #CMYK).
  893. * @param colorProfileName the color profile name to check
  894. * @return true if the color profile name is of a built-in pseudo-profile
  895. */
  896. public static boolean isPseudoProfile(String colorProfileName) {
  897. return CMYK_PSEUDO_PROFILE.equalsIgnoreCase(colorProfileName)
  898. || SEPARATION_PSEUDO_PROFILE.equalsIgnoreCase(colorProfileName);
  899. }
  900. /**
  901. * Indicates whether the color is a gray value.
  902. * @param col the color
  903. * @return true if it is a gray value
  904. */
  905. public static boolean isGray(Color col) {
  906. return org.apache.xmlgraphics.java2d.color.ColorUtil.isGray(col);
  907. }
  908. /**
  909. * Creates an uncalibrated CMYK color with the given gray value.
  910. * @param black the gray component (0 - 1)
  911. * @return the CMYK color
  912. */
  913. public static Color toCMYKGrayColor(float black) {
  914. return org.apache.xmlgraphics.java2d.color.ColorUtil.toCMYKGrayColor(black);
  915. }
  916. }