1
2 package org.freehep.graphics2d;
3
4 import java.awt.Color;
5 import java.awt.color.ColorSpace;
6
7
8
9
10
11
12
13 public class PrintColor extends Color {
14
15 public static final int COLOR = 0;
16
17 public static final int GRAYSCALE = 1;
18
19 public static final int BLACK_AND_WHITE = 2;
20
21 protected static Color[] defaultColors = { Color.RED, Color.GREEN,
22 Color.BLUE, Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.ORANGE,
23 Color.PINK, Color.WHITE, Color.LIGHT_GRAY, Color.GRAY,
24 Color.DARK_GRAY, Color.BLACK };
25
26 protected float asGray;
27
28 protected boolean asBlack;
29
30 private static void testColorValueRange(float asGray) {
31 boolean rangeError = false;
32 String badComponentString = "";
33 if (asGray < 0.0f || asGray > 1.0f) {
34 rangeError = true;
35 badComponentString = badComponentString + " asGray";
36 }
37 if (rangeError) {
38 throw new IllegalArgumentException(
39 "PrintColor parameter outside of expected range:"
40 + badComponentString);
41 }
42 }
43
44 public PrintColor(float red, float green, float blue, float asGray,
45 boolean asBlack) {
46 this(red, green, blue, 1.0f, asGray, asBlack);
47 }
48
49 public PrintColor(float red, float green, float blue, float alpha,
50 float asGray, boolean asBlack) {
51 super(red, green, blue, alpha);
52 this.asGray = asGray;
53 this.asBlack = asBlack;
54 testColorValueRange(asGray);
55 }
56
57 public PrintColor(Color color, float asGray, boolean asBlack) {
58 super(color.getRed(), color.getGreen(), color.getBlue(), color
59 .getAlpha());
60 this.asGray = asGray;
61 this.asBlack = asBlack;
62 testColorValueRange(asGray);
63 }
64
65 public float getAsGray() {
66 return asGray;
67 }
68
69 public boolean getAsBlack() {
70 return asBlack;
71 }
72
73 public PrintColor getColor(int mode) {
74
75 switch (mode) {
76 case COLOR:
77 return this;
78 case GRAYSCALE:
79 return new PrintColor(getAsGray(), getAsGray(), getAsGray(),
80 getAlpha() / 255.0f, getAsGray(), getAsBlack());
81 case BLACK_AND_WHITE:
82 if (getAsBlack()) {
83 return new PrintColor(Color.black, getAsGray(), getAsBlack());
84 } else {
85 return new PrintColor(Color.white, getAsGray(), getAsBlack());
86 }
87 default:
88 throw new IllegalArgumentException(
89 "ColorMode on PrintColor out of range: " + mode);
90 }
91 }
92
93 public static PrintColor createPrintColor(Color color) {
94 if (color == null) {
95 return null;
96 }
97
98 if (color instanceof PrintColor) {
99 return (PrintColor) color;
100 }
101
102
103
104 float[] gray = ColorSpace.getInstance(
105 ColorSpace.CS_GRAY).fromRGB(
106 color.getRGBComponents(null));
107 if (gray[0] == 0.0f) {
108 gray[0] = 1.0f;
109 } else if (gray[0] == 1.0f) {
110 gray[0] = 0.0f;
111 }
112 return new PrintColor(color, gray[0], !color.equals(Color.black));
113 }
114
115
116
117
118 public static Color getDefaultColor(int index) {
119 if ((index < 0) || (index >= defaultColors.length))
120 throw new IllegalArgumentException(
121 "PrintColor.getDefaultColor index outside of expected range: "
122 + index);
123 return createPrintColor(defaultColors[index]);
124 }
125
126
127 public static Color mixColor(Color c1, Color c2) {
128 int red = (c1.getRed() + c2.getRed()) / 2;
129 int green = (c1.getGreen() + c2.getGreen()) / 2;
130 int blue = (c1.getBlue() + c2.getBlue()) / 2;
131 return new Color(red, green, blue);
132 }
133
134 public int hashCode() {
135
136 return super.hashCode();
137 }
138
139 public boolean equals(Object obj) {
140 return super.equals(obj) && obj instanceof PrintColor
141 && ((PrintColor) obj).asGray == this.asGray
142 && ((PrintColor) obj).asBlack == this.asBlack;
143 }
144
145 public String toString() {
146 return super.toString() + ", asGray: " + asGray + ", asBlack: "
147 + asBlack;
148 }
149
150 public static PrintColor invert(Color color) {
151 PrintColor printColor = createPrintColor(color);
152 return new PrintColor(new Color(printColor.getRGB() ^ 0x00808080),
153 (printColor.getAsGray() + 0.5f) % 1.0f, !printColor
154 .getAsBlack());
155 }
156 }