| 1 24 package info.monitorenter.gui.util; 25 26 35 public class HSBColor implements java.io.Serializable , Cloneable { 36 37 40 private static final long serialVersionUID = 3257288036910903863L; 41 42 57 public static HSBColor rgbToHSB(final java.awt.Color color) { 58 int rgb = color.getRGB(); 59 int r = (rgb & 0xFF0000) >> 16; 60 int g = (rgb & 0xFF00) >> 8; 61 int b = (rgb & 0xFF); 62 HSBColor ret = new HSBColor(); 63 64 int cmax = (r >= g) ? (r >= b) ? r : b : (g >= b) ? g : b; 65 int cmin = (r <= g) ? (r <= b) ? r : b : (g <= b) ? g : b; 66 ret.m_lum = ((float) cmax) / 255f; 67 if (cmax != cmin) { 68 float difference = (cmax - cmin); 69 ret.m_sat = difference / ((float) cmax); 70 if (r == cmax) { 71 ret.m_hue = (g - b) / difference; 72 } else if (g == cmax) { 73 ret.m_hue = (b - r) / difference + 2.0f; 74 } else { 75 ret.m_hue = (r - g) / difference + 4.0f; 76 } 77 ret.m_hue /= 6.0f; 78 if (ret.m_hue < 0) { 79 ret.m_hue += 1.0f; 80 } 81 } else { 82 ret.m_sat = 0; 83 ret.m_hue = 0; 84 } 85 return ret; 86 } 87 88 89 protected float m_hue; 90 91 92 protected float m_lum; 93 94 95 protected float m_sat; 96 97 101 private HSBColor() { 102 } 104 105 116 HSBColor(final float hue, final float saturation, final float brightness) { 117 this.m_hue = hue; 118 this.m_sat = saturation; 119 this.m_lum = brightness; 120 } 121 122 129 public HSBColor(final java.awt.Color rgbcolor) { 130 int rgb = rgbcolor.getRGB(); 131 int r = (rgb & 0xFF0000) >> 16; 132 int g = (rgb & 0xFF00) >> 8; 133 int b = (rgb & 0xFF); 134 int cmax = (r >= g) ? (r >= b) ? r : b : (g >= b) ? g : b; 135 int cmin = (r <= g) ? (r <= b) ? r : b : (g <= b) ? g : b; 136 this.m_lum = ((float) cmax) / 255f; 137 if (cmax != cmin) { 138 float difference = (cmax - cmin); 139 this.m_sat = difference / ((float) cmax); 140 if (r == cmax) { 141 this.m_hue = (g - b) / difference; 142 } else if (g == cmax) { 143 this.m_hue = (b - r) / difference + 2.0f; 144 } else { 145 this.m_hue = (r - g) / difference + 4.0f; 146 } 147 this.m_hue /= 6.0f; 148 if (this.m_hue < 0) { 149 this.m_hue += 1.0f; 150 } 151 } else { 152 this.m_sat = 0; 153 this.m_hue = 0; 154 } 155 } 156 157 172 public Object clone() { 173 return new HSBColor(this.m_hue, this.m_sat, this.m_lum); 174 } 175 176 193 public boolean equals(final Object o) { 194 if (!(o instanceof HSBColor)) { 195 return false; 196 } 197 HSBColor other = (HSBColor) o; 198 return (this.m_hue == other.m_hue) && (this.m_sat == other.m_sat) 199 && (this.m_lum == other.m_lum); 200 } 201 202 208 209 public java.awt.Color getRGBColor() { 210 return new java.awt.Color (java.awt.Color.HSBtoRGB(this.m_hue, this.m_sat, this.m_lum)); 211 } 212 213 216 public int hashCode() { 217 218 return (int) (this.m_hue * 10000 + this.m_sat * 1000 + this.m_lum * 100); 219 } 220 } 221 | Popular Tags |