Esempio n. 1
0
        public void TestIndexedColour()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("48779.xlsx");

            // Check the CTColor is as expected
            XSSFColor indexed = (XSSFColor)wb.GetCellStyleAt(1).FillBackgroundColorColor;

            Assert.AreEqual(true, indexed.GetCTColor().IsSetIndexed());
            Assert.AreEqual(64, indexed.GetCTColor().indexed);
            Assert.AreEqual(false, indexed.GetCTColor().IsSetRgb());
            Assert.AreEqual(null, indexed.GetCTColor().GetRgb());

            // Now check the XSSFColor
            // Note - 64 is a special "auto" one with no rgb equiv
            Assert.AreEqual(64, indexed.Indexed);
            Assert.AreEqual(null, indexed.GetRgb());
            Assert.AreEqual(null, indexed.GetRgbWithTint());
            Assert.AreEqual(null, indexed.GetARGBHex());

            // Now Move to one with indexed rgb values
            indexed.Indexed = (59);
            Assert.AreEqual(true, indexed.GetCTColor().IsSetIndexed());
            Assert.AreEqual(59, indexed.GetCTColor().indexed);
            Assert.AreEqual(false, indexed.GetCTColor().IsSetRgb());
            Assert.AreEqual(null, indexed.GetCTColor().GetRgb());

            Assert.AreEqual(59, indexed.Indexed);
            Assert.AreEqual("FF333300", indexed.GetARGBHex());

            Assert.AreEqual(3, indexed.GetRgb().Length);
            Assert.AreEqual(0x33, indexed.GetRgb()[0]);
            Assert.AreEqual(0x33, indexed.GetRgb()[1]);
            Assert.AreEqual(0x00, indexed.GetRgb()[2]);

            Assert.AreEqual(4, indexed.GetARgb().Length);
            Assert.AreEqual(255, indexed.GetARgb()[0]);
            Assert.AreEqual(0x33, indexed.GetARgb()[1]);
            Assert.AreEqual(0x33, indexed.GetARgb()[2]);
            Assert.AreEqual(0x00, indexed.GetARgb()[3]);

            // You don't Get tinted indexed colours, sorry...
            Assert.AreEqual(null, indexed.GetRgbWithTint());
        }
Esempio n. 2
0
        public void TestGetSetFillBackgroundColor()
        {
            Assert.AreEqual(IndexedColors.Automatic.Index, cellStyle.FillBackgroundColor);
            Assert.IsNull(cellStyle.FillBackgroundColorColor);

            XSSFColor clr;

            int num = stylesTable.GetFills().Count;

            //setting indexed color
            cellStyle.FillBackgroundColor = (IndexedColors.Red.Index);
            Assert.AreEqual(IndexedColors.Red.Index, cellStyle.FillBackgroundColor);
            clr = (XSSFColor)cellStyle.FillBackgroundColorColor;
            Assert.IsTrue(clr.GetCTColor().IsSetIndexed());
            Assert.AreEqual(IndexedColors.Red.Index, clr.Indexed);
            //a new fill was Added to the styles table
            Assert.AreEqual(num + 1, stylesTable.GetFills().Count);

            //id of the Created border
            int FillId = (int)cellStyle.GetCoreXf().fillId;

            Assert.IsTrue(FillId > 0);
            //check changes in the underlying xml bean
            CT_Fill ctFill = stylesTable.GetFillAt(FillId).GetCTFill();

            Assert.AreEqual((uint)IndexedColors.Red.Index, ctFill.GetPatternFill().bgColor.indexed);

            //setting XSSFColor
            num = stylesTable.GetFills().Count;
            clr = new XSSFColor(Color.Cyan);
            cellStyle.SetFillBackgroundColor(clr); // TODO this testcase assumes that cellStyle creates a new CT_Fill, but the implementation changes the existing style. - do not know whats right 8-(
            Assert.AreEqual(clr.GetCTColor().ToString(), ((XSSFColor)cellStyle.FillBackgroundColorColor).GetCTColor().ToString());
            byte[] rgb = ((XSSFColor)cellStyle.FillBackgroundColorColor).GetRgb();
            Assert.AreEqual(Color.Cyan.ToArgb(), Color.FromArgb(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF).ToArgb());
            //another border was added to the styles table
            Assert.AreEqual(num + 1, stylesTable.GetFills().Count);

            //passing null unsets the color
            cellStyle.SetFillBackgroundColor(null);
            Assert.IsNull(cellStyle.FillBackgroundColorColor);
            Assert.AreEqual(IndexedColors.Automatic.Index, cellStyle.FillBackgroundColor);
        }
