Shading

Help
nipps
2011-04-21
2013-04-17
  • nipps

    nipps - 2011-04-21

    Hello,
    I would like to inform you about a workaround for the radial and axial shading types. This just works for the AWT implementation. Since SWT is about to get support for radial patterns, you could do it the same way. I'm sure there is room for improvement but it may be the first step.

    AwtGraphicsEnvironmentAdapter:

    protected Paint createPaintShadingPaint(ShadingPaint paint) {
            // TODO 2 @ehk
            switch (paint.getPdShading().getShadingType()) {
            case 2:
                return createAxialShading(paint);
            case 3:
                return createRadialShading(paint);
            default:
                return new Color(0.5f, 0.5f, 0.5f, 0.5f);
            }
        }
    
    private Paint createRadialShading(ShadingPaint paint) {
            PDRadialShading radialShading = (PDRadialShading) paint.getPdShading();
            float[] coords = radialShading.getCoords();
            float[] domain = radialShading.getDomain();
            float[] rgb1, rgb2;
            PDFunction function = radialShading.getFunction();
            Color rgbColor1;
            Color rgbColor2;
            AwtColorSpace awtColorSpace = AwtColorSpaceFactory.get().createColorSpace(paint.getPdShading().getColorSpace());
            rgb1 = awtColorSpace.getRGB(function.evaluate(new float[] { domain[0] }));
            rgbColor1 = new Color(rgb1[0], rgb1[1], rgb1[2]);
            rgb2 = awtColorSpace.getRGB(function.evaluate(new float[] { domain[1] }));
            rgbColor2 = new Color(rgb2[0], rgb2[1], rgb2[2]);
            float[] fractions = new float[] {0.0f, 1.0f};
            PDFunction[] functions = new PDFunction[]{ function };
            float[] bounds = new float[0];
            if (function instanceof PDStitchingFunction) {
                PDStitchingFunction stitch = (PDStitchingFunction) function;
                functions = stitch.getFunctions();
                bounds = stitch.getBounds();
            }
    
            Color[] colors = new Color[functions.length + 1];
            fractions = new float[functions.length + 1];
            colors[0] = rgbColor1;
            fractions[0] = 0.0f;
            float norm = domain[1];
    
            float[] rgb;
    
            if (function instanceof PDSampledFunction) {
                colors[1] = rgbColor2;
                fractions[1] = 1.0f;
            }
            else {
                for(int i = 0; i < functions.length; i++) {
                    PDInterpolationFunction f = (PDInterpolationFunction) functions[i];
                    fractions[i+1] = i < functions.length - 1 ? bounds[i] / norm : 1.0f;
    
                    rgb = awtColorSpace.getRGB(f.getC1());
    
                    colors[i+1] = new Color(rgb[0], rgb[1], rgb[2]);
                }
            }
            return new RadialGradientPaint(
                    coords[3], coords[4],
                    coords[5],
                    coords[0], coords[1],
                    fractions,
                    colors, CycleMethod.NO_CYCLE);
        }
    
    private Paint createAxialShading(ShadingPaint paint) {
            PDAxialShading axialShading = (PDAxialShading) paint.getPdShading();
            float[] coords = axialShading.getCoords();
            float[] domain = axialShading.getDomain();
            PDFunction function = axialShading.getFunction();
            float[] rgb1, rgb2;
            Color rgbColor1;
            Color rgbColor2;
            AwtColorSpace awtColorSpace = AwtColorSpaceFactory.get()
            .createColorSpace(paint.getPdShading().getColorSpace());
            rgb1 = awtColorSpace.getRGB(function.evaluate(new float[] { domain[0] }));
            rgb2 = awtColorSpace.getRGB(function.evaluate(new float[] { domain[1] }));
    
            // try to get correct colors
            PDFunction[] functions = new PDFunction[]{ function };
            if(function instanceof PDStitchingFunction) {
                PDStitchingFunction stitch = (PDStitchingFunction) function;
                functions = stitch.getFunctions();
    
                PDInterpolationFunction f = (PDInterpolationFunction) functions[0];
                rgb1 = awtColorSpace.getRGB(f.getC0());
    
                f = (PDInterpolationFunction) functions[1];
                rgb2 = awtColorSpace.getRGB(f.getC1());
            }
    
            rgbColor1 = new Color(rgb1[0], rgb1[1], rgb1[2]);
    
            // dirty workaround
            try {
                rgbColor2 = new Color(rgb2[0] - 0.5f, rgb2[1] - 0.5f, rgb2[2] - 0.5f);
            }
            catch (Exception e) {
                rgbColor2 = new Color(rgb2[0], rgb2[1], rgb2[2]);
            }
    
            return new GradientPaint(coords[0], coords[1], rgbColor1, coords[2], coords[3], rgbColor2);
        }
    

    You need to create PDRadialShading.java. It should be pretty much as PDAxialShading.

     
  • mtraut

    mtraut - 2011-04-26

    Many thanks - we will check this for the next release….

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks