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.

ColorGradientFormatting.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.cf;
  16. import org.apache.poi.util.BitField;
  17. import org.apache.poi.util.BitFieldFactory;
  18. import org.apache.poi.util.LittleEndianInput;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. import org.apache.poi.util.POILogFactory;
  21. import org.apache.poi.util.POILogger;
  22. /**
  23. * Color Gradient / Color Scale Conditional Formatting Rule Record.
  24. * (Called Color Gradient in the file format docs, but more commonly
  25. * Color Scale in the UI)
  26. */
  27. public final class ColorGradientFormatting implements Cloneable {
  28. private static POILogger log = POILogFactory.getLogger(ColorGradientFormatting.class);
  29. private byte options = 0;
  30. private Threshold[] thresholds;
  31. private byte[] colors; // TODO Decode
  32. private static BitField clamp = BitFieldFactory.getInstance(0x01);
  33. private static BitField background = BitFieldFactory.getInstance(0x02);
  34. public ColorGradientFormatting() {
  35. options = 3;
  36. thresholds = new Threshold[3];
  37. }
  38. public ColorGradientFormatting(LittleEndianInput in) {
  39. in.readShort(); // Ignored
  40. in.readByte(); // Reserved
  41. int numI = in.readByte();
  42. int numG = in.readByte();
  43. if (numI != numG) {
  44. log.log(POILogger.WARN, "Inconsistent Color Gradient defintion, found " + numI + " vs " + numG + " entries");
  45. }
  46. options = in.readByte();
  47. // TODO Are these correct?
  48. thresholds = new Threshold[numI];
  49. for (int i=0; i<thresholds.length; i++) {
  50. thresholds[i] = new Threshold(in);
  51. in.readDouble(); // Rather pointless value...
  52. }
  53. // TODO Decode colors
  54. colors = new byte[in.available()];
  55. in.readFully(colors);
  56. }
  57. public int getNumControlPoints() {
  58. return thresholds.length;
  59. }
  60. public void setNumControlPoints(int num) {
  61. if (num != thresholds.length) {
  62. thresholds = new Threshold[num];
  63. // TODO Colors
  64. }
  65. }
  66. public Threshold[] getThresholds() {
  67. return thresholds;
  68. }
  69. public void setThresholds(Threshold[] thresholds) {
  70. this.thresholds = thresholds;
  71. }
  72. // TODO Colors
  73. public boolean isClampToCurve() {
  74. return getOptionFlag(clamp);
  75. }
  76. public boolean isAppliesToBackground() {
  77. return getOptionFlag(background);
  78. }
  79. private boolean getOptionFlag(BitField field) {
  80. int value = field.getValue(options);
  81. return value==0 ? false : true;
  82. }
  83. public String toString() {
  84. StringBuffer buffer = new StringBuffer();
  85. buffer.append(" [Color Gradient Formatting]\n");
  86. buffer.append(" .clamp = ").append(isClampToCurve()).append("\n");
  87. buffer.append(" .background= ").append(isAppliesToBackground()).append("\n");
  88. for (Threshold t : thresholds) {
  89. buffer.append(t.toString());
  90. }
  91. buffer.append(" [/Color Gradient Formatting]\n");
  92. return buffer.toString();
  93. }
  94. public Object clone() {
  95. ColorGradientFormatting rec = new ColorGradientFormatting();
  96. rec.options = options;
  97. rec.thresholds = new Threshold[thresholds.length];
  98. System.arraycopy(thresholds, 0, rec.thresholds, 0, thresholds.length);
  99. // TODO Colors
  100. return rec;
  101. }
  102. public int getDataLength() {
  103. int len = 6;
  104. for (Threshold t : thresholds) {
  105. len += t.getDataLength();
  106. len += 8;
  107. }
  108. len += colors.length;
  109. return len;
  110. }
  111. public void serialize(LittleEndianOutput out) {
  112. out.writeShort(0);
  113. out.writeByte(0);
  114. out.writeByte(thresholds.length);
  115. out.writeByte(thresholds.length);
  116. out.writeByte(options);
  117. double step = 1d / (thresholds.length-1);
  118. for (int i=0; i<thresholds.length; i++) {
  119. Threshold t = thresholds[i];
  120. t.serialize(out);
  121. out.writeDouble(step*i);
  122. }
  123. out.write(colors);
  124. }
  125. }