You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFCellFill.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel.extensions;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.IOException;
  19. import org.apache.poi.ss.usermodel.FillPatternType;
  20. import org.apache.poi.xssf.XSSFTestDataSamples;
  21. import org.apache.poi.xssf.usermodel.XSSFCell;
  22. import org.apache.poi.xssf.usermodel.XSSFColor;
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  24. import org.junit.Test;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  29. public class TestXSSFCellFill {
  30. @Test
  31. public void testGetFillBackgroundColor() {
  32. CTFill ctFill = CTFill.Factory.newInstance();
  33. XSSFCellFill cellFill = new XSSFCellFill(ctFill, null);
  34. CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
  35. CTColor bgColor = ctPatternFill.addNewBgColor();
  36. assertNotNull(cellFill.getFillBackgroundColor());
  37. bgColor.setIndexed(2);
  38. assertEquals(2, cellFill.getFillBackgroundColor().getIndexed());
  39. }
  40. @Test
  41. public void testGetFillForegroundColor() {
  42. CTFill ctFill = CTFill.Factory.newInstance();
  43. XSSFCellFill cellFill = new XSSFCellFill(ctFill, null);
  44. CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
  45. CTColor fgColor = ctPatternFill.addNewFgColor();
  46. assertNotNull(cellFill.getFillForegroundColor());
  47. fgColor.setIndexed(8);
  48. assertEquals(8, cellFill.getFillForegroundColor().getIndexed());
  49. }
  50. @Test
  51. public void testGetSetPatternType() {
  52. CTFill ctFill = CTFill.Factory.newInstance();
  53. XSSFCellFill cellFill = new XSSFCellFill(ctFill, null);
  54. CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
  55. ctPatternFill.setPatternType(STPatternType.SOLID);
  56. assertEquals(FillPatternType.SOLID_FOREGROUND.ordinal(), cellFill.getPatternType().intValue()-1);
  57. }
  58. @Test
  59. public void testGetNotModifies() {
  60. CTFill ctFill = CTFill.Factory.newInstance();
  61. XSSFCellFill cellFill = new XSSFCellFill(ctFill, null);
  62. CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
  63. ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
  64. assertEquals(8, cellFill.getPatternType().intValue());
  65. }
  66. @Test
  67. public void testColorFromTheme() throws IOException {
  68. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
  69. XSSFCell cellWithThemeColor = wb.getSheetAt(0).getRow(10).getCell(0);
  70. //color RGB will be extracted from theme
  71. XSSFColor foregroundColor = cellWithThemeColor.getCellStyle().getFillForegroundXSSFColor();
  72. byte[] rgb = foregroundColor.getRGB();
  73. byte[] rgbWithTint = foregroundColor.getRGBWithTint();
  74. // Dk2
  75. assertEquals(rgb[0],31);
  76. assertEquals(rgb[1],73);
  77. assertEquals(rgb[2],125);
  78. // Dk2, lighter 40% (tint is about 0.39998)
  79. // 31 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 120.59552 => 120 (byte)
  80. // 73 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 145.79636 => -111 (byte)
  81. // 125 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 176.99740 => -80 (byte)
  82. assertEquals(rgbWithTint[0],120);
  83. assertEquals(rgbWithTint[1],-111);
  84. assertEquals(rgbWithTint[2],-80);
  85. wb.close();
  86. }
  87. }