// implementation of java.awt.event.MouseMotionListener interface
  public void mouseDragged(MouseEvent e) {
    if (e.getSource() == cs) {
      int i = e.getX();
      int j = e.getY();

      Color c = new Color(cs.getRGB(i, j));

      updateColor(c, e.getSource());
    }
  }
Example #2
0
 private static ImageIcon makeGrayImageIcon1(Image img) {
   BufferedImage source =
       new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
   Graphics g = source.createGraphics();
   g.drawImage(img, 0, 0, null);
   g.dispose();
   ColorConvertOp colorConvert =
       new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
   BufferedImage destination = colorConvert.filter(source, null);
   return new ImageIcon(destination);
 }
  /** Decodes an image */
  public PlanarImage decodeJAI() throws IOException {

    // ------------------------------------------- construimos la imagen
    int tileWidth = width, tileHeight = height;

    int[] order = new int[bands];
    for (int i = 0; i < bands; i++) order[i] = i;
    ComponentSampleModel csm =
        new ComponentSampleModel(
            DataBuffer.TYPE_SHORT, tileWidth, tileHeight, tileWidth * bands, bands, order);

    for (int i = 0; i < bands; i++) order[i] = 16;
    ColorSpace cs;
    switch (bands) {
      case 1:
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        break;
      case 3:
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        break;
      default:
        // F = 15 component
        cs = ColorSpace.getInstance(ColorSpace.TYPE_FCLR);
    }
    ComponentColorModel ccm =
        new ComponentColorModel(
            cs, order, false, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);

    outImage = new TiledImage(0, 0, width, height, 0, 0, csm, ccm);

    // --------------------------------------------------- decodificamos

    decode(); // ya redefinimos aqui el setValue, asi que todo el proceso es igual

    return outImage;
  }
  public ColorChooser(Dialog d) {
    dialog = d;

    JCContainer spinboxContainer = new JCContainer();

    cs = new ColorSpace();
    cs.addMouseListener(this);
    cs.addMouseMotionListener(this);

    Layout.fill(this, cs, 0, 0, GridBagConstraints.BOTH);

    JCLabel redLabel = new JCLabel("Red");
    red = new JCSpinBox(3);
    red.setAutoArrowDisable(false);
    red.setMinimum(0);
    red.setMaximum(255);

    JCLabel greenLabel = new JCLabel("Green");
    green = new JCSpinBox(3);
    green.setMinimum(0);
    green.setMaximum(255);

    JCLabel blueLabel = new JCLabel("Blue");
    blue = new JCSpinBox(3);
    blue.setMinimum(0);
    blue.setMaximum(255);

    Layout.fill(spinboxContainer, redLabel, 0, 0, GridBagConstraints.NONE);
    Layout.fill(spinboxContainer, red, 1, 0, GridBagConstraints.NONE);

    Layout.fill(spinboxContainer, greenLabel, 0, 1, GridBagConstraints.NONE);
    Layout.fill(spinboxContainer, green, 1, 1, GridBagConstraints.NONE);

    Layout.fill(spinboxContainer, blueLabel, 0, 2, GridBagConstraints.NONE);
    Layout.fill(spinboxContainer, blue, 1, 2, GridBagConstraints.NONE);

    JCLabel thisColor = new JCLabel("New");
    colorSample = new Panel();

    // common colours

    JCContainer commonContainer = new JCContainer();

    String commonColors[] = {
      "red",
      "orange",
      "brown",
      "yellow",
      "green",
      "blue",
      "cyan",
      "magenta",
      "purple",
      "white",
      "lightgrey",
      "grey",
      "darkgrey",
      "black"
    };

    for (int i = 0; i < commonColors.length; i++) {
      JCButton b = new JCButton();
      b.setPreferredSize(2, 10);
      b.setBackground(new Color(Color32.getColorFromName(commonColors[i])));
      b.addActionListener(this);
      Layout.fill(commonContainer, b, i, 0, GridBagConstraints.NONE);
    }

    Layout.fill(this, commonContainer, 0, 1, GridBagConstraints.HORIZONTAL);

    JCLabel hexLabel = new JCLabel("Hex");
    hexText = new TextField("");
    hexText.setColumns(6);
    hexText.setFont(new Font("courier", Font.PLAIN, 9));
    hexText.addActionListener(this);

    Layout.fill(spinboxContainer, hexLabel, 0, 4, GridBagConstraints.HORIZONTAL);
    Layout.fill(spinboxContainer, hexText, 1, 4, GridBagConstraints.HORIZONTAL);

    Layout.fill(spinboxContainer, thisColor, 0, 5, GridBagConstraints.HORIZONTAL);
    Layout.fill(spinboxContainer, colorSample, 1, 5, GridBagConstraints.HORIZONTAL);

    Layout.fill(this, spinboxContainer, 1, 0, GridBagConstraints.NONE);

    JCContainer buttonContainer = new JCContainer();

    ok = new JCButton("OK");
    ok.addActionListener(this);
    cancel = new JCButton("Cancel");
    cancel.addActionListener(this);

    Layout.fill(buttonContainer, ok, 0, 0, GridBagConstraints.NONE);
    Layout.fill(buttonContainer, cancel, 1, 0, GridBagConstraints.NONE);

    Layout.fill(this, buttonContainer, 0, 2, GridBagConstraints.NONE);

    doLayout();
  }
