@NotNull
  public static Icon colorize(@NotNull final Icon source, @NotNull Color color, boolean keepGray) {
    float[] base = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);

    final BufferedImage image =
        UIUtil.createImage(source.getIconWidth(), source.getIconHeight(), Transparency.TRANSLUCENT);
    final Graphics2D g = image.createGraphics();
    source.paintIcon(null, g, 0, 0);
    g.dispose();

    final BufferedImage img =
        UIUtil.createImage(source.getIconWidth(), source.getIconHeight(), Transparency.TRANSLUCENT);
    int[] rgba = new int[4];
    float[] hsb = new float[3];
    for (int y = 0; y < image.getRaster().getHeight(); y++) {
      for (int x = 0; x < image.getRaster().getWidth(); x++) {
        image.getRaster().getPixel(x, y, rgba);
        if (rgba[3] != 0) {
          Color.RGBtoHSB(rgba[0], rgba[1], rgba[2], hsb);
          int rgb = Color.HSBtoRGB(base[0], base[1] * (keepGray ? hsb[1] : 1f), base[2] * hsb[2]);
          img.getRaster()
              .setPixel(x, y, new int[] {rgb >> 16 & 0xff, rgb >> 8 & 0xff, rgb & 0xff, rgba[3]});
        }
      }
    }

    return createImageIcon(img);
  }
    private void setColor(Color color, Object source) {
      float[] hsb = new float[3];
      Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
      myColor = color;
      myHue = hsb[0];
      mySaturation = hsb[1];
      myBrightness = hsb[2];
      myOpacity = color.getAlpha();

      fireColorChanged(source);

      repaint();
    }
    public void setColor(Color color, Object source) {
      float[] hsb = new float[3];
      Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);

      myBrightnessComponent.setValue(255 - (int) (hsb[2] * 255));
      myBrightnessComponent.repaint();

      myColorWheel.dropImage();
      if (myOpacityComponent != null && source instanceof ColorPicker) {
        myOpacityComponent.setValue(color.getAlpha());
        myOpacityComponent.repaint();
      }

      myColorWheel.setColor(color, source);
    }
Exemple #4
0
  /**
   * Sets the selected color of this panel.
   *
   * <p>If this panel is in HUE, SAT, or BRI mode, then this method converts these values to HSB
   * coordinates and calls <code>setHSB</code>.
   *
   * <p>This method may regenerate the graphic if necessary.
   *
   * @param r the red value of the selected color.
   * @param g the green value of the selected color.
   * @param b the blue value of the selected color.
   */
  public void setRGB(int r, int g, int b) {
    if (r < 0 || r > 255)
      throw new IllegalArgumentException("The red value (" + r + ") must be between [0,255].");
    if (g < 0 || g > 255)
      throw new IllegalArgumentException("The green value (" + g + ") must be between [0,255].");
    if (b < 0 || b > 255)
      throw new IllegalArgumentException("The blue value (" + b + ") must be between [0,255].");

    if (red != r || green != g || blue != b) {
      if (mode == ColorPicker.RED || mode == ColorPicker.GREEN || mode == ColorPicker.BLUE) {
        int lastR = red;
        int lastG = green;
        int lastB = blue;
        red = r;
        green = g;
        blue = b;

        if (mode == ColorPicker.RED) {
          if (lastR != r) {
            regenerateImage();
          }
        } else if (mode == ColorPicker.GREEN) {
          if (lastG != g) {
            regenerateImage();
          }
        } else if (mode == ColorPicker.BLUE) {
          if (lastB != b) {
            regenerateImage();
          }
        }
      } else {
        float[] hsb = new float[3];
        Color.RGBtoHSB(r, g, b, hsb);
        setHSB(hsb[0], hsb[1], hsb[2]);
        return;
      }
      regeneratePoint();
      repaint();
      fireChangeListeners();
    }
  }
 private void applyColorToHSB(final Color c) {
   final float[] hbs = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
   myRed.setText(String.valueOf(((int) (360f * hbs[0]))));
   myGreen.setText(String.valueOf(((int) (100f * hbs[1]))));
   myBlue.setText(String.valueOf(((int) (100f * hbs[2]))));
 }