Esempio n. 3
0
        public void TestGetSetRightBorderColor()
        {
            //defaults
            Assert.AreEqual(IndexedColors.Black.Index, cellStyle.RightBorderColor);
            Assert.IsNull(cellStyle.RightBorderXSSFColor);

            int num = stylesTable.GetBorders().Count;

            XSSFColor clr;

            //setting indexed color
            cellStyle.RightBorderColor = (IndexedColors.BlueGrey.Index);
            Assert.AreEqual(IndexedColors.BlueGrey.Index, cellStyle.RightBorderColor);
            clr = cellStyle.RightBorderXSSFColor;
            Assert.IsTrue(clr.GetCTColor().IsSetIndexed());
            Assert.AreEqual(IndexedColors.BlueGrey.Index, clr.Indexed);
            //a new border was Added to the styles table
            Assert.AreEqual(num + 1, stylesTable.GetBorders().Count);

            //id of the Created border
            int borderId = (int)cellStyle.GetCoreXf().borderId;

            Assert.IsTrue(borderId > 0);
            //check Changes in the underlying xml bean
            CT_Border ctBorder = stylesTable.GetBorderAt(borderId).GetCTBorder();

            Assert.AreEqual((uint)IndexedColors.BlueGrey.Index, ctBorder.right.color.indexed);

            //setting XSSFColor
            num = stylesTable.GetBorders().Count;
            clr = new XSSFColor(Color.Cyan);
            cellStyle.SetRightBorderColor(clr);
            Assert.AreEqual(clr.GetCTColor().ToString(), cellStyle.RightBorderXSSFColor.GetCTColor().ToString());
            byte[] rgb = cellStyle.RightBorderXSSFColor.GetRgb();
            Assert.AreEqual(Color.Cyan.ToArgb(), Color.FromArgb(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF).ToArgb());
            //another border was Added to the styles table
            Assert.AreEqual(num, stylesTable.GetBorders().Count);

            //passing null unsets the color
            cellStyle.SetRightBorderColor(null);
            Assert.IsNull(cellStyle.RightBorderXSSFColor);
        }
Esempio n. 4
0
        public void SetFillForegroundColor(XSSFColor color)
        {
            CT_Fill        ctFill        = this.GetCTFill();
            CT_PatternFill ctPatternFill = ctFill.GetPatternFill();

            if (color == null)
            {
                ctPatternFill?.unsetFgColor();
            }
            else
            {
                if (ctPatternFill == null)
                {
                    ctPatternFill = ctFill.AddNewPatternFill();
                }
                ctPatternFill.fgColor = color.GetCTColor();
            }
            this._cellXf.fillId    = (uint)this._stylesSource.PutFill(new XSSFCellFill(ctFill));
            this._cellXf.applyFill = true;
        }
