1 package de.java2html.util; 2 3 8 public class RGB { 9 public static final RGB MAGENTA = new RGB(255, 0, 255); 10 public static final RGB GREEN = new RGB(0, 255, 0); 11 public static final RGB BLACK = new RGB(0, 0, 0); 12 public static final RGB RED = new RGB(255, 0, 0); 13 public static final RGB WHITE = new RGB(255, 255, 255); 14 public static final RGB ORANGE = new RGB(255, 200, 0); 15 public final static RGB CYAN = new RGB(0, 255, 255); 16 public final static RGB BLUE = new RGB(0, 0, 255); 17 public final static RGB LIGHT_GRAY = new RGB(192, 192, 192); 18 public final static RGB GRAY = new RGB(128, 128, 128); 19 public final static RGB DARK_GRAY = new RGB(64, 64, 64); 20 public final static RGB YELLOW = new RGB(255, 255, 0); 21 public final static RGB PINK = new RGB(255, 175, 175); 22 23 private int red; 24 private int green; 25 private int blue; 26 27 public RGB(int red, int green, int blue) { 28 assertColorValueRange(red, green, blue); 29 this.red = red; 30 this.green = green; 31 this.blue = blue; 32 } 33 34 private static void assertColorValueRange(int red, int green, int blue) { 35 boolean rangeError = false; 36 String badComponentString = ""; if (red < 0 || red > 255) { 38 rangeError = true; 39 badComponentString = badComponentString + " Red"; } 41 if (green < 0 || green > 255) { 42 rangeError = true; 43 badComponentString = badComponentString + " Green"; } 45 if (blue < 0 || blue > 255) { 46 rangeError = true; 47 badComponentString = badComponentString + " Blue"; } 49 if (rangeError == true) { 50 throw new IllegalArgumentException ("Color parameter outside of expected range:" + badComponentString); 52 } 53 } 54 55 public int getRed() { 56 return red; 57 } 58 59 public int getGreen() { 60 return green; 61 } 62 63 public int getBlue() { 64 return blue; 65 } 66 67 public boolean equals(Object obj) { 68 if (!(obj instanceof RGB)) { 69 return false; 70 } 71 RGB other = (RGB) obj; 72 return other.getRed() == getRed() 73 && other.getGreen() == getGreen() 74 && other.getBlue() == getBlue(); 75 } 76 77 public int hashCode() { 78 return (red & 0xFF) << 16 | (green & 0xFF) << 8 | blue & 0xFF; 79 } 80 81 87 public String toString () { 88 return "RGB {" + red + ", " + green + ", " + blue + "}"; } 90 } | Popular Tags |