Пример #1
0
  /**
   * Sets the hexadecimal notation for Red, Green, and Blue. This will automatically populate all
   * the other fields, too.
   *
   * @param hex Hexadecimal notation of Red, Green and Blue in the range of 000000-FFFFFF
   * @throws java.lang.Exception A generic exception if the hexadecimal notation is bad.
   */
  public void setHex(String hex) throws Exception {
    Color color = new Color();
    color.setHex(hex);

    this.red = color.getRed();
    this.green = color.getGreen();
    this.blue = color.getBlue();
    this.hue = color.getHue();
    this.saturation = color.getSaturation();
    this.brightness = color.getValue();

    tbRed.setText(Integer.toString(this.red));
    tbGreen.setText(Integer.toString(this.green));
    tbBlue.setText(Integer.toString(this.blue));
    tbHue.setText(Integer.toString(this.hue));
    tbSaturation.setText(Integer.toString(this.saturation));
    tbBrightness.setText(Integer.toString(this.brightness));
    tbHexColor.setText(color.getHex());
    setPreview(color.getHex());

    updateSliders();
  }
Пример #2
0
  /**
   * Sets the Red, Green, and Blue color variables. This will automatically populate the Hue,
   * Saturation and Brightness and Hexadecimal fields, too.
   *
   * <p>The RGB color model is an additive color model in which red, green, and blue light are added
   * together in various ways to reproduce a broad array of colors. The name of the model comes from
   * the initials of the three additive primary colors, red, green, and blue.
   *
   * @param red strength - valid range is 0-255
   * @param green strength - valid range is 0-255
   * @param blue strength - valid range is 0-255
   * @throws java.lang.Exception Exception if the Red, Green or Blue variables are out of range.
   */
  public void setRGB(int red, int green, int blue) throws Exception {
    Color color = new Color();
    color.setRGB(red, green, blue);

    this.red = red;
    this.green = green;
    this.blue = blue;
    this.hue = color.getHue();
    this.saturation = color.getSaturation();
    this.brightness = color.getValue();

    tbRed.setText(Integer.toString(this.red));
    tbGreen.setText(Integer.toString(this.green));
    tbBlue.setText(Integer.toString(this.blue));
    tbHue.setText(Integer.toString(this.hue));
    tbSaturation.setText(Integer.toString(this.saturation));
    tbBrightness.setText(Integer.toString(this.brightness));
    tbHexColor.setText(color.getHex());
    setPreview(color.getHex());

    updateSliders();
  }
Пример #3
0
  /**
   * Fired whenever something in this widget changes.
   *
   * <p>Subclasses that override this method must call <tt>super.onChange(sender)</tt> to ensure
   * that the Widget recieves its events.
   *
   * @param sender the widget that has changed.
   */
  public void onChange(Widget sender) {
    if (sender == tbHexColor) {
      // Figure out colors
      // Color class will do bounds check on hex input
      try {
        Color color = new Color();
        color.setHex(tbHexColor.getText());
        tbHue.setText(Integer.toString(color.getHue()));
        tbSaturation.setText(Integer.toString(color.getSaturation()));
        tbBrightness.setText(Integer.toString(color.getValue()));
        tbRed.setText(Integer.toString(color.getRed()));
        tbGreen.setText(Integer.toString(color.getGreen()));
        tbBlue.setText(Integer.toString(color.getBlue()));
        tbHexColor.setText(color.getHex());
        setPreview(color.getHex());
      } catch (Exception e) {
      }
    }

    if (sender == tbRed || sender == tbGreen || sender == tbBlue) {
      // Don't allow this value to overflow or underflow
      try {
        if (Integer.parseInt(((TextBox) sender).getText()) > 255) {
          ((TextBox) sender).setText("255");
        }
        if (Integer.parseInt(((TextBox) sender).getText()) < 0) {
          ((TextBox) sender).setText("0");
        }
      } catch (Exception e) {
      }

      red = Integer.parseInt(tbRed.getText());
      green = Integer.parseInt(tbGreen.getText());
      blue = Integer.parseInt(tbBlue.getText());
      hue = Integer.parseInt(tbHue.getText());
      saturation = Integer.parseInt(tbSaturation.getText());
      brightness = Integer.parseInt(tbBrightness.getText());

      // Figure out the colors
      try {
        Color color = new Color();
        color.setRGB(red, green, blue);
        tbHue.setText(Integer.toString(color.getHue()));
        tbSaturation.setText(Integer.toString(color.getSaturation()));
        tbBrightness.setText(Integer.toString(color.getValue()));
        tbHexColor.setText(color.getHex());
        setPreview(color.getHex());
      } catch (Exception e) {
      }
    } else if (sender == tbHue || sender == tbSaturation || sender == tbBrightness) {
      // Don't allow this value to overflow
      try {
        if (Integer.parseInt(tbHue.getText()) > 359) {
          tbHue.setText("359");
        }

        if (Integer.parseInt(tbSaturation.getText()) > 100) {
          tbSaturation.setText("100");
        }

        if (Integer.parseInt(tbBrightness.getText()) > 100) {
          tbBrightness.setText("100");
        }
      } catch (Exception e) {
      }

      red = Integer.parseInt(tbRed.getText());
      green = Integer.parseInt(tbGreen.getText());
      blue = Integer.parseInt(tbBlue.getText());
      hue = Integer.parseInt(tbHue.getText());
      saturation = Integer.parseInt(tbSaturation.getText());
      brightness = Integer.parseInt(tbBrightness.getText());

      // Figure out colors
      try {
        Color color = new Color();
        color.setHSV(hue, saturation, brightness);
        tbRed.setText(Integer.toString(color.getRed()));
        tbGreen.setText(Integer.toString(color.getGreen()));
        tbBlue.setText(Integer.toString(color.getBlue()));
        tbHexColor.setText(color.getHex());
        setPreview(color.getHex());
      } catch (Exception e) {
      }
    }

    // Let the sliders know something's changed
    updateSliders();

    ChangeEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
  }