Esempio n. 5
0
        public void SetTopBorderColor(XSSFColor color)
        {
            CT_Border ctBorder = this.GetCTBorder();

            if (color == null && !ctBorder.IsSetTop())
            {
                return;
            }
            CT_BorderPr ctBorderPr = ctBorder.IsSetTop() ? ctBorder.top : ctBorder.AddNewTop();

            if (color != null)
            {
                ctBorderPr.color = color.GetCTColor();
            }
            else
            {
                ctBorderPr.UnsetColor();
            }
            this._cellXf.borderId    = (uint)this._stylesSource.PutBorder(new XSSFCellBorder(ctBorder, this._theme));
            this._cellXf.applyBorder = true;
        }
        public XSSFDataBarFormatting CreateDataBarFormatting(XSSFColor color)
        {
            // Is it already there?
            if (_cfRule.IsSetDataBar() && _cfRule.type == ST_CfType.dataBar)
            {
                return(DataBarFormatting as XSSFDataBarFormatting);
            }

            // Mark it as being a Data Bar
            _cfRule.type = ST_CfType.dataBar;

            // Ensure the right element
            CT_DataBar bar = null;

            if (_cfRule.IsSetDataBar())
            {
                bar = _cfRule.dataBar;
            }
            else
            {
                bar = _cfRule.AddNewDataBar();
            }
            // Set the color
            bar.color = (color.GetCTColor());

            // Add the default thresholds
            CT_Cfvo min = bar.AddNewCfvo();

            min.type = (ST_CfvoType)Enum.Parse(typeof(ST_CfvoType), RangeType.MIN.name);
            CT_Cfvo max = bar.AddNewCfvo();

            max.type = (ST_CfvoType)Enum.Parse(typeof(ST_CfvoType), RangeType.MAX.name);

            // Wrap and return
            return(new XSSFDataBarFormatting(bar));
        }
Esempio n. 7
0
        /**
        * Set the foreground fill color represented as a {@link XSSFColor} value.
         * <br/>
        * <i>Note: Ensure Foreground color is Set prior to background color.</i>
        * @param color the color to use
        * @see #setFillBackgroundColor(NPOI.xssf.usermodel.XSSFColor) )
        */
        public void SetFillForegroundColor(XSSFColor color)
        {
            CT_Fill ct = GetCTFill();

            CT_PatternFill ptrn = ct.GetPatternFill();
            if (color == null)
            {
                if (ptrn != null) ptrn.unsetFgColor();
            }
            else
            {
                if (ptrn == null) ptrn = ct.AddNewPatternFill();
                ptrn.fgColor =(color.GetCTColor());
            }

            int idx = _stylesSource.PutFill(new XSSFCellFill(ct));

            _cellXf.fillId = (uint)idx;
            _cellXf.applyFill = (true);
        }
Esempio n. 8
0
        /**
         * Set the color to use for the bottom border
         *
         * @param color the color to use, null means no color
         */
        public void SetBottomBorderColor(XSSFColor color)
        {
            CT_Border ct = GetCTBorder();
            if (color == null && !ct.IsSetBottom()) return;

            CT_BorderPr pr = ct.IsSetBottom() ? ct.bottom : ct.AddNewBottom();
            if (color != null) pr.SetColor(color.GetCTColor());
            else pr.UnsetColor();

            int idx = _stylesSource.PutBorder(new XSSFCellBorder(ct, _theme));

            _cellXf.borderId = (uint)idx;
            _cellXf.applyBorder = (true);
        }
