/* * sets the colormap to Rainbow */ private void setColorModelRainbow() { // Copy in the default colors for (int i = 0; i < tableSize; i++) { cm_red[i] = puke_red[i]; cm_green[i] = puke_green[i]; cm_blue[i] = puke_blue[i]; } // Set rainbow for attribute colors float maxHue = 0.8f; float hue = 0; float delta = maxHue / attrSize; int rainbowColor; for (int i = startColor; i <= endColor; ++i, hue += delta) { rainbowColor = Color.HSBtoRGB(hue, 1.0f, 1.0f); cm_red[i] = (byte) ((rainbowColor >> 16) & 0xff); cm_green[i] = (byte) ((rainbowColor >> 8) & 0xff); cm_blue[i] = (byte) ((rainbowColor >> 0) & 0xff); } colorModel = new IndexColorModel(8, tableSize, cm_red, cm_green, cm_blue); }
/** * Sets the selected color of this panel. * * <p>If this panel is in RED, GREEN, or BLUE mode, then this method converts these values to RGB * coordinates and calls <code>setRGB</code>. * * <p>This method may regenerate the graphic if necessary. * * @param h the hue value of the selected color. * @param s the saturation value of the selected color. * @param b the brightness value of the selected color. */ public void setHSB(float h, float s, float b) { if (Float.isInfinite(h) || Float.isNaN(h)) throw new IllegalArgumentException("The hue value (" + h + ") is not a valid number."); // hue is cyclic, so it can be any value: while (h < 0) h++; while (h > 1) h--; if (s < 0 || s > 1) throw new IllegalArgumentException("The saturation value (" + s + ") must be between [0,1]"); if (b < 0 || b > 1) throw new IllegalArgumentException("The brightness value (" + b + ") must be between [0,1]"); if (hue != h || sat != s || bri != b) { if (mode == ColorPicker.HUE || mode == ColorPicker.BRI || mode == ColorPicker.SAT) { float lastHue = hue; float lastBri = bri; float lastSat = sat; hue = h; sat = s; bri = b; if (mode == ColorPicker.HUE) { if (lastHue != hue) { regenerateImage(); } } else if (mode == ColorPicker.SAT) { if (lastSat != sat) { regenerateImage(); } } else if (mode == ColorPicker.BRI) { if (lastBri != bri) { regenerateImage(); } } } else { Color c = new Color(Color.HSBtoRGB(h, s, b)); setRGB(c.getRed(), c.getGreen(), c.getBlue()); return; } Color c = new Color(Color.HSBtoRGB(hue, sat, bri)); red = c.getRed(); green = c.getGreen(); blue = c.getBlue(); regeneratePoint(); repaint(); fireChangeListeners(); } }
/** Regenerates the image. */ private synchronized void regenerateImage() { int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) { float bri2 = this.bri; float sat2 = this.sat; float radius = ((float) size) / 2f; float hue2; float k = 1.2f; // the number of pixels to antialias for (int y = 0; y < size; y++) { float y2 = (y - size / 2f); for (int x = 0; x < size; x++) { float x2 = (x - size / 2f); double theta = Math.atan2(y2, x2) - 3 * Math.PI / 2.0; if (theta < 0) theta += 2 * Math.PI; double r = Math.sqrt(x2 * x2 + y2 * y2); if (r <= radius) { if (mode == ColorPicker.BRI) { hue2 = (float) (theta / (2 * Math.PI)); sat2 = (float) (r / radius); } else { // SAT hue2 = (float) (theta / (2 * Math.PI)); bri2 = (float) (r / radius); } row[x] = Color.HSBtoRGB(hue2, sat2, bri2); if (r > radius - k) { int alpha = (int) (255 - 255 * (r - radius + k) / k); if (alpha < 0) alpha = 0; if (alpha > 255) alpha = 255; row[x] = row[x] & 0xffffff + (alpha << 24); } } else { row[x] = 0x00000000; } } image.getRaster().setDataElements(0, y, size, 1, row); } } else if (mode == ColorPicker.HUE) { float hue2 = this.hue; for (int y = 0; y < size; y++) { float y2 = ((float) y) / ((float) size); for (int x = 0; x < size; x++) { float x2 = ((float) x) / ((float) size); row[x] = Color.HSBtoRGB(hue2, x2, y2); } image.getRaster().setDataElements(0, y, image.getWidth(), 1, row); } } else { // mode is RED, GREEN, or BLUE int red2 = red; int green2 = green; int blue2 = blue; for (int y = 0; y < size; y++) { float y2 = ((float) y) / ((float) size); for (int x = 0; x < size; x++) { float x2 = ((float) x) / ((float) size); if (mode == ColorPicker.RED) { green2 = (int) (x2 * 255 + .49); blue2 = (int) (y2 * 255 + .49); } else if (mode == ColorPicker.GREEN) { red2 = (int) (x2 * 255 + .49); blue2 = (int) (y2 * 255 + .49); } else { red2 = (int) (x2 * 255 + .49); green2 = (int) (y2 * 255 + .49); } row[x] = 0xFF000000 + (red2 << 16) + (green2 << 8) + blue2; } image.getRaster().setDataElements(0, y, size, 1, row); } } repaint(); }