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.

ColorWithFallback.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 org.apache.xmlgraphics.java2d.color.ColorWithAlternatives;
  22. /**
  23. * This class is a {@link Color} subclass adding a fallback color that FOP uses to re-serialize
  24. * color specifications as textual functions. The fallback is otherwise not used in producing
  25. * output formats.
  26. */
  27. public class ColorWithFallback extends ColorWithAlternatives {
  28. private static final long serialVersionUID = 7913922854959637136L;
  29. private final Color fallback;
  30. /**
  31. * Creates a new color
  32. * @param cspace the color space of the primary color
  33. * @param components the color components
  34. * @param alpha the alpha component
  35. * @param alternativeColors the array of alternative colors if applicable (may be null)
  36. * @param fallback the fallback color (usually an sRGB color)
  37. */
  38. public ColorWithFallback(ColorSpace cspace, float[] components, float alpha,
  39. Color[] alternativeColors, Color fallback) {
  40. super(cspace, components, alpha, alternativeColors);
  41. this.fallback = fallback;
  42. }
  43. /**
  44. * Copy constructor adding a fallback color.
  45. * @param color the color to be duplicated
  46. * @param fallback the fallback color (usually an sRGB color)
  47. */
  48. public ColorWithFallback(Color color, Color fallback) {
  49. this(color.getColorSpace(), color.getColorComponents(null),
  50. getAlphaFloat(color), getAlternativeColors(color), fallback);
  51. }
  52. private static float getAlphaFloat(Color color) {
  53. float[] comps = color.getComponents(null);
  54. return comps[comps.length - 1]; //Alpha is on last component
  55. }
  56. private static Color[] getAlternativeColors(Color color) {
  57. if (color instanceof ColorWithAlternatives) {
  58. ColorWithAlternatives cwa = (ColorWithAlternatives)color;
  59. if (cwa.hasAlternativeColors()) {
  60. return cwa.getAlternativeColors();
  61. }
  62. }
  63. return null;
  64. }
  65. /**
  66. * Returns the fallback color.
  67. * @return the fallback color
  68. */
  69. public Color getFallbackColor() {
  70. return this.fallback;
  71. }
  72. }