/**
   * Calculate the colours value of the point xy on the line point1 and point2.
   *
   * @param colorSpace colour space to apply to the function output
   * @param xy point to calcualte the colour of.
   * @param point1 start of gradient line
   * @param point2 end of gradient line.
   * @param t0 domain min
   * @param t1 domain max
   * @return colour derived from the input parameters.
   */
  private Color calculateColour(
      PColorSpace colorSpace,
      Point2D.Float xy,
      Point2D.Float point1,
      Point2D.Float point2,
      float t0,
      float t1) {
    // find colour at point 1
    float xPrime = linearMapping(xy, point1, point2);
    float t = parametrixValue(xPrime, t0, t1, extend);
    // find colour at point 2
    float[] input = new float[1];
    input[0] = t;
    // apply the function to the given input
    if (function != null) {

      float[] output;
      int length = function.length;
      // simple 1 in N out function
      if (length == 1) {
        output = function[0].calculate(input);
      } else {
        // vector of function for each colour component, 1 in 1 out.
        output = new float[length];
        for (int i = 0; i < length; i++) {
          output[i] = function[i].calculate(input)[0];
        }
      }

      if (output != null) {
        output = PColorSpace.reverse(output);
        return colorSpace.getColor(output, true);
      } else {
        return null;
      }

    } else {
      logger.fine("Error processing Shading Type 2 Pattern.");
      return null;
    }
  }
Example #2
0
 public Color getColor(float[] f, boolean fillAndStroke) {
   init();
   if (colorSpace != null && !failed) {
     try {
       // generate a key for the colour
       int key = generateKey(f);
       if (f.length <= 3) {
         return addColorToCache(iccColorCache3B, key, colorSpace, f);
       } else {
         return addColorToCache(iccColorCache4B, key, colorSpace, f);
       }
     } catch (Exception e) {
       logger.log(Level.FINE, "Error getting ICCBased colour", e);
       failed = true;
     }
   }
   return alternate.getColor(f);
 }
  @SuppressWarnings("unchecked")
  public synchronized void init() {

    if (inited) {
      return;
    }

    // shading dictionary
    if (shading == null) {
      shading = library.getDictionary(entries, SHADING_KEY);
    }

    shadingType = library.getInt(shading, SHADING_TYPE_KEY);
    bBox = library.getRectangle(shading, BBOX_KEY);
    colorSpace = PColorSpace.getColorSpace(library, library.getObject(shading, COLORSPACE_KEY));
    Object tmp = library.getObject(shading, BACKGROUND_KEY);
    if (tmp != null && tmp instanceof List) {
      background = (java.util.List) tmp;
    }
    antiAlias = library.getBoolean(shading, ANTIALIAS_KEY);

    // get type 2 specific data.
    tmp = library.getObject(shading, DOMAIN_KEY);
    if (tmp instanceof List) {
      domain = (List<Number>) tmp;
    } else {
      domain = new ArrayList<Number>(2);
      domain.add(new Float(0.0));
      domain.add(new Float(1.0));
    }

    tmp = library.getObject(shading, COORDS_KEY);
    if (tmp instanceof List) {
      coords = (java.util.List) tmp;
    }
    tmp = library.getObject(shading, EXTEND_KEY);
    if (tmp instanceof List) {
      extend = (List<Boolean>) tmp;
    } else {
      extend = new ArrayList<Boolean>(2);
      extend.add(false);
      extend.add(false);
    }
    tmp = library.getObject(shading, FUNCTION_KEY);
    if (tmp != null) {
      if (!(tmp instanceof List)) {
        function = new Function[] {Function.getFunction(library, tmp)};
      } else {
        List functionTemp = (List) tmp;
        function = new Function[functionTemp.size()];
        for (int i = 0; i < functionTemp.size(); i++) {
          function[i] = Function.getFunction(library, functionTemp.get(i));
        }
      }
    }

    // calculate the t's
    float t0 = domain.get(0).floatValue();
    float t1 = domain.get(1).floatValue();

    // first off, create the two needed start and end points of the line
    Point2D.Float startPoint =
        new Point2D.Float(
            ((Number) coords.get(0)).floatValue(), ((Number) coords.get(1)).floatValue());

    Point2D.Float endPoint =
        new Point2D.Float(
            ((Number) coords.get(2)).floatValue(), ((Number) coords.get(3)).floatValue());

    // corner case where a pdf engine give zero zero coords which batik
    // can't handle so we pad it slightly.
    if (startPoint.equals(endPoint)) {
      endPoint.x++;
    }

    // calculate colour based on points that make up the line, 10 is a good
    // number for speed and gradient quality.
    int numberOfPoints = 10;
    Color[] colors = calculateColorPoints(numberOfPoints, startPoint, endPoint, t0, t1);
    float[] dist = calculateDomainEntries(numberOfPoints, t0, t1);

    linearGradientPaint =
        new LinearGradientPaint(
            startPoint,
            endPoint,
            dist,
            colors,
            MultipleGradientPaint.NO_CYCLE,
            MultipleGradientPaint.LINEAR_RGB,
            matrix);
    inited = true;
  }