Esempio n. 9
0
        public void TestGetSetFillBackgroundColor()
        {
            Assert.AreEqual(IndexedColors.Automatic.Index, cellStyle.FillBackgroundColor);
            Assert.IsNull(cellStyle.FillBackgroundColorColor);

            XSSFColor clr;

            int num = stylesTable.GetFills().Count;

            //setting indexed color
            cellStyle.FillBackgroundColor = (IndexedColors.Red.Index);
            Assert.AreEqual(IndexedColors.Red.Index, cellStyle.FillBackgroundColor);
            clr = (XSSFColor)cellStyle.FillBackgroundColorColor;
            Assert.IsTrue(clr.GetCTColor().IsSetIndexed());
            Assert.AreEqual(IndexedColors.Red.Index, clr.Indexed);
            //a new fill was Added to the styles table
            Assert.AreEqual(num + 1, stylesTable.GetFills().Count);

            //id of the Created border
            int FillId = (int)cellStyle.GetCoreXf().fillId;
            Assert.IsTrue(FillId > 0);
            //check changes in the underlying xml bean
            CT_Fill ctFill = stylesTable.GetFillAt(FillId).GetCTFill();
            Assert.AreEqual((uint)IndexedColors.Red.Index, ctFill.GetPatternFill().bgColor.indexed);

            //setting XSSFColor
            num = stylesTable.GetFills().Count;
            clr = new XSSFColor(Color.Cyan);
            cellStyle.SetFillBackgroundColor(clr); // TODO this testcase assumes that cellStyle creates a new CT_Fill, but the implementation changes the existing style. - do not know whats right 8-(
            Assert.AreEqual(clr.GetCTColor().ToString(), ((XSSFColor)cellStyle.FillBackgroundColorColor).GetCTColor().ToString());
            byte[] rgb = ((XSSFColor)cellStyle.FillBackgroundColorColor).GetRgb();
            Assert.AreEqual(Color.Cyan.ToArgb(), Color.FromArgb(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF).ToArgb());
            //another border was added to the styles table
            Assert.AreEqual(num, stylesTable.GetFills().Count);

            //passing null unsets the color
            cellStyle.SetFillBackgroundColor(null);
            Assert.IsNull(cellStyle.FillBackgroundColorColor);
            Assert.AreEqual(IndexedColors.Automatic.Index, cellStyle.FillBackgroundColor);
        }
Esempio n. 10
0
        public void TestGetSetRightBorderColor()
        {
            //defaults
            Assert.AreEqual(IndexedColors.Black.Index, cellStyle.RightBorderColor);
            Assert.IsNull(cellStyle.RightBorderXSSFColor);

            int num = stylesTable.GetBorders().Count;

            XSSFColor clr;

            //setting indexed color
            cellStyle.RightBorderColor = (IndexedColors.BlueGrey.Index);
            Assert.AreEqual(IndexedColors.BlueGrey.Index, cellStyle.RightBorderColor);
            clr = cellStyle.RightBorderXSSFColor;
            Assert.IsTrue(clr.GetCTColor().IsSetIndexed());
            Assert.AreEqual(IndexedColors.BlueGrey.Index, clr.Indexed);
            //a new border was Added to the styles table
            Assert.AreEqual(num + 1, stylesTable.GetBorders().Count);

            //id of the Created border
            int borderId = (int)cellStyle.GetCoreXf().borderId;
            Assert.IsTrue(borderId > 0);
            //check Changes in the underlying xml bean
            CT_Border ctBorder = stylesTable.GetBorderAt(borderId).GetCTBorder();
            Assert.AreEqual((uint)IndexedColors.BlueGrey.Index, ctBorder.right.color.indexed);

            //setting XSSFColor
            num = stylesTable.GetBorders().Count;
            clr = new XSSFColor(Color.Cyan);
            cellStyle.SetRightBorderColor(clr);
            Assert.AreEqual(clr.GetCTColor().ToString(), cellStyle.RightBorderXSSFColor.GetCTColor().ToString());
            byte[] rgb = cellStyle.RightBorderXSSFColor.GetRgb();
            Assert.AreEqual(Color.Cyan.ToArgb(), Color.FromArgb(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF).ToArgb());
            //another border was Added to the styles table
            Assert.AreEqual(num, stylesTable.GetBorders().Count);

            //passing null unsets the color
            cellStyle.SetRightBorderColor(null);
            Assert.IsNull(cellStyle.RightBorderXSSFColor);
        }
Esempio n. 11
0
        /**
         * If the colour is based on a theme, then inherit 
         *  information (currently just colours) from it as
         *  required.
         */
        public void InheritFromThemeAsRequired(XSSFColor color)
        {
            if (color == null)
            {
                // Nothing for us to do
                return;
            }
            if (!color.GetCTColor().themeSpecified)
            {
                // No theme Set, nothing to do
                return;
            }

            // Get the theme colour
            XSSFColor themeColor = GetThemeColor(color.Theme);
            // Set the raw colour, not the adjusted one
            // Do a raw Set, no adjusting at the XSSFColor layer either
            color.GetCTColor().SetRgb(themeColor.GetCTColor().GetRgb());

            // All done
        }
