Author: fanningpj
Date: Sun May 11 07:37:23 2025
New Revision: 1925491
URL: http://svn.apache.org/viewvc?rev=1925491&view=rev
Log:
[github-806] HSSFColor: Reduce use of java.awt.Color. Thanks to Jaroslav
Tulach. This closes #806
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/hssf/util/HSSFColor.java
poi/trunk/poi/src/test/java/org/apache/poi/hssf/util/TestHSSFColor.java
Modified: poi/trunk/poi/src/main/java/org/apache/poi/hssf/util/HSSFColor.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/hssf/util/HSSFColor.java?rev=1925491&r1=1925490&r2=1925491&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/hssf/util/HSSFColor.java
(original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/hssf/util/HSSFColor.java Sun May
11 07:37:23 2025
@@ -42,7 +42,7 @@ public class HSSFColor implements Color
private static Map<Integer,HSSFColor> indexHash;
private static Map<HSSFColorPredefined,HSSFColor> enumList;
- private final java.awt.Color color;
+ private final int rgb;
private final int index;
private final int index2;
@@ -110,7 +110,7 @@ public class HSSFColor implements Color
private final HSSFColor color;
HSSFColorPredefined(int index, int index2, int rgb) {
- this.color = new HSSFColor(index, index2, new java.awt.Color(rgb));
+ this.color = new HSSFColor(index, index2, rgb);
}
/**
@@ -145,7 +145,7 @@ public class HSSFColor implements Color
* @return (a copy of) the HSSFColor assigned to the enum
*/
public HSSFColor getColor() {
- return new HSSFColor(getIndex(), getIndex2(), color.color);
+ return new HSSFColor(getIndex(), getIndex2(), color.rgb);
}
}
@@ -153,13 +153,38 @@ public class HSSFColor implements Color
/** Creates a new instance of HSSFColor */
public HSSFColor() {
// automatic index
- this(0x40, -1, java.awt.Color.BLACK);
+ this(0x40, -1, 0x000000);
}
+ /** Constructs new instance of {@code HSSFColor} by
+ * extracting RGB from {@link java.awt.Color}. The code is equivalent
+ * to calling:
+ * <pre>
+ * new HSSFColor(index, index2, color.getRGB());
+ * </pre>
+ * or specifying {@link #HSSFColor(int, int, int) RGB directly}.
+ *
+ * @param index
+ * @param index2
+ * @param color color to extract RGB from
+ */
public HSSFColor(int index, int index2, java.awt.Color color) {
+ this(index, index2, color.getRGB());
+ }
+
+ /** Constructs new instance of {@code HSSFColor} by
+ * specifying RGB as an {@code int} value. Given {@code blue}, {@code
green} and
+ * {@code blue} being byte values between {@code 0x00 to 0xFF}, then
+ * {@code rgb = blue + (green >> 8) + (red >> 16)}.
+ * @param index
+ * @param index2
+ * @param rgb combined value of RGB
+ * @since POI 5.4.2
+ */
+ public HSSFColor(int index, int index2, int rgb) {
this.index = index;
this.index2 = index2;
- this.color = color;
+ this.rgb = 0xFF000000 | rgb;
}
/**
@@ -203,7 +228,7 @@ public class HSSFColor implements Color
}
/**
- * this function returns all colors in a hastable. It's not implemented
as a
+ * This function returns all colors in a map. It's not implemented as a
* static member/statically initialized because that would be dirty in a
* server environment as it is intended. This means you'll eat the time
* it takes to create it once per request but you will not hold onto it
@@ -235,7 +260,7 @@ public class HSSFColor implements Color
private static synchronized Map<HSSFColorPredefined,HSSFColor>
mapEnumToColorClass() {
if (enumList == null) {
enumList = new EnumMap<>(HSSFColorPredefined.class);
- // AUTOMATIC is not add to list
+ // AUTOMATIC is not added to list
addHSSFColorPredefined(HSSFColorPredefined.BLACK);
addHSSFColorPredefined(HSSFColorPredefined.BROWN);
addHSSFColorPredefined(HSSFColorPredefined.OLIVE_GREEN);
@@ -315,7 +340,7 @@ public class HSSFColor implements Color
*/
public short [] getTriplet() {
- return new short[] { (short)color.getRed(), (short)color.getGreen(),
(short)color.getBlue() };
+ return new short[] { getRed(), getGreen(), getBlue() };
}
/**
@@ -324,9 +349,19 @@ public class HSSFColor implements Color
*/
public String getHexString() {
- return (Integer.toHexString(color.getRed()*0x101) + ":" +
- Integer.toHexString(color.getGreen()*0x101) + ":" +
-
Integer.toHexString(color.getBlue()*0x101)).toUpperCase(Locale.ROOT);
+ return (Integer.toHexString(getRed()*0x101) + ":" +
+ Integer.toHexString(getGreen()*0x101) + ":" +
+ Integer.toHexString(getBlue()*0x101)).toUpperCase(Locale.ROOT);
+ }
+
+ private final short getBlue() {
+ return (short) (rgb & 0xFF);
+ }
+ private final short getGreen() {
+ return (short) ((rgb >> 8) & 0xFF);
+ }
+ private final short getRed() {
+ return (short) ((rgb >> 16) & 0xFF);
}
@Override
@@ -338,12 +373,12 @@ public class HSSFColor implements Color
if (index != hssfColor.index) return false;
if (index2 != hssfColor.index2) return false;
- return Objects.equals(color, hssfColor.color);
+ return Objects.equals(rgb, hssfColor.rgb);
}
@Override
public int hashCode() {
- return Objects.hash(color,index,index2);
+ return Objects.hash(rgb, index, index2);
}
/**
Modified:
poi/trunk/poi/src/test/java/org/apache/poi/hssf/util/TestHSSFColor.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/hssf/util/TestHSSFColor.java?rev=1925491&r1=1925490&r2=1925491&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/hssf/util/TestHSSFColor.java
(original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/hssf/util/TestHSSFColor.java Sun
May 11 07:37:23 2025
@@ -17,6 +17,7 @@
package org.apache.poi.hssf.util;
+import java.awt.Color;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -56,4 +57,15 @@ final class TestHSSFColor {
triplets.get(HSSFColorPredefined.YELLOW.getHexString())
);
}
+
+ @Test
+ void testRgbAndColorWorkTheSame() {
+ Color color = java.awt.Color.YELLOW;
+ @SuppressWarnings("deprecation")
+ HSSFColor fromColor = new HSSFColor(10, 20, color);
+ HSSFColor fromRgb = new HSSFColor(10, 20, color.getRGB());
+
+ assertEquals(fromColor, fromRgb, "Both colors are equal");
+ assertEquals(fromColor.hashCode(), fromRgb.hashCode(), "Both have the
same hash code");
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]