@@ -, +, @@ --- .../org/apache/poi/xslf/usermodel/Tutorial3.java | 4 ++ .../org/apache/poi/xslf/usermodel/XSLFColor.java | 34 ++++++++++++++ .../apache/poi/xslf/usermodel/XSLFSimpleShape.java | 48 +++++++------------- .../apache/poi/xslf/usermodel/XSLFTableCell.java | 4 +- .../org/apache/poi/xslf/usermodel/XSLFTextRun.java | 15 +++---- 5 files changed, 62 insertions(+), 43 deletions(-) --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java +++ a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java @@ -19,6 +19,7 @@ package org.apache.poi.xslf.usermodel; +import java.awt.Color; import java.awt.Rectangle; import java.io.FileOutputStream; import java.io.IOException; @@ -40,6 +41,9 @@ public class Tutorial3 { titleShape.setText("This is a slide title"); titleShape.setAnchor(new Rectangle(50, 50, 400, 100)); + // set background, with 25% opacity + titleShape.setFillColor(new Color(255, 0, 0, 64)); + FileOutputStream out = new FileOutputStream("title.pptx"); ppt.write(out); out.close(); --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFColor.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFColor.java @@ -33,6 +33,7 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor; import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor; import org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor; import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor; +import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor; import org.w3c.dom.Node; @@ -388,6 +389,39 @@ public class XSLFColor { } + + /** + * Set the solid fill colors - RGB and alpha. + * + * No sanity checks, the caller must handle null values itself. + * + * @param fill The solid fill properties + * @param color The color to use. + */ + /* package */ static void setFillColor(CTSolidColorFillProperties fill, Color color) + { + CTSRgbColor rgb = CTSRgbColor.Factory.newInstance(); + rgb.setVal(new byte[]{ + (byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue() + }); + + // alpha (%) + int alpha = (int) (100000 * color.getAlpha() / 255.0); + if (alpha < 100000) + { + rgb.addNewAlpha().setVal(alpha); + } + + fill.setSrgbClr(rgb); + + if (fill.isSetHslClr()) fill.unsetHslClr(); + if (fill.isSetPrstClr()) fill.unsetPrstClr(); + if (fill.isSetSchemeClr()) fill.unsetSchemeClr(); + if (fill.isSetScrgbClr()) fill.unsetScrgbClr(); + if (fill.isSetSysClr()) fill.unsetSysClr(); + } + + /** * Preset colors defined in DrawingML */ --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java @@ -180,35 +180,29 @@ public abstract class XSLFSimpleShape extends XSLFShape implements SimpleShape { if (styleMatrix == null) return null; CTLineStyleList lineStyles = styleMatrix.getLnStyleLst(); if (lineStyles == null || lineStyles.sizeOfLnArray() < idx) return null; - + return lineStyles.getLnArray(idx - 1); } + /** - * @param color the color to paint the shape outline. + * @param color the color to paint the shape outline. * A null value turns off the shape outline. */ public void setLineColor(Color color) { CTShapeProperties spPr = getSpPr(); if (color == null) { - if (spPr.isSetLn() && spPr.getLn().isSetSolidFill()) + if (spPr.isSetLn() && spPr.getLn().isSetSolidFill()) { spPr.getLn().unsetSolidFill(); + } } else { - CTLineProperties ln = spPr.isSetLn() ? spPr.getLn() : spPr - .addNewLn(); + CTLineProperties ln = spPr.isSetLn() ? spPr.getLn() : spPr.addNewLn(); - CTSRgbColor rgb = CTSRgbColor.Factory.newInstance(); - rgb.setVal(new byte[]{(byte) color.getRed(), - (byte) color.getGreen(), (byte) color.getBlue()}); - - CTSolidColorFillProperties fill = ln.isSetSolidFill() ? ln - .getSolidFill() : ln.addNewSolidFill(); - fill.setSrgbClr(rgb); - if(fill.isSetHslClr()) fill.unsetHslClr(); - if(fill.isSetPrstClr()) fill.unsetPrstClr(); - if(fill.isSetSchemeClr()) fill.unsetSchemeClr(); - if(fill.isSetScrgbClr()) fill.unsetScrgbClr(); - if(fill.isSetSysClr()) fill.unsetSysClr(); + XSLFColor.setFillColor + ( + ln.isSetSolidFill() ? ln.getSolidFill() : ln.addNewSolidFill(), + color + ); } } @@ -491,7 +485,7 @@ public abstract class XSLFSimpleShape extends XSLFShape implements SimpleShape { * specified color. * * @param color the solid color fill. The value of null unsets - * the solidFIll attribute from the underlying xml + * the solidFill attribute from the underlying xml */ public void setFillColor(Color color) { CTShapeProperties spPr = getSpPr(); @@ -502,19 +496,11 @@ public abstract class XSLFSimpleShape extends XSLFShape implements SimpleShape { } else { if (spPr.isSetNoFill()) spPr.unsetNoFill(); - CTSolidColorFillProperties fill = spPr.isSetSolidFill() ? spPr - .getSolidFill() : spPr.addNewSolidFill(); - - CTSRgbColor rgb = CTSRgbColor.Factory.newInstance(); - rgb.setVal(new byte[]{(byte) color.getRed(), - (byte) color.getGreen(), (byte) color.getBlue()}); - - fill.setSrgbClr(rgb); - if(fill.isSetHslClr()) fill.unsetHslClr(); - if(fill.isSetPrstClr()) fill.unsetPrstClr(); - if(fill.isSetSchemeClr()) fill.unsetSchemeClr(); - if(fill.isSetScrgbClr()) fill.unsetScrgbClr(); - if(fill.isSetSysClr()) fill.unsetSysClr(); + XSLFColor.setFillColor + ( + spPr.isSetSolidFill() ? spPr.getSolidFill() : spPr.addNewSolidFill(), + color + ); } } --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java @@ -146,9 +146,7 @@ public class XSLFTableCell extends XSLFTextShape { tl.setW(STLineEndWidth.MED); tl.setLen(STLineEndLength.MED); - CTSRgbColor rgb = CTSRgbColor.Factory.newInstance(); - rgb.setVal(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()}); - ln.addNewSolidFill().setSrgbClr(rgb); + XSLFColor.setFillColor(ln.addNewSolidFill(), color); } } --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java @@ -85,18 +85,15 @@ public class XSLFTextRun implements TextRun { @Override public void setFontColor(Color color) { CTTextCharacterProperties rPr = getRPr(); - CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill(); - CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr(); - clr.setVal(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()}); - - if(fill.isSetHslClr()) fill.unsetHslClr(); - if(fill.isSetPrstClr()) fill.unsetPrstClr(); - if(fill.isSetSchemeClr()) fill.unsetSchemeClr(); - if(fill.isSetScrgbClr()) fill.unsetScrgbClr(); - if(fill.isSetSysClr()) fill.unsetSysClr(); + XSLFColor.setFillColor + ( + rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill(), + color + ); } + @Override public Color getFontColor(){ final XSLFTheme theme = _p.getParentShape().getSheet().getTheme(); --