01: package snow.utils.gui;
02:
03: import java.awt.*;
04:
05: public final class Colors {
06: private Colors() {
07: }
08:
09: /** Brighter or darker depending on the average grayscale.
10: * Brighter doesn't work... (HAS AN ERROR: black.brighter should give gray(128.128.128), as stated in the
11: * doc of Color, but it gives(3,3,3))
12: * @param percent use at least 30% to get good contrast.
13: */
14: public static Color createContrastedColor(final Color c,
15: float percent) {
16: if (percent < 0)
17: throw new IllegalArgumentException("Not in [0,100]: "
18: + percent);
19: if (percent > 100)
20: throw new IllegalArgumentException("Not in [0,100]: "
21: + percent);
22:
23: if (c == null)
24: return Color.lightGray;
25: if (Math.abs(percent) < 1e-4)
26: return c;
27:
28: int r = c.getRed();
29: int g = c.getGreen();
30: int b = c.getBlue();
31:
32: if (getAverageGrayScale(c) < 120) {
33: // c is near "black" (0,0,0) => move in brighter direction
34: return new Color(r + (int) ((255 - r) * percent / 100.0), g
35: + (int) ((255 - g) * percent / 100.0), b
36: + (int) ((255 - b) * percent / 100.0), c.getAlpha());
37: } else {
38: // c is near white(255, 255, 255) => reduce
39: return new Color((int) (r - r * percent / 100.0),
40: (int) (g - g * percent / 100.0), (int) (b - b
41: * percent / 100.0), c.getAlpha());
42: }
43: }
44:
45: // public static int interpolateBounded(int value, int i1, int i2,
46:
47: /** in [0,255].
48: * White: 255
49: * Black
50: */
51: public final static int getAverageGrayScale(final Color c) {
52: return (c.getRed() + c.getGreen() + c.getBlue()) / 3;
53: }
54:
55: }
|