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 8.3KB

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