Esempio n. 12
0
 /**
  * Set the color to use for the selected border
  *
  * @param side  - where to apply the color defInition
  * @param color - the color to use
  */
 public void SetBorderColor(BorderSide side, XSSFColor color)
 {
     CT_BorderPr borderPr = GetBorder(side, true);
     if (color == null) borderPr.UnsetColor();
     else
         borderPr.color = color.GetCTColor();
 }
Esempio n. 13
0
        public void TestRGBColour()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("50299.xlsx");

            // Check the CTColor is as expected
            XSSFColor rgb3 = (XSSFColor)((XSSFCellStyle)wb.GetCellStyleAt((short)25)).FillForegroundXSSFColor;

            Assert.AreEqual(false, rgb3.GetCTColor().IsSetIndexed());
            Assert.AreEqual(0, rgb3.GetCTColor().indexed);
            Assert.AreEqual(true, rgb3.GetCTColor().IsSetTint());
            Assert.AreEqual(-0.34999, rgb3.GetCTColor().tint, 0.00001);
            Assert.AreEqual(true, rgb3.GetCTColor().IsSetRgb());
            Assert.AreEqual(3, rgb3.GetCTColor().GetRgb().Length);

            // Now check the XSSFColor
            Assert.AreEqual(0, rgb3.Indexed);
            Assert.AreEqual(-0.34999, rgb3.Tint, 0.00001);

            Assert.AreEqual("FFFFFFFF", rgb3.GetARGBHex());
            Assert.AreEqual(3, rgb3.GetRgb().Length);
            Assert.AreEqual(255, rgb3.GetRgb()[0]);
            Assert.AreEqual(255, rgb3.GetRgb()[1]);
            Assert.AreEqual(255, rgb3.GetRgb()[2]);

            Assert.AreEqual(4, rgb3.GetARgb().Length);
            Assert.AreEqual(255, rgb3.GetARgb()[0]);
            Assert.AreEqual(255, rgb3.GetARgb()[1]);
            Assert.AreEqual(255, rgb3.GetARgb()[2]);
            Assert.AreEqual(255, rgb3.GetARgb()[3]);

            // Tint doesn't have the alpha
            // tint = -0.34999
            // 255 * (1 + tint) = 165 truncated
            // or (byte) -91 (which is 165 - 256)
            Assert.AreEqual(3, rgb3.GetRgbWithTint().Length);
            Assert.AreEqual(-91, (sbyte)rgb3.GetRgbWithTint()[0]);
            Assert.AreEqual(-91, (sbyte)rgb3.GetRgbWithTint()[1]);
            Assert.AreEqual(-91, (sbyte)rgb3.GetRgbWithTint()[2]);

            // Set the colour to black, will Get translated internally
            // (Excel stores 3 colour white and black wrong!)
            // Set the color to black (no theme).
            rgb3.SetRgb(new byte[] { 0, 0, 0 });
            Assert.AreEqual("FF000000", rgb3.GetARGBHex());
            Assert.AreEqual(0, rgb3.GetCTColor().GetRgb()[0]);
            Assert.AreEqual(0, rgb3.GetCTColor().GetRgb()[1]);
            Assert.AreEqual(0, rgb3.GetCTColor().GetRgb()[2]);

            // Set another, is fine
            rgb3.SetRgb(new byte[] { 16, 17, 18 });
            Assert.AreEqual("FF101112", rgb3.GetARGBHex());
            Assert.AreEqual(0x10, rgb3.GetCTColor().GetRgb()[0]);
            Assert.AreEqual(0x11, rgb3.GetCTColor().GetRgb()[1]);
            Assert.AreEqual(0x12, rgb3.GetCTColor().GetRgb()[2]);
        }