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.

HSSFExtendedColor.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 static org.apache.poi.hssf.record.common.ExtendedColor.TYPE_AUTO;
  17. import static org.apache.poi.hssf.record.common.ExtendedColor.TYPE_INDEXED;
  18. import static org.apache.poi.hssf.record.common.ExtendedColor.TYPE_RGB;
  19. import static org.apache.poi.hssf.record.common.ExtendedColor.TYPE_THEMED;
  20. import org.apache.poi.hssf.util.HSSFColor;
  21. import org.apache.poi.ss.usermodel.ExtendedColor;
  22. /**
  23. * The HSSF file format normally stores Color information in the
  24. * Palette (see PaletteRecord), but for a few cases (eg Conditional
  25. * Formatting, Sheet Extensions), this XSSF-style color record
  26. * can be used.
  27. */
  28. public class HSSFExtendedColor extends ExtendedColor {
  29. private org.apache.poi.hssf.record.common.ExtendedColor color;
  30. public HSSFExtendedColor(org.apache.poi.hssf.record.common.ExtendedColor color) {
  31. this.color = color;
  32. }
  33. protected org.apache.poi.hssf.record.common.ExtendedColor getExtendedColor() {
  34. return color;
  35. }
  36. public boolean isAuto() {
  37. return color.getType() == TYPE_AUTO;
  38. }
  39. public boolean isIndexed() {
  40. return color.getType() == TYPE_INDEXED;
  41. }
  42. public boolean isRGB() {
  43. return color.getType() == TYPE_RGB;
  44. }
  45. public boolean isThemed() {
  46. return color.getType() == TYPE_THEMED;
  47. }
  48. public short getIndex() {
  49. return (short)color.getColorIndex();
  50. }
  51. public int getTheme() {
  52. return color.getThemeIndex();
  53. }
  54. public byte[] getRGB() {
  55. // Trim trailing A
  56. byte[] rgb = new byte[3];
  57. byte[] rgba = color.getRGBA();
  58. if (rgba == null) return null;
  59. System.arraycopy(rgba, 0, rgb, 0, 3);
  60. return rgb;
  61. }
  62. public byte[] getARGB() {
  63. // Swap from RGBA to ARGB
  64. byte[] argb = new byte[4];
  65. byte[] rgba = color.getRGBA();
  66. if (rgba == null) return null;
  67. System.arraycopy(rgba, 0, argb, 1, 3);
  68. argb[0] = rgba[3];
  69. return argb;
  70. }
  71. protected byte[] getStoredRBG() {
  72. return getARGB();
  73. }
  74. public void setRGB(byte[] rgb) {
  75. if (rgb.length == 3) {
  76. byte[] rgba = new byte[4];
  77. System.arraycopy(rgb, 0, rgba, 0, 3);
  78. rgba[3] = -1;
  79. } else {
  80. // Shuffle from ARGB to RGBA
  81. byte a = rgb[0];
  82. rgb[0] = rgb[1];
  83. rgb[1] = rgb[2];
  84. rgb[2] = rgb[3];
  85. rgb[3] = a;
  86. color.setRGBA(rgb);
  87. }
  88. color.setType(TYPE_RGB);
  89. }
  90. public double getTint() {
  91. return color.getTint();
  92. }
  93. public void setTint(double tint) {
  94. color.setTint(tint);
  95. }
  96. protected byte[] getIndexedRGB() {
  97. if (isIndexed() && getIndex() > 0) {
  98. int indexNum = getIndex();
  99. HSSFColor indexed = HSSFColor.getIndexHash().get(indexNum);
  100. if (indexed != null) {
  101. byte[] rgb = new byte[3];
  102. rgb[0] = (byte) indexed.getTriplet()[0];
  103. rgb[1] = (byte) indexed.getTriplet()[1];
  104. rgb[2] = (byte) indexed.getTriplet()[2];
  105. return rgb;
  106. }
  107. } // else
  108. return null;
  109. }
  110. }