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.

RGBNamedColorFunction.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.fo.expr;
  19. import org.apache.fop.datatypes.PercentBase;
  20. import org.apache.fop.datatypes.PercentBaseContext;
  21. import org.apache.fop.fo.pagination.ColorProfile;
  22. import org.apache.fop.fo.pagination.Declarations;
  23. import org.apache.fop.fo.properties.ColorProperty;
  24. import org.apache.fop.fo.properties.Property;
  25. /**
  26. * Implements the rgb-named-color() function.
  27. * @since XSL-FO 2.0
  28. */
  29. class RGBNamedColorFunction extends FunctionBase {
  30. /** {@inheritDoc} */
  31. public int getRequiredArgsCount() {
  32. return 5;
  33. }
  34. /** {@inheritDoc} */
  35. @Override
  36. public PercentBase getPercentBase() {
  37. return new RGBNamedPercentBase();
  38. }
  39. /** {@inheritDoc} */
  40. public Property eval(Property[] args, PropertyInfo pInfo) throws PropertyException {
  41. // Map color profile NCNAME to src from declarations/color-profile element
  42. String colorProfileName = args[3].getString();
  43. String colorName = args[4].getString();
  44. Declarations decls = (pInfo.getFO() != null
  45. ? pInfo.getFO().getRoot().getDeclarations()
  46. : null);
  47. ColorProfile cp = null;
  48. if (decls != null) {
  49. cp = decls.getColorProfile(colorProfileName);
  50. }
  51. if (cp == null) {
  52. PropertyException pe = new PropertyException("The " + colorProfileName
  53. + " color profile was not declared");
  54. pe.setPropertyInfo(pInfo);
  55. throw pe;
  56. }
  57. float red = 0;
  58. float green = 0;
  59. float blue = 0;
  60. red = args[0].getNumber().floatValue();
  61. green = args[1].getNumber().floatValue();
  62. blue = args[2].getNumber().floatValue();
  63. /* Verify rgb replacement arguments */
  64. if ((red < 0 || red > 255)
  65. || (green < 0 || green > 255)
  66. || (blue < 0 || blue > 255)) {
  67. throw new PropertyException("sRGB color values out of range. "
  68. + "Arguments to rgb-named-color() must be [0..255] or [0%..100%]");
  69. }
  70. // rgb-named-color is replaced with fop-rgb-named-color which has an extra argument
  71. // containing the color profile src attribute as it is defined in the color-profile
  72. // declarations element.
  73. StringBuffer sb = new StringBuffer();
  74. sb.append("fop-rgb-named-color(");
  75. sb.append(red / 255f);
  76. sb.append(',').append(green / 255f);
  77. sb.append(',').append(blue / 255f);
  78. sb.append(',').append(colorProfileName);
  79. sb.append(',').append(cp.getSrc());
  80. sb.append(", '").append(colorName).append('\'');
  81. sb.append(")");
  82. return ColorProperty.getInstance(pInfo.getUserAgent(), sb.toString());
  83. }
  84. private static final class RGBNamedPercentBase implements PercentBase {
  85. /** {@inheritDoc} */
  86. public int getBaseLength(PercentBaseContext context) throws PropertyException {
  87. return 0;
  88. }
  89. /** {@inheritDoc} */
  90. public double getBaseValue() {
  91. return 255f;
  92. }
  93. /** {@inheritDoc} */
  94. public int getDimension() {
  95. return 0;
  96. }
  97. }
  98. }