Example #1
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.IsFalse(rgb3.HasAlpha);
            Assert.IsTrue(rgb3.HasTint);

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

            Assert.AreEqual(4, rgb3.ARGB.Length);
            Assert.AreEqual(255, rgb3.ARGB[0]);
            Assert.AreEqual(255, rgb3.ARGB[1]);
            Assert.AreEqual(255, rgb3.ARGB[2]);
            Assert.AreEqual(255, rgb3.ARGB[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.ARGBHex);
            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.IsFalse(rgb3.HasAlpha);
            Assert.AreEqual("FF101112", rgb3.ARGBHex);
            Assert.AreEqual(0x10, rgb3.GetCTColor().GetRgb()[0]);
            Assert.AreEqual(0x11, rgb3.GetCTColor().GetRgb()[1]);
            Assert.AreEqual(0x12, rgb3.GetCTColor().GetRgb()[2]);
        }
Example #2
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.RGB);
            Assert.AreEqual(null, indexed.GetRgbWithTint());
            Assert.AreEqual(null, indexed.ARGBHex);
            Assert.IsFalse(indexed.HasAlpha);
            Assert.IsFalse(indexed.HasTint);

            // 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.ARGBHex);

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

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

            // You don't Get tinted indexed colours, sorry...
            Assert.AreEqual(null, indexed.GetRgbWithTint());
        }
Example #3
0
        public void TestARGBColour()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("48779.xlsx");

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

            Assert.AreEqual(false, rgb4.GetCTColor().IsSetIndexed());
            Assert.AreEqual(0, rgb4.GetCTColor().indexed);
            Assert.AreEqual(true, rgb4.GetCTColor().IsSetRgb());
            Assert.AreEqual(4, rgb4.GetCTColor().GetRgb().Length);

            // Now check the XSSFColor
            Assert.AreEqual(0, rgb4.Indexed);
            Assert.AreEqual(0.0, rgb4.Tint);
            Assert.IsFalse(rgb4.HasTint);
            Assert.IsTrue(rgb4.HasAlpha);

            Assert.AreEqual("FFFF0000", rgb4.ARGBHex);
            Assert.AreEqual(3, rgb4.RGB.Length);
            Assert.AreEqual(255, rgb4.RGB[0]);
            Assert.AreEqual(0, rgb4.RGB[1]);
            Assert.AreEqual(0, rgb4.RGB[2]);

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

            // Tint doesn't have the alpha
            Assert.AreEqual(3, rgb4.GetRgbWithTint().Length);
            Assert.AreEqual(255, rgb4.GetRgbWithTint()[0]);
            Assert.AreEqual(0, rgb4.GetRgbWithTint()[1]);
            Assert.AreEqual(0, rgb4.GetRgbWithTint()[2]);


            // Turn on tinting, and check it behaves
            // TODO These values are suspected to be wrong...
            rgb4.Tint = (0.4);
            Assert.IsTrue(rgb4.HasTint);
            Assert.AreEqual(0.4, rgb4.Tint);

            Assert.AreEqual(3, rgb4.GetRgbWithTint().Length);
            Assert.AreEqual(255, rgb4.GetRgbWithTint()[0]);
            Assert.AreEqual(102, rgb4.GetRgbWithTint()[1]);
            Assert.AreEqual(102, rgb4.GetRgbWithTint()[2]);
        }
Example #4
0
        public void TestColorFromTheme()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("styles.xlsx");
            XSSFCell     cellWithThemeColor = (XSSFCell)wb.GetSheetAt(0).GetRow(10).GetCell(0);
            //color RGB will be extracted from theme
            XSSFColor foregroundColor = (XSSFColor)((XSSFCellStyle)cellWithThemeColor.CellStyle).FillForegroundColorColor;

            byte[] rgb = foregroundColor.GetRgb();
            Assert.AreEqual(rgb[0], 238);
            Assert.AreEqual(rgb[1], 236);
            Assert.AreEqual(rgb[2], 225);
            byte[] rgbWithTint = foregroundColor.GetRgbWithTint();
            Assert.AreEqual(rgbWithTint[0], 244);
            Assert.AreEqual(rgbWithTint[1], 243);
            Assert.AreEqual(rgbWithTint[2], 236);
        }
Example #5
0
        public void TestColorFromTheme()
        {
            XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("styles.xlsx");
            XSSFCell     cellWithThemeColor = (XSSFCell)wb.GetSheetAt(0).GetRow(10).GetCell(0);
            //color RGB will be extracted from theme
            XSSFColor foregroundColor = (XSSFColor)((XSSFCellStyle)cellWithThemeColor.CellStyle).FillForegroundColorColor;

            byte[] rgb         = foregroundColor.GetRgb();
            byte[] rgbWithTint = foregroundColor.GetRgbWithTint();
            // Dk2
            Assert.AreEqual(rgb[0], 31);
            Assert.AreEqual(rgb[1], 73);
            Assert.AreEqual(rgb[2], 125);
            // Dk2, lighter 40% (tint is about 0.39998)
            // 31 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 120.59552 => 120 (byte)
            // 73 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 145.79636 => -111 (byte)
            // 125 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 176.99740 => -80 (byte)
            Assert.AreEqual(rgbWithTint[0], 120);
            Assert.AreEqual((sbyte)rgbWithTint[1], -111);
            Assert.AreEqual((sbyte)rgbWithTint[2], -80);
        }