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.

PaletteRecord.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.hssf.record;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.common.usermodel.GenericRecord;
  21. import org.apache.poi.util.GenericRecordUtil;
  22. import org.apache.poi.util.LittleEndianOutput;
  23. /**
  24. * Supports custom palettes.
  25. */
  26. public final class PaletteRecord extends StandardRecord {
  27. public static final short sid = 0x0092;
  28. /** The standard size of an XLS palette */
  29. public static final byte STANDARD_PALETTE_SIZE = (byte) 56;
  30. /** The byte index of the first color */
  31. public static final short FIRST_COLOR_INDEX = (short) 0x8;
  32. private static final int[] DEFAULT_COLORS = {
  33. 0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF,
  34. 0x800000, 0x008000, 0x000080, 0x808000, 0x800080, 0x008080, 0xC0C0C0, 0x808080,
  35. 0x9999FF, 0x993366, 0xFFFFCC, 0xCCFFFF, 0x660066, 0xFF8080, 0x0066CC, 0xCCCCFF,
  36. 0x000080, 0xFF00FF, 0xFFFF00, 0x00FFFF, 0x800080, 0x800000, 0x008080, 0x0000FF,
  37. 0x00CCFF, 0xCCFFFF, 0xCCFFCC, 0xFFFF99, 0x99CCFF, 0xFF99CC, 0xCC99FF, 0xFFCC99,
  38. 0x3366FF, 0x33CCCC, 0x99CC00, 0xFFCC00, 0xFF9900, 0xFF6600, 0x666699, 0x969696,
  39. 0x003366, 0x339966, 0x003300, 0x333300, 0x993300, 0x993366, 0x333399, 0x333333
  40. };
  41. private final ArrayList<PColor> _colors = new ArrayList<>(100);
  42. public PaletteRecord() {
  43. Arrays.stream(DEFAULT_COLORS).mapToObj(PColor::new).forEach(_colors::add);
  44. }
  45. public PaletteRecord(PaletteRecord other) {
  46. super(other);
  47. _colors.ensureCapacity(other._colors.size());
  48. other._colors.stream().map(PColor::new).forEach(_colors::add);
  49. }
  50. public PaletteRecord(RecordInputStream in) {
  51. int field_1_numcolors = in.readShort();
  52. _colors.ensureCapacity(field_1_numcolors);
  53. for (int k = 0; k < field_1_numcolors; k++) {
  54. _colors.add(new PColor(in));
  55. }
  56. }
  57. @Override
  58. public void serialize(LittleEndianOutput out) {
  59. out.writeShort(_colors.size());
  60. for (PColor color : _colors) {
  61. color.serialize(out);
  62. }
  63. }
  64. @Override
  65. protected int getDataSize() {
  66. return 2 + _colors.size() * PColor.ENCODED_SIZE;
  67. }
  68. @Override
  69. public short getSid() {
  70. return sid;
  71. }
  72. /**
  73. * Returns the color value at a given index
  74. *
  75. * @param byteIndex palette index, must be &gt;= 0x8
  76. *
  77. * @return the RGB triplet for the color, or <code>null</code> if the specified index
  78. * does not exist
  79. */
  80. public byte[] getColor(int byteIndex) {
  81. int i = byteIndex - FIRST_COLOR_INDEX;
  82. if (i < 0 || i >= _colors.size()) {
  83. return null;
  84. }
  85. return _colors.get(i).getTriplet();
  86. }
  87. /**
  88. * Sets the color value at a given index
  89. *
  90. * If the given index is greater than the current last color index,
  91. * then black is inserted at every index required to make the palette continuous.
  92. *
  93. * @param byteIndex the index to set; if this index is less than 0x8 or greater than
  94. * 0x40, then no modification is made
  95. * @param red the red color part
  96. * @param green the green color part
  97. * @param blue the blue color part
  98. */
  99. public void setColor(short byteIndex, byte red, byte green, byte blue)
  100. {
  101. int i = byteIndex - FIRST_COLOR_INDEX;
  102. if (i < 0 || i >= STANDARD_PALETTE_SIZE)
  103. {
  104. return;
  105. }
  106. // may need to grow - fill intervening palette entries with black
  107. while (_colors.size() <= i) {
  108. _colors.add(new PColor(0, 0, 0));
  109. }
  110. PColor custColor = new PColor(red, green, blue);
  111. _colors.set(i, custColor);
  112. }
  113. @Override
  114. public PaletteRecord copy() {
  115. return new PaletteRecord(this);
  116. }
  117. @Override
  118. public HSSFRecordTypes getGenericRecordType() {
  119. return HSSFRecordTypes.PALETTE;
  120. }
  121. @Override
  122. public Map<String, Supplier<?>> getGenericProperties() {
  123. return GenericRecordUtil.getGenericProperties("colors", () -> _colors);
  124. }
  125. /**
  126. * PColor - element in the list of colors
  127. */
  128. private static final class PColor implements GenericRecord {
  129. public static final short ENCODED_SIZE = 4;
  130. private final int _red;
  131. private final int _green;
  132. private final int _blue;
  133. PColor(int rgb) {
  134. _red = (rgb >>> 16) & 0xFF;
  135. _green = (rgb >>> 8) & 0xFF;
  136. _blue = rgb & 0xFF;
  137. }
  138. PColor(int red, int green, int blue) {
  139. _red = red;
  140. _green = green;
  141. _blue = blue;
  142. }
  143. PColor(PColor other) {
  144. _red = other._red;
  145. _green = other._green;
  146. _blue = other._blue;
  147. }
  148. PColor(RecordInputStream in) {
  149. _red = in.readByte();
  150. _green = in.readByte();
  151. _blue = in.readByte();
  152. in.readByte(); // unused
  153. }
  154. byte[] getTriplet() {
  155. return new byte[] { (byte) _red, (byte) _green, (byte) _blue };
  156. }
  157. void serialize(LittleEndianOutput out) {
  158. out.writeByte(_red);
  159. out.writeByte(_green);
  160. out.writeByte(_blue);
  161. out.writeByte(0);
  162. }
  163. @Override
  164. public Map<String, Supplier<?>> getGenericProperties() {
  165. return GenericRecordUtil.getGenericProperties(
  166. "red", () -> _red & 0xFF,
  167. "green", () -> _green & 0xFF,
  168. "blue", () -> _blue & 0xFF
  169. );
  170. }
  171. }
  172. }