Example #5
0
  public BufferedImage getBufferedImage(int type, Color c) {

    BufferedImage image = null;
    float[] colComp = new float[3];
    c.getRGBColorComponents(colComp);
    double red = (double) colComp[0];
    double green = (double) colComp[1];
    double blue = (double) colComp[2];
    // System.out.println("blue, green, red = "+ blue +", " + green + ", " + red);

    double x = 0.0;
    double x2;
    switch (type) {
      case ScalarImage.TYPE_BYTE_RANDOM:
        {
          int numCol = 256;
          byte[] bBuf = new byte[numCol * 3];
          blue *= 255 * 4.;
          green *= 255 * 4.;
          red *= 255 * 4.;
          double delta = 1.0 / (double) (numCol + 1);
          int j = 0;
          for (int i = 0; i < numCol; i++) {
            if (i % 5 == 0) x = 0.7 * Math.random() + 0.3 * x;
            x2 = x * x;
            bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
            bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
            bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
            // x += delta;
          }
          IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false);
          // image = new
          // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm);
          byte[] idxBuffer = new byte[size];
          for (int i = 0; i < size; i++) {
            idxBuffer[i] = (byte) (clamp(f[i] * 255.));
          }
          DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size);
          int idxOffset[] = {0};
          int idxBits[] = {8};
          try {
            ComponentSampleModel idxSampleModel =
                new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset);
            WritableRaster rasterIdx =
                java.awt.image.Raster.createWritableRaster(
                    idxSampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(cm, rasterIdx, false, null);
          } catch (Exception e) {
            System.out.println("Exception caught while acquiring image:");
            System.out.println(e.getMessage());
          }
        }
        break;
      case BufferedImage.TYPE_BYTE_INDEXED:
        {
          int numCol = 256;
          byte[] bBuf = new byte[numCol * 3];
          blue *= 255 * 4.;
          green *= 255 * 4.;
          red *= 255 * 4.;
          double delta = 1.0 / (double) (numCol + 1);
          int j = 0;
          for (int i = 0; i < numCol; i++) {
            x2 = x * x;
            bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
            bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
            bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
            x += delta;
          }
          IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false);
          // image = new
          // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm);
          byte[] idxBuffer = new byte[size];
          for (int i = 0; i < size; i++) {
            idxBuffer[i] = (byte) (clamp(f[i] * 255.));
          }
          DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size);
          int idxOffset[] = {0};
          int idxBits[] = {8};
          try {
            ComponentSampleModel idxSampleModel =
                new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset);
            WritableRaster rasterIdx =
                java.awt.image.Raster.createWritableRaster(
                    idxSampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(cm, rasterIdx, false, null);
          } catch (Exception e) {
            System.out.println("Exception caught while acquiring image:");
            System.out.println(e.getMessage());
          }
        }
        break;
      case BufferedImage.TYPE_BYTE_GRAY:
        break;
      case BufferedImage.TYPE_3BYTE_BGR:
      default:
        byte[] byteBuffer = new byte[size * 3];
        blue *= 255 * 4.;
        green *= 255 * 4.;
        red *= 255 * 4.;

        int j = 0;
        for (int i = 0; i < size; i++) {
          x = f[i];
          x2 = x * x;
          /*
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * red ) * x * x + ( 4 * red - 255 ) * x);
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * green ) * x * x + ( 4 * green - 255 ) * x);
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * blue ) * x * x + ( 4 * blue - 255 ) * x);
          */
          byteBuffer[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
          byteBuffer[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
          byteBuffer[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
        }
        DataBufferByte dataBuffer = new DataBufferByte(byteBuffer, size * 3);
        int componentOffset[] = {0, 1, 2};
        int componentBits[] = {8, 8, 8};
        try {
          WritableRaster raster =
              java.awt.image.Raster.createWritableRaster(
                  new PixelInterleavedSampleModel(
                      DataBuffer.TYPE_BYTE, width, height, 3, width * 3, componentOffset),
                  dataBuffer,
                  new Point(0, 0));
          image =
              new BufferedImage(
                  new ComponentColorModel(
                      ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB),
                      componentBits,
                      false,
                      false,
                      ColorModel.OPAQUE,
                      DataBuffer.TYPE_BYTE),
                  raster,
                  false,
                  null);

        } catch (Exception e) {
          System.out.println("Exception caught while acquiring image:");
          System.out.println(e.getMessage());
        }
        break;
    }
    return image;
  }