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.

CIELabColorFunction.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.apps.FOUserAgent;
  20. import org.apache.fop.datatypes.PercentBase;
  21. import org.apache.fop.datatypes.PercentBaseContext;
  22. import org.apache.fop.fo.properties.ColorProperty;
  23. import org.apache.fop.fo.properties.Property;
  24. /**
  25. * Implements the cie-lab-color() function.
  26. * @since XSL-FO 2.0
  27. */
  28. class CIELabColorFunction extends FunctionBase {
  29. /** {@inheritDoc} */
  30. public int getRequiredArgsCount() {
  31. return 6;
  32. }
  33. @Override
  34. /** {@inheritDoc} */
  35. public PercentBase getPercentBase() {
  36. return new CIELabPercentBase();
  37. }
  38. /** {@inheritDoc} */
  39. public Property eval(Property[] args, PropertyInfo pInfo) throws PropertyException {
  40. float red = args[0].getNumber().floatValue();
  41. float green = args[1].getNumber().floatValue();
  42. float blue = args[2].getNumber().floatValue();
  43. /* Verify sRGB replacement arguments */
  44. if ((red < 0 || red > 255) || (green < 0 || green > 255) || (blue < 0 || blue > 255)) {
  45. throw new PropertyException("sRGB color values out of range. "
  46. + "Arguments to cie-lab-color() must be [0..255] or [0%..100%]");
  47. }
  48. float l = args[3].getNumber().floatValue();
  49. float a = args[4].getNumber().floatValue();
  50. float b = args[5].getNumber().floatValue();
  51. if (l < 0 || l > 100) {
  52. throw new PropertyException("L* value out of range. Valid range: [0..100]");
  53. }
  54. if (a < -127 || a > 127 || b < -127 || b > 127) {
  55. throw new PropertyException("a* and b* values out of range. Valid range: [-127..+127]");
  56. }
  57. StringBuffer sb = new StringBuffer();
  58. sb.append("cie-lab-color(" + red + "," + green + "," + blue + ","
  59. + l + "," + a + "," + b + ")");
  60. FOUserAgent ua = (pInfo == null)
  61. ? null
  62. : (pInfo.getFO() == null ? null : pInfo.getFO().getUserAgent());
  63. return ColorProperty.getInstance(ua, sb.toString());
  64. }
  65. private static class CIELabPercentBase implements PercentBase {
  66. public int getDimension() {
  67. return 0;
  68. }
  69. public double getBaseValue() {
  70. return 1.0f;
  71. }
  72. public int getBaseLength(PercentBaseContext context) {
  73. return 0;
  74. }
  75. }
  76. }