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.

CustomIndexedColorMap.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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;
  16. import java.util.List;
  17. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColors;
  18. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRgbColor;
  19. /**
  20. * custom index color map, i.e. from the styles.xml definition
  21. */
  22. public class CustomIndexedColorMap implements IndexedColorMap {
  23. private final byte[][] colorIndex;
  24. /**
  25. * @param colors array of RGB triplets indexed by color index
  26. */
  27. private CustomIndexedColorMap(byte [][] colors) {
  28. this.colorIndex = colors;
  29. }
  30. public byte[] getRGB(int index) {
  31. if (colorIndex == null || index < 0 || index >= colorIndex.length) return null;
  32. return colorIndex[index];
  33. }
  34. /**
  35. * OOXML spec says if this exists it must have all indexes.
  36. * <p/>
  37. * From the OOXML Spec, Part 1, section 18.8.27:
  38. * <p/><i>
  39. * This element contains a sequence of RGB color values that correspond to color indexes (zero-based). When
  40. * using the default indexed color palette, the values are not written out, but instead are implied. When the color
  41. * palette has been modified from default, then the entire color palette is written out.
  42. * </i>
  43. * @param colors CTColors from styles.xml possibly defining a custom color indexing scheme
  44. * @return custom indexed color map or null if none defined in the document
  45. */
  46. public static CustomIndexedColorMap fromColors(CTColors colors) {
  47. if (colors == null || ! colors.isSetIndexedColors()) return null;
  48. List<CTRgbColor> rgbColorList = colors.getIndexedColors().getRgbColorList();
  49. byte[][] customColorIndex = new byte[rgbColorList.size()][3];
  50. for (int i=0; i < rgbColorList.size(); i++) {
  51. customColorIndex[i] = rgbColorList.get(i).getRgb();
  52. }
  53. return new CustomIndexedColorMap(customColorIndex);
  54. }
  55. }