import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class albnegru1 extends Applet 
{

 Image rimg;

  albnegru1 (Image source) {
//	canFilterIndexColorModel=true;
	ColorModel cm=ColorModel.getRGBdefault();
	boolean success;
        int widtho,heighto,oldi;
	int[] Pixo,Pixn;
	int pixelval;
	
	PixelGrabber pg;
	widtho = source.getWidth(this);
	heighto = source.getHeight(this);


	Pixo = new int[widtho*heighto];     
	Pixn = new int[widtho*heighto];     

	pg = new PixelGrabber(source.getSource(), 0,0,widtho,heighto,Pixo,0,widtho);

	try
	{
		success = pg.grabPixels(0);             
	}                                                                      
	catch (Exception e)
	{
		System.out.println("Eroare!! " + e);
	}

	for (int y=0; y < heighto; y++)
	{
	   for (int x=0; x < widtho; x++)
	   {
		int r=cm.getRed(Pixo[y*widtho+x]);
		int g=cm.getGreen(Pixo[y*widtho+x]);
		int b=cm.getBlue(Pixo[y*widtho+x]);
		int alp=cm.getAlpha(Pixo[y*widtho+x]);
                double xx=Math.sqrt(r*r+g*g+b*b);
                double yy=Math.sqrt((255-r)*(255-r)+(255-g)*(255-g)+(255-b)*(255-b));
                double zz=255*Math.pow(3,0.5);
                double z1=(xx*xx-yy*yy+zz*zz)/(2*zz);
                int r1=(int)(z1/(Math.pow(3,0.5)));  

//                int r1=(int)((r+g+b)/3.0f);
                r1=Math.max(0,Math.min(r1,255));
                int g1=r1;
                int b1=r1; 
		Pixn[y*widtho+x]=(alp<<24) | (r1<<16) | (g1<<8) | (b1<<0);
	   }
	}

	rimg = createImage(new MemoryImageSource(widtho,heighto,Pixn,0,widtho));
  }
    
}