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.

HSSFPalette.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.usermodel;
  16. import java.util.Locale;
  17. import org.apache.poi.hssf.record.PaletteRecord;
  18. import org.apache.poi.hssf.util.HSSFColor;
  19. import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
  20. /**
  21. * Represents a workbook color palette.
  22. * Internally, the XLS format refers to colors using an offset into the palette
  23. * record. Thus, the first color in the palette has the index 0x8, the second
  24. * has the index 0x9, etc. through 0x40
  25. */
  26. public final class HSSFPalette {
  27. private PaletteRecord _palette;
  28. protected HSSFPalette(PaletteRecord palette)
  29. {
  30. _palette = palette;
  31. }
  32. /**
  33. * Retrieves the color at a given index
  34. *
  35. * @param index the palette index, between 0x8 to 0x40 inclusive
  36. * @return the color, or null if the index is not populated
  37. */
  38. public HSSFColor getColor(short index)
  39. {
  40. //Handle the special AUTOMATIC case
  41. if (index == HSSFColorPredefined.AUTOMATIC.getIndex()) {
  42. return HSSFColorPredefined.AUTOMATIC.getColor();
  43. }
  44. byte[] b = _palette.getColor(index);
  45. return (b == null) ? null : new CustomColor(index, b);
  46. }
  47. /**
  48. * Retrieves the color at a given index
  49. *
  50. * @param index the palette index, between 0x8 to 0x40 inclusive
  51. * @return the color, or null if the index is not populated
  52. */
  53. public HSSFColor getColor(int index) {
  54. return getColor((short)index);
  55. }
  56. /**
  57. * Finds the first occurrence of a given color
  58. *
  59. * @param red the RGB red component, between 0 and 255 inclusive
  60. * @param green the RGB green component, between 0 and 255 inclusive
  61. * @param blue the RGB blue component, between 0 and 255 inclusive
  62. * @return the color, or null if the color does not exist in this palette
  63. */
  64. public HSSFColor findColor(byte red, byte green, byte blue)
  65. {
  66. byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
  67. for (short i = PaletteRecord.FIRST_COLOR_INDEX; b != null;
  68. b = _palette.getColor(++i))
  69. {
  70. if (b[0] == red && b[1] == green && b[2] == blue)
  71. {
  72. return new CustomColor(i, b);
  73. }
  74. }
  75. return null;
  76. }
  77. /**
  78. * Finds the closest matching color in the custom palette. The
  79. * method for finding the distance between the colors is fairly
  80. * primative.
  81. *
  82. * @param red The red component of the color to match.
  83. * @param green The green component of the color to match.
  84. * @param blue The blue component of the color to match.
  85. * @return The closest color or null if there are no custom
  86. * colors currently defined.
  87. */
  88. public HSSFColor findSimilarColor(byte red, byte green, byte blue) {
  89. return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue));
  90. }
  91. /**
  92. * Finds the closest matching color in the custom palette. The
  93. * method for finding the distance between the colors is fairly
  94. * primative.
  95. *
  96. * @param red The red component of the color to match.
  97. * @param green The green component of the color to match.
  98. * @param blue The blue component of the color to match.
  99. * @return The closest color or null if there are no custom
  100. * colors currently defined.
  101. */
  102. public HSSFColor findSimilarColor(int red, int green, int blue) {
  103. HSSFColor result = null;
  104. int minColorDistance = Integer.MAX_VALUE;
  105. byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
  106. for (short i = PaletteRecord.FIRST_COLOR_INDEX; b != null;
  107. b = _palette.getColor(++i))
  108. {
  109. int colorDistance = Math.abs(red - unsignedInt(b[0])) +
  110. Math.abs(green - unsignedInt(b[1])) +
  111. Math.abs(blue - unsignedInt(b[2]));
  112. if (colorDistance < minColorDistance)
  113. {
  114. minColorDistance = colorDistance;
  115. result = getColor(i);
  116. }
  117. }
  118. return result;
  119. }
  120. /**
  121. * Turn a byte of between -127 and 127 into something between
  122. * 0 and 255, so distance calculations work as expected.
  123. */
  124. private int unsignedInt(byte b) {
  125. return 0xFF & b;
  126. }
  127. /**
  128. * Sets the color at the given offset
  129. *
  130. * @param index the palette index, between 0x8 to 0x40 inclusive
  131. * @param red the RGB red component, between 0 and 255 inclusive
  132. * @param green the RGB green component, between 0 and 255 inclusive
  133. * @param blue the RGB blue component, between 0 and 255 inclusive
  134. */
  135. public void setColorAtIndex(short index, byte red, byte green, byte blue)
  136. {
  137. _palette.setColor(index, red, green, blue);
  138. }
  139. /**
  140. * Adds a new color into an empty color slot.
  141. * @param red The red component
  142. * @param green The green component
  143. * @param blue The blue component
  144. *
  145. * @return The new custom color.
  146. *
  147. * @throws RuntimeException if there are more more free color indexes.
  148. */
  149. public HSSFColor addColor( byte red, byte green, byte blue )
  150. {
  151. byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
  152. short i;
  153. for (i = PaletteRecord.FIRST_COLOR_INDEX; i < PaletteRecord.STANDARD_PALETTE_SIZE + PaletteRecord.FIRST_COLOR_INDEX; b = _palette.getColor(++i))
  154. {
  155. if (b == null)
  156. {
  157. setColorAtIndex( i, red, green, blue );
  158. return getColor(i);
  159. }
  160. }
  161. throw new RuntimeException("Could not find free color index");
  162. }
  163. private static final class CustomColor extends HSSFColor {
  164. private short _byteOffset;
  165. private byte _red;
  166. private byte _green;
  167. private byte _blue;
  168. public CustomColor(short byteOffset, byte[] colors)
  169. {
  170. this(byteOffset, colors[0], colors[1], colors[2]);
  171. }
  172. private CustomColor(short byteOffset, byte red, byte green, byte blue)
  173. {
  174. _byteOffset = byteOffset;
  175. _red = red;
  176. _green = green;
  177. _blue = blue;
  178. }
  179. @Override
  180. public short getIndex()
  181. {
  182. return _byteOffset;
  183. }
  184. @Override
  185. public short[] getTriplet()
  186. {
  187. return new short[]
  188. {
  189. (short) (_red & 0xff),
  190. (short) (_green & 0xff),
  191. (short) (_blue & 0xff)
  192. };
  193. }
  194. @Override
  195. public String getHexString() {
  196. return getGnumericPart(_red) + ":" + getGnumericPart(_green) + ":" + getGnumericPart(_blue);
  197. }
  198. private String getGnumericPart(byte color)
  199. {
  200. String s;
  201. if (color == 0)
  202. {
  203. s = "0";
  204. }
  205. else
  206. {
  207. int c = color & 0xff; //as unsigned
  208. c = (c << 8) | c; //pad to 16-bit
  209. s = Integer.toHexString(c).toUpperCase(Locale.ROOT);
  210. while (s.length() < 4)
  211. {
  212. s = "0" + s;
  213. }
  214. }
  215. return s;
  216. }
  217. }
  218. }