1 11 package org.eclipse.swt.graphics; 12 13 import org.eclipse.swt.internal.SerializableCompatibility; 14 import org.eclipse.swt.*; 15 16 38 39 public final class RGB implements SerializableCompatibility { 40 41 44 public int red; 45 46 49 public int green; 50 51 54 public int blue; 55 56 static final long serialVersionUID = 3258415023461249074L; 57 58 70 public RGB(int red, int green, int blue) { 71 if ((red > 255) || (red < 0) || 72 (green > 255) || (green < 0) || 73 (blue > 255) || (blue < 0)) 74 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 75 this.red = red; 76 this.green = green; 77 this.blue = blue; 78 } 79 80 95 public RGB(float hue, float saturation, float brightness) { 96 if (hue < 0 || hue > 360 || saturation < 0 || saturation > 1 || 97 brightness < 0 || brightness > 1) { 98 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 99 } 100 float r, g, b; 101 if (saturation == 0) { 102 r = g = b = brightness; 103 } else { 104 if (hue == 360) hue = 0; 105 hue /= 60; 106 int i = (int)hue; 107 float f = hue - i; 108 float p = brightness * (1 - saturation); 109 float q = brightness * (1 - saturation * f); 110 float t = brightness * (1 - saturation * (1 - f)); 111 switch(i) { 112 case 0: 113 r = brightness; 114 g = t; 115 b = p; 116 break; 117 case 1: 118 r = q; 119 g = brightness; 120 b = p; 121 break; 122 case 2: 123 r = p; 124 g = brightness; 125 b = t; 126 break; 127 case 3: 128 r = p; 129 g = q; 130 b = brightness; 131 break; 132 case 4: 133 r = t; 134 g = p; 135 b = brightness; 136 break; 137 case 5: 138 default: 139 r = brightness; 140 g = p; 141 b = q; 142 break; 143 } 144 } 145 red = (int)(r * 255 + 0.5); 146 green = (int)(g * 255 + 0.5); 147 blue = (int)(b * 255 + 0.5); 148 } 149 150 157 public float[] getHSB() { 158 float r = red / 255f; 159 float g = green / 255f; 160 float b = blue / 255f; 161 float max = Math.max(Math.max(r, g), b); 162 float min = Math.min(Math.min(r, g), b); 163 float delta = max - min; 164 float hue = 0; 165 float brightness = max; 166 float saturation = max == 0 ? 0 : (max - min) / max; 167 if (delta != 0) { 168 if (r == max) { 169 hue = (g - b) / delta; 170 } else { 171 if (g == max) { 172 hue = 2 + (b - r) / delta; 173 } else { 174 hue = 4 + (r - g) / delta; 175 } 176 } 177 hue *= 60; 178 if (hue < 0) hue += 360; 179 } 180 return new float[] {hue, saturation, brightness}; 181 } 182 183 193 public boolean equals(Object object) { 194 if (object == this) return true; 195 if (!(object instanceof RGB)) return false; 196 RGB rgb = (RGB)object; 197 return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue); 198 } 199 200 210 public int hashCode() { 211 return (blue << 16) | (green << 8) | red; 212 } 213 214 220 public String toString() { 221 return "RGB {" + red + ", " + green + ", " + blue + "}"; } 223 224 } 225 | Popular Tags |