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.

XSSFColor.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  17. import org.apache.poi.util.Internal;
  18. /**
  19. * Represents a color in SpreadsheetML
  20. */
  21. public class XSSFColor {
  22. private CTColor ctColor;
  23. /**
  24. * Create an instance of XSSFColor from the supplied XML bean
  25. */
  26. public XSSFColor(CTColor color) {
  27. this.ctColor = color;
  28. }
  29. /**
  30. * Create an new instance of XSSFColor
  31. */
  32. public XSSFColor() {
  33. this.ctColor = CTColor.Factory.newInstance();
  34. }
  35. public XSSFColor(java.awt.Color clr) {
  36. this();
  37. ctColor.setRgb(new byte[]{(byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
  38. }
  39. public XSSFColor(byte[] rgb) {
  40. this();
  41. ctColor.setRgb(rgb);
  42. }
  43. /**
  44. * A boolean value indicating the ctColor is automatic and system ctColor dependent.
  45. */
  46. public boolean isAuto() {
  47. return ctColor.getAuto();
  48. }
  49. /**
  50. * A boolean value indicating the ctColor is automatic and system ctColor dependent.
  51. */
  52. public void setAuto(boolean auto) {
  53. ctColor.setAuto(auto);
  54. }
  55. /**
  56. * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
  57. */
  58. public short getIndexed() {
  59. return (short)ctColor.getIndexed();
  60. }
  61. /**
  62. * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
  63. */
  64. public void setIndexed(int indexed) {
  65. ctColor.setIndexed(indexed);
  66. }
  67. /**
  68. * Standard Alpha Red Green Blue ctColor value (ARGB).
  69. */
  70. public byte[] getRgb() {
  71. return ctColor.getRgb();
  72. }
  73. /**
  74. * Standard Alpha Red Green Blue ctColor value (ARGB) with applied tint.
  75. */
  76. public byte[] getRgbWithTint() {
  77. byte[] rgb =ctColor.getRgb();
  78. for(int i = 0; i < rgb.length; i++){
  79. rgb[i] = applyTint(rgb[i] & 0xFF, ctColor.getTint());
  80. }
  81. return rgb;
  82. }
  83. private static byte applyTint(int lum, double tint){
  84. if(tint > 0){
  85. return (byte)(lum * (1.0-tint) + (255 - 255 * (1.0-tint)));
  86. } else if (tint < 0){
  87. return (byte)(lum*(1+tint));
  88. } else {
  89. return (byte)lum;
  90. }
  91. }
  92. /**
  93. * Standard Alpha Red Green Blue ctColor value (ARGB).
  94. */
  95. public void setRgb(byte[] rgb) {
  96. ctColor.setRgb(rgb);
  97. }
  98. /**
  99. * Index into the <clrScheme> collection, referencing a particular <sysClr> or
  100. * <srgbClr> value expressed in the Theme part.
  101. */
  102. public int getTheme() {
  103. return (int)ctColor.getTheme();
  104. }
  105. /**
  106. * Index into the <clrScheme> collection, referencing a particular <sysClr> or
  107. * <srgbClr> value expressed in the Theme part.
  108. */
  109. public void setTheme(int theme) {
  110. ctColor.setTheme(theme);
  111. }
  112. /**
  113. * Specifies the tint value applied to the ctColor.
  114. *
  115. * <p>
  116. * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
  117. * ctColor applied.
  118. * </p>
  119. * <p>
  120. * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
  121. * 1.0 means 100% lighten. Also, 0.0 means no change.
  122. * </p>
  123. * <p>
  124. * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where
  125. * HLSMAX is currently 255.
  126. * </p>
  127. * Here are some examples of how to apply tint to ctColor:
  128. * <blockquote>
  129. * <pre>
  130. * If (tint &lt; 0)
  131. * Lum' = Lum * (1.0 + tint)
  132. *
  133. * For example: Lum = 200; tint = -0.5; Darken 50%
  134. * Lum' = 200 * (0.5) =&gt; 100
  135. * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
  136. * Lum' = 200 * (1.0-1.0) =&gt; 0
  137. * If (tint &gt; 0)
  138. * Lum' = Lum * (1.0-tint) + (HLSMAX - HLSMAX * (1.0-tint))
  139. * For example: Lum = 100; tint = 0.75; Lighten 75%
  140. *
  141. * Lum' = 100 * (1-.75) + (HLSMAX - HLSMAX*(1-.75))
  142. * = 100 * .25 + (255 - 255 * .25)
  143. * = 25 + (255 - 63) = 25 + 192 = 217
  144. * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
  145. * Lum' = 100 * (1-1) + (HLSMAX - HLSMAX*(1-1))
  146. * = 100 * 0 + (255 - 255 * 0)
  147. * = 0 + (255 - 0) = 255
  148. * </pre>
  149. * </blockquote>
  150. *
  151. * @return the tint value
  152. */
  153. public double getTint() {
  154. return ctColor.getTint();
  155. }
  156. /**
  157. * Specifies the tint value applied to the ctColor.
  158. *
  159. * <p>
  160. * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
  161. * ctColor applied.
  162. * </p>
  163. * <p>
  164. * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
  165. * 1.0 means 100% lighten. Also, 0.0 means no change.
  166. * </p>
  167. * <p>
  168. * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where
  169. * HLSMAX is currently 255.
  170. * </p>
  171. * Here are some examples of how to apply tint to ctColor:
  172. * <blockquote>
  173. * <pre>
  174. * If (tint &lt; 0)
  175. * Lum' = Lum * (1.0 + tint)
  176. *
  177. * For example: Lum = 200; tint = -0.5; Darken 50%
  178. * Lum' = 200 * (0.5) =&gt; 100
  179. * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
  180. * Lum' = 200 * (1.0-1.0) =&gt; 0
  181. * If (tint &gt; 0)
  182. * Lum' = Lum * (1.0-tint) + (HLSMAX - HLSMAX * (1.0-tint))
  183. * For example: Lum = 100; tint = 0.75; Lighten 75%
  184. *
  185. * Lum' = 100 * (1-.75) + (HLSMAX - HLSMAX*(1-.75))
  186. * = 100 * .25 + (255 - 255 * .25)
  187. * = 25 + (255 - 63) = 25 + 192 = 217
  188. * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
  189. * Lum' = 100 * (1-1) + (HLSMAX - HLSMAX*(1-1))
  190. * = 100 * 0 + (255 - 255 * 0)
  191. * = 0 + (255 - 0) = 255
  192. * </pre>
  193. * </blockquote>
  194. *
  195. * @param tint the tint value
  196. */
  197. public void setTint(double tint) {
  198. ctColor.setTint(tint);
  199. }
  200. /**
  201. * Returns the underlying XML bean
  202. *
  203. * @return the underlying XML bean
  204. */
  205. @Internal
  206. public CTColor getCTColor(){
  207. return ctColor;
  208. }
  209. public int hashCode(){
  210. return ctColor.toString().hashCode();
  211. }
  212. public boolean equals(Object o){
  213. if(o == null || !(o instanceof XSSFColor)) return false;
  214. XSSFColor cf = (XSSFColor)o;
  215. return ctColor.toString().equals(cf.getCTColor().toString());
  216. }
  217. }