/** * 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(); } }
private Color getThreadBlockColor(BumpThread bt) { if (bt == null) return Color.BLACK; synchronized (thread_colors) { Color c = thread_colors.get(bt); if (c == null) { double v; int ct = thread_colors.size(); if (ct == 0) v = 0; else if (ct == 1) v = 1; else { v = 0.5; int p0 = ct - 1; int p1 = 1; for (int p = p0; p > 1; p /= 2) { v /= 2.0; p0 -= p1; p1 *= 2; } if ((p0 & 1) == 0) p0 = 2 * p1 - p0 + 1; v = v * p0; } float h = (float) (v * 0.8); float s = 0.7f; float b = 1.0f; int rgb = Color.HSBtoRGB(h, s, b); rgb |= 0xc0000000; c = new Color(rgb, true); thread_colors.put(bt, c); } return c; } }
/** 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(); }