public void TestRgbColor() { CT_Font ctFont = new CT_Font(); CT_Color color = ctFont.AddNewColor(); //Integer.toHexString(0xFFFFFF).getBytes() = [102, 102, 102, 102, 102, 102] color.SetRgb(Encoding.ASCII.GetBytes("ffffff")); ctFont.SetColorArray(0, color); XSSFFont xssfFont = new XSSFFont(ctFont); Assert.AreEqual(ctFont.GetColorArray(0).GetRgb()[0], xssfFont.GetXSSFColor().RGB[0]); Assert.AreEqual(ctFont.GetColorArray(0).GetRgb()[1], xssfFont.GetXSSFColor().RGB[1]); Assert.AreEqual(ctFont.GetColorArray(0).GetRgb()[2], xssfFont.GetXSSFColor().RGB[2]); Assert.AreEqual(ctFont.GetColorArray(0).GetRgb()[3], xssfFont.GetXSSFColor().RGB[3]); xssfFont.Color = ((short)23); byte[] bytes = Encoding.ASCII.GetBytes(HexDump.ToHex(0xF1F1F1)); color.rgb = (bytes); XSSFColor newColor = new XSSFColor(color); xssfFont.SetColor(newColor); Assert.AreEqual(ctFont.GetColorArray(0).GetRgb()[2], newColor.RGB[2]); CollectionAssert.AreEqual(bytes, xssfFont.GetXSSFColor().RGB); Assert.AreEqual(0, xssfFont.Color); }
public bool CompareWithXSSFFont(XSSFFont font) { if (FontName != font.FontName) { return(false); } if (FontWeight > 400 != font.IsBold) { return(false); } if (Math.Abs(FontSize - font.FontHeightInPoints) > 0.01) { return(false); } if (IsUnderline && font.Underline == FontUnderlineType.None) { return(false); } if (IsStrikeout != font.IsStrikeout) { return(false); } if (IsItalic != font.IsItalic) { return(false); } if (!CompareColor(FontColor, font.GetXSSFColor())) { return(false); } return(true); }
public void TestThemesTableColors() { // Load our two test workbooks XSSFWorkbook simple = XSSFTestDataSamples.OpenSampleWorkbook(testFileSimple); XSSFWorkbook complex = XSSFTestDataSamples.OpenSampleWorkbook(testFileComplex); // Save and re-load them, to check for stability across that XSSFWorkbook simpleRS = XSSFTestDataSamples.WriteOutAndReadBack(simple) as XSSFWorkbook; XSSFWorkbook complexRS = XSSFTestDataSamples.WriteOutAndReadBack(complex) as XSSFWorkbook; // Fetch fresh copies to test with simple = XSSFTestDataSamples.OpenSampleWorkbook(testFileSimple); complex = XSSFTestDataSamples.OpenSampleWorkbook(testFileComplex); // Files and descriptions Dictionary <String, XSSFWorkbook> workbooks = new Dictionary <String, XSSFWorkbook>(); workbooks.Add(testFileSimple, simple); workbooks.Add("Re-Saved_" + testFileSimple, simpleRS); workbooks.Add(testFileComplex, complex); workbooks.Add("Re-Saved_" + testFileComplex, complexRS); // Sanity check //Assert.AreEqual(rgbExpected.Length, rgbExpected.Length); // For offline testing bool createFiles = false; // Check each workbook in turn, and verify that the colours // for the theme-applied cells in Column A are correct foreach (String whatWorkbook in workbooks.Keys) { XSSFWorkbook workbook = workbooks[whatWorkbook]; XSSFSheet sheet = workbook.GetSheetAt(0) as XSSFSheet; int startRN = 0; if (whatWorkbook.EndsWith(testFileComplex)) { startRN++; } for (int rn = startRN; rn < rgbExpected.Length + startRN; rn++) { XSSFRow row = sheet.GetRow(rn) as XSSFRow; Assert.IsNotNull(row, "Missing row " + rn + " in " + whatWorkbook); String ref1 = (new CellReference(rn, 0)).FormatAsString(); XSSFCell cell = row.GetCell(0) as XSSFCell; Assert.IsNotNull(cell, "Missing cell " + ref1 + " in " + whatWorkbook); int expectedThemeIdx = rn - startRN; ThemeElement themeElem = ThemeElement.ById(expectedThemeIdx); Assert.AreEqual(themeElem.name.ToLower(), cell.StringCellValue, "Wrong theme at " + ref1 + " in " + whatWorkbook); // Fonts are theme-based in their colours XSSFFont font = (cell.CellStyle as XSSFCellStyle).GetFont(); CT_Color ctColor = font.GetCTFont().GetColorArray(0); Assert.IsNotNull(ctColor); Assert.AreEqual(true, ctColor.IsSetTheme()); Assert.AreEqual(themeElem.idx, ctColor.theme); // Get the colour, via the theme XSSFColor color = font.GetXSSFColor(); // Theme colours aren't tinted Assert.AreEqual(color.HasTint, false); // Check the RGB part (no tint) Assert.AreEqual(rgbExpected[expectedThemeIdx], HexDump.EncodeHexString(color.RGB), "Wrong theme colour " + themeElem.name + " on " + whatWorkbook); long themeIdx = font.GetCTFont().GetColorArray(0).theme; Assert.AreEqual(expectedThemeIdx, themeIdx, "Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook ); if (createFiles) { XSSFCellStyle cs = row.Sheet.Workbook.CreateCellStyle() as XSSFCellStyle; cs.SetFillForegroundColor(color); cs.FillPattern = FillPatternType.SolidForeground; row.CreateCell(1).CellStyle = (cs); } } if (createFiles) { FileStream fos = new FileStream("Generated_" + whatWorkbook, FileMode.Create, FileAccess.ReadWrite); workbook.Write(fos); fos.Close